Pie & Doughnut in ASP.NET Core Accumulation Chart Component

18 Nov 201821 minutes to read

Pie Chart

To render a pie series, use the series type as Pie.

Radius Customization

By default, radius of the pie series will be 80% of the size (minimum of chart width and height). You can customize this using radius property of the series.

Pie Center

The center position of the pie can be changed by Center X and Center Y. By default, center value of the pie series x and y is 50%. You can customize this using center property of the series.

Various Radius Pie Chart

You can use radius mapping to render the slice with different radius pie and also use border , fill properties to customize the point. dataLabel is used to represent individual data and its value.

## Doughnut Chart

To achieve a doughnut in pie series, customize the [`innerRadius`](https://help.syncfusion.com/cr/aspnetcore-js2/Syncfusion.EJ2.Charts.AccumulationSeries.html#Syncfusion_EJ2_Charts_AccumulationSeries_InnerRadius) property of the series. By setting value greater than 0%, a doughnut will appear. The `innerRadius` property takes value from 0% to 100% of the pie radius.

Multiple Doughnut Series

You can create multiple doughnut within a single chart by adding multiple series with different innerRadius and radius values. This allows you to compare multiple data sets with the same categories. Each series can have different data, colors, and customizations. You can also use the mappingKey property in legendSettings to map the legend items based on the specified field from the data source. When set, points with matching mappingKey values are grouped into a single legend item.

Start and End angles

You can customize the start and end angle of the pie series using the startAngle and endAngle properties. The default value of startAngle is 0 degree, and endAngle is 360 degrees. By customizing this, you can achieve a semi pie series.

Color & Text Mapping

The fill color and the text in the data source can be mapped to the chart using pointColorMapping in series and name in datalabel respectively.

Border radius

You can create rounded corners for each slice by using the BorderRadius option, which gives the chart a modern and polished appearance.

Customization

Individual points can be customized using the pointRender event.

Patterns

You can apply different patterns to the pie slices using the ApplyPattern property in the series and the PointRender event.

Hide pie or doughnut border

By default, the border will appear in the pie or doughnut chart while mouse hover on the chart. You can disable the border by setting EnableBorderOnMouseMove property is false.

Color Palette

You can customize the color of the point using the palettes property.

Multi-level pie chart

You can achieve a multi-level drill down in pie and doughnut charts using pointClick event. If user clicks any point in the chart, that corresponding data will be shown in the next level and so on based on point clicked.

You can also achieve drill-up (back to the initial state) by using chartMouseClick event. In below sample, you can drill-up by clicking back button in the center of the chart.

<div>
    <ejs-accumulationchart id="chart" textRender="textRender" PointClick="pointClick" chartMouseClick="chartMouseClick" title="Automobile Sales by Category">
        <e-accumulationchart-legendsettings visible="false">
        </e-accumulationchart-legendsettings>
        <e-accumulation-series-collection>
            <e-accumulation-series explode="false" type="Pie" xName="x" yName="y" dataSource="ViewBag.dataSource" name="Project">
                <e-accumulationseries-datalabel visible="true" name="text">
                    <e-font fontWeight="600"></e-font>
                </e-accumulationseries-datalabel>
            </e-accumulation-series>
        </e-accumulation-series-collection>
    </ejs-accumulationchart>

    <ejs-grid id="grid" dataSource="ViewBag.dataSource">
        <e-grid-columns>
            <e-grid-column field="x" headerText="Vehicle" type="string"></e-grid-column>
            <e-grid-column field="y" headerText="Sales" type="string"></e-grid-column>
        </e-grid-columns>
    </ejs-grid>
</div>
<script>
    var pointIndex = -1;
    var data = [
        {
            x: 'SUV',
            y: 25,
            z: [
                {
                    title: 'Automobile Sales in the SUV Segment',
                    x: 'Toyota',
                    y: 8,
                    z: [
                        { x: '2000', y: 20 },
                        { x: '2001', y: 30 },
                        { x: '2002', y: 40 },
                    ],
                },
                { x: 'Ford', y: 12 },
                { x: 'GM', y: 17 },
                { x: 'Renault', y: 6 },
            ],
        },
        {
            x: 'Car',
            y: 37,
            z: [
                { title: 'Automobile Sales in the Car Segment', x: 'Toyota', y: 7 },
                { x: 'Chrysler', y: 12 },
                { x: 'Nissan', y: 9 },
                { x: 'Ford', y: 15 },
            ],
        },
        {
            x: 'Pickup',
            y: 15,
            z: [
                { title: 'Automobile Sales in the Pickup Segment', x: 'Nissan', y: 9 },
                { x: 'Chrysler', y: 4 },
                { x: 'Ford', y: 7 },
                { x: 'Toyota', y: 20 },
            ],
        },
        {
            x: 'Minivan',
            y: 23,
            z: [
                { title: 'Automobile Sales in the Minivan Segment', x: 'Hummer', y: 11 },
                { x: 'Ford', y: 5 },
                { x: 'GM', y: 12 },
                { x: 'Chrysler', y: 3 },
            ],
        },
    ];

    var textRender = function (args) {
        args.text = args.point.x + ' ' + args.point.y + ' %';
    };

    var chartMouseClick = function (args) {
        var pie = document.getElementById("chart").ej2_instances[0];
        var grid = document.getElementById("grid").ej2_instances[0];
        if (args.target.indexOf('back') > -1) {
            if (pie.series[0].name === 'Level 3') {
                pie.series[0].dataSource = data[window.pointIndex].z;
                pie.series[0].name = 'Level 2';
                pie.title = data[window.pointIndex].z[0].title;
                pie.series[0].innerRadius = '30%';
                grid.dataSource = pie.series[0].dataSource;
                grid.columns[0].headerText = data[window.pointIndex].x;
                grid.refresh();
                pie.refresh();
            } else if (pie.series[0].name === 'Level 2') {
                pie.series[0].dataSource = data;
                pie.series[0].name = 'Level 1';
                pie.series[0].innerRadius = '0%';
                pie.title = 'Automobile Sales by Category';
                pie.annotations = [{}];
                pie.pointClick = pointClick;
                grid.dataSource = pie.series[0].dataSource;
                grid.columns[0].headerText = 'Vehicle';
                grid.refresh();
                pie.refresh();
            }
        }
        grid.dataSource = pie.series[0].dataSource;
    };
    var pointClick = function (args) {
        var pie = document.getElementById("chart").ej2_instances[0];
        var grid = document.getElementById("grid").ej2_instances[0];
        if (
            ej.charts.getElement(
                pie.element.id +
                '_Series_' +
                args.seriesIndex +
                '_Point_' +
                args.pointIndex
            )
        ) {
            pie.series[0].dataSource = data[args.pointIndex].z;
            pie.title = data[args.pointIndex].z[0].title;
            window.pointIndex = args.pointIndex;

            pie.series[0].name = 'Level 2';
            pie.series[0].innerRadius = '30%';
            pie.annotations = [
                {
                    content:
                        '<div id="back" style="cursor:pointer;padding:3px;width:30px; height:30px;">' +
                        '<img src="https://ej2.syncfusion.com/javascript/demos/src/chart/images/back.png" id="back" />',
                    region: 'Series',
                    x: '50%',
                    y: '50%',
                },
            ];
            pie.pointClick = click;
        }
        grid.dataSource = pie.series[0].dataSource;
        grid.columns[0].headerText = data[args.pointIndex].x;
        grid.refresh();
        pie.refresh();
    }
    var click = function (args) {
        var pie = document.getElementById("chart").ej2_instances[0];
        var grid = document.getElementById("grid").ej2_instances[0];
        if (pie.series[0].name !== 'Level 3') {
            switch (args.pointIndex) {
                case 0:
                    pie.series[0].dataSource = data[0].z[0].z;
                    pie.title = 'SUV Sales by Years';
                    pie.series[0].name = 'Level 3';
                    grid.columns[0].headerText = 'Year';
                    grid.refresh();
                    pie.refresh();
                    break;
            }
            grid.dataSource = pie.series[0].dataSource;
        }
    };
</script>
public IActionResult Index()
    {
        List<CategoryData> chartData = new List<CategoryData>
        {
            new CategoryData { x = "SUV", y = 25 },
            new CategoryData { x = "Car", y = 37 },
            new CategoryData { x = "Pickup", y = 15 },
            new CategoryData { x = "Minivan", y = 23 },

            };
        ViewBag.datalabel = new
        {
            visible = true,
            position = "Inside",
            name = "text",
            font = new
            {
                fontWeight = "600"
            }
        };
        ViewBag.dataSource = chartData;

        return View();
    }
    public class CategoryData
    {
        public string x;
        public double y;
    }

ASP.NET Core AccumulationChart Drill-Down and Display Data in Grid

See Also