Search results

Add or Remove Series in ASP.NET Core Chart control

22 Jul 2021 / 1 minute to read

You can add or remove the chart series dynamically by using the addSeries or removeSeries method.

To add or remove the series dynamically, follow the given steps:

Step 1:

To add a new series to chart dynamically, pass the series value to the addSeries method.

To remove the new series from chart dynamically, pass the series index to the removeSeries method.

tagHelper
add-remove.cs
Copied to clipboard
<button id="add">Add</button>
<button id="remove">Remove</button>
        <ejs-chart id="container" title="Sales History of Product X" >
            <e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
            <e-chart-primaryyaxis title="Profit ($)"></e-chart-primaryyaxis>
            <e-series-collection>
                <e-series name="Product X" dataSource="ViewBag.dataSource" xName="x" yName="yValue"
                          type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column"
                         ></e-series>
            </e-series-collection>
        </ejs-chart>
<script>
    document.getElementById("add").onclick = function () {
        var chart = document.getElementById("container").ej2_instances[0];
        chart.addSeries([{
            type: 'Column',
            dataSource: [{ x: 'John', y: 11000 }, { x: 'Jake', y: 16000 }, { x: 'Peter', y: 19000 },
            { x: 'James', y: 12000 }, { x: 'Mary', y: 10700 }],
            xName: 'x', width: 2,
            yName: 'y'
        }]);
    };
    document.getElementById("remove").onclick = function () {
        var chart = document.getElementById("container").ej2_instances[0];
        chart.removeSeries(1);
    }
</script>
Copied to clipboard
public ActionResult Index()
        {
            List<ColumnChartData> chartData = new List<ColumnChartData>
            {
                new ColumnChartData { x= "John", yValue= 10000, yValue1=37, yValue2=38 },
                new ColumnChartData { x= "Jake", yValue= 12000, yValue1=23, yValue2=17 },
                new ColumnChartData { x= "Peter", yValue= 18000, yValue1=18, yValue2=26 },
                new ColumnChartData { x= "James", yValue= 11000, yValue1=18, yValue2=26 }
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class ColumnChartData
        {
            public string x;
            public double yValue;
            public double yValue1;
            public double yValue2;
        }