Search results

Getting started with ASP.NET MVC Bullet Chart control

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your ASP.NET MVC application to use our components.

Prerequisites

To get start with ASP.NET MVC application, need to ensure the following software to be installed on the machine.

  1. .Net Framework 4.5 and above.
  2. ASP.NET MVC 4 or ASP.NET MVC 5
  3. Visual Studio

Preparing ASP.NET MVC application

The following steps to create ASP.NET MVC Application.

Step 1: Create ASP.NET MVC Application with default template project in Visual Studio.

Default Template

Step 2: Once your project created. We need to add Syncfusion EJ2 package into your application by using NuGet Package Manager.

Open the NuGet package manager.

Solution Explorer

Install the Syncfusion.EJ2.MVC4 package to the application.

Nuget Demo

After installation complete, this will be included in the project. You can refer it from the Project Assembly Reference.

We need to install NewtonSoft.JSON as a dependency, since Syncfusion.EJ2 dependent to NewtonSoft.JSON package.

Step 3: Add Syncfusion.EJ2 namespace reference in Web.config

<namespaces>
    <add namespace="Syncfusion.EJ2"/>
</namespaces>
<system.web>
    <compilation>
      <assemblies>
        <add assembly="Syncfusion.EJ2" Version=15.3400.0.27, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
  </system.web>

Step 4: Add client side resource through CDN or local package in the layout page _Layout.cshtml.

<head>
@* Syncfusion Essential JS 2 Styles *@
<link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/material.css" />

@* Syncfusion Essential JS 2 Scripts *@
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js"></script>
</head>

Step 5: Adding Script Manager in layout page _Layout.cshtml.

@Html.EJS().ScriptManager()

Step 6: Add the below code to your Index.cshtml view page which is present under Views/Home folder, to initialize the chart.

<div>
@(Html.EJS().BulletChart("container1").Render())
</div>

BulletChart With Data

This section explains how to plot local data to the bullet chart.

public ActionResult Index()
{
    List < BulletChartData > data = new List < BulletChartData >{
    new BulletChartData{ value = 100, target = 80 },
    new BulletChartData{ value = 200, target = 180 },
    new BulletChartData{ value = 300, target = 280 },
    new BulletChartData{ value = 400, target = 380 },
    new BulletChartData{ value = 500, target = 480 }
    };
    ViewBag.dataSource = data;
    return View();
}
public class BulletChartData {
    public double target;
    public double value;
}

Now assign the ViewBag.dataSource to DataSource property. value and target values should be mapped with ValueName and TargetName respectively.

razor
default.cs
@Html.EJS().BulletChart("container")
                        .ValueField("value")
                        .TargetField("target")
                        .Minimum(0).Maximum(300).Interval(50)
                        .DataSource(ViewBag.dataSource)
                        .Render()
public IActionResult Index()
        {
            List<DefaultBulletData> bulletData = new List<DefaultBulletData>
            {
                new DefaultBulletData { value = 270, target = 250}     
            };
            ViewBag.dataSource = bulletData;
            return View();
        }
        public class DefaultBulletData
        {           
            public double value;
            public double target;
        }

Add Bullet Chart Title

You can add a title using Title property to the bullet chart to provide quick information to the user about the data plotted in the bullet chart.

razor
title.cs
@Html.EJS().BulletChart("container")
                        .Title("Revenue")
                        .ValueField("value")
                        .TargetField("target")
                        .Minimum(0).Maximum(300).Interval(50)
                        .DataSource(ViewBag.dataSource)
                        .Render()
public IActionResult Index()
        {
            List<DefaultBulletData> bulletData = new List<DefaultBulletData>
            {
                new DefaultBulletData { value = 270, target = 250}     
            };
            ViewBag.dataSource = bulletData;
            return View();
        }
        public class DefaultBulletData
        {           
            public double value;
            public double target;
        }

Ranges

You can add a range using Ranges property to the bullet chart.

razor
ranges.cs
@Html.EJS().BulletChart("container")
                        .Title("Revenue")
                        .ValueField("value")
                        .TargetField("target")
                        .Ranges(rn =>
                        {
                            rn.End(100).Add();
                            rn.End(200).Add();
                            rn.End(300).Add();
                        })
                        .Minimum(0).Maximum(300).Interval(50)
                        .DataSource(ViewBag.dataSource)
                        .Render()
public IActionResult Index()
        {
            List<DefaultBulletData> bulletData = new List<DefaultBulletData>
            {
                new DefaultBulletData { value = 270, target = 250}     
            };
            ViewBag.dataSource = bulletData;
            return View();
        }
        public class DefaultBulletData
        {           
            public double value;
            public double target;
        }

Tooltip

You can use tooltip for the bullet chart by setting the Enable property to true in Tooltip.

razor
tooltip.cs
@Html.EJS().BulletChart("container")
                        .Title("Revenue")
                        .Tooltip(tp => tp.Enable(true))
                        .ValueField("value")
                        .TargetField("target")
                        .Ranges(rn =>
                        {
                            rn.End(100).Add();
                            rn.End(200).Add();
                            rn.End(300).Add();
                        })
                        .Minimum(0).Maximum(300).Interval(50)
                        .DataSource(ViewBag.dataSource)
                        .Render()
public IActionResult Index()
        {
            List<DefaultBulletData> bulletData = new List<DefaultBulletData>
            {
                new DefaultBulletData { value = 270, target = 250}     
            };
            ViewBag.dataSource = bulletData;
            return View();
        }
        public class DefaultBulletData
        {           
            public double value;
            public double target;
        }