# Charts

**You can create/modify/remove all types of Excel charts with EPPlus. It [supports all Excel 2019 chart types](<https://epplussoftware.com/docs/charts-and-drawing-objects/charts/supported-charts/index.md>) with modern chart styling.**

### Example 1 - add a Pie chart

To add a chart use the `AddChart` method or even better its typed variant (in this case `AddPieChart`).

```csharp
//Add the piechart
var pieChart = worksheet.Drawings.AddPieChart("crtExtensionsSize", ePieChartType.PieExploded3D);
//Set top left corner to row 1 column 2
pieChart.SetPosition(1, 0, 2, 0);
pieChart.SetSize(400, 400);
var chartRange = worksheet.Cells["A3:B14];
pieChart.Series.Add(chartRange.TakeSingleColumn(1), chartRange.TakeSingleColumn(0));

pieChart.Title.Text = "Extension Size";
//Set datalabels and remove the legend
pieChart.DataLabel.ShowCategory = true;
pieChart.DataLabel.ShowPercent = true;
pieChart.DataLabel.ShowLeaderLines = true;
pieChart.Legend.Remove();
```

```vbnet
'Add the piechart
Dim pieChart = worksheet.Drawings.AddPieChart("crtExtensionsSize", ePieChartType.PieExploded3D)
'Set top left corner to row 1 column 2
pieChart.SetPosition(1, 0, 2, 0)
pieChart.SetSize(400, 400)
Dim chartRange = worksheet.Cells("A3:B14")
pieChart.Series.Add(chartRange.TakeSingleColumn(1), chartRange.TakeSingleColumn(0))

pieChart.Title.Text = "Extension Size"
'Set datalabels and remove the legend
pieChart.DataLabel.ShowCategory = True
pieChart.DataLabel.ShowPercent = True
pieChart.DataLabel.ShowLeaderLines = True
pieChart.Legend.Remove()
```

### Example 2 - add a Sunburst chart

From EPPlus 5.2, all chart types are supported, even newer types like Sunburst Charts and Region Maps.

Here's a piece of code from the sample project showing how to add a Sunburst Chart:

```csharp
var ws = package.Workbook.Worksheets.Add("Sunburst & Treemap Chart");
var range = await LoadSalesFromDatabase(connectionString, ws);

var sunburstChart = ws.Drawings.AddSunburstChart("SunburstChart1");
var r1 = range.SkipRows(1).SkipColumns(3);
var r2 = range.SkipRows(1).TakeColumns(3);
var sbSerie = sunburstChart.Series.Add(r1, r2);
sbSerie.HeaderAddress = ws.Cells["D1"];
sunburstChart.SetPosition(1, 0, 6, 0);
sunburstChart.SetSize(800, 800);
sunburstChart.Title.Text = "Sales";            
sunburstChart.Legend.Add();
sunburstChart.Legend.Position = eLegendPosition.Bottom;
sbSerie.DataLabel.Add(true, true);
sunburstChart.StyleManager.SetChartStyle(ePresetChartStyle.SunburstChartStyle3);
```

```vbnet
Dim ws = package.Workbook.Worksheets.Add("Sunburst & Treemap Chart")
Dim range = Await LoadSalesFromDatabase(connectionString, ws)

Dim sunburstChart = ws.Drawings.AddSunburstChart("SunburstChart1")
Dim r1 = range.SkipRows(1).SkipColumns(3)
Dim r2 = range.SkipRows(1).TakeColumns(3)
Dim sbSerie = sunburstChart.Series.Add(r1, r2)
sbSerie.HeaderAddress = ws.Cells("D1")
sunburstChart.SetPosition(1, 0, 6, 0)
sunburstChart.SetSize(800, 800)
sunburstChart.Title.Text = "Sales"
sunburstChart.Legend.Add()
sunburstChart.Legend.Position = eLegendPosition.Bottom
sbSerie.DataLabel.Add(True, True)
sunburstChart.StyleManager.SetChartStyle(ePresetChartStyle.SunburstChartStyle3)
```

### Reading existing charts

All charts on a worksheet is stored in the `Drawings` collection. The `As` properties on Drawing objects helps you to cast the Drawing to its specialized subclass.

```csharp
// As.Chart.Chart means that we cast all the charts to ExcelChart which is the common base class for all charts
var charts = worksheet.Drawings.Where(x => x.DrawingType == eDrawingType.Chart).Select(x => x.As.Chart.Chart);
foreach(var chart in charts)
{
    var name = chart.Name;
    var chartType = chart.ChartType;
    if(chartType == eChartType.PieExploded3D)
    {
        var myPieChart = chart.As.Chart.PieChart;
        var series = myPieChart.Series;
    }
    // etc
}
```

```vbnet
' As.Chart.Chart means that we cast all the charts to ExcelChart which is the common base class for all charts
Dim charts = worksheet.Drawings.Where(Function(x) x.DrawingType = eDrawingType.Chart).Select(Function(x) x.As.Chart.Chart)
For Each chart In charts
    Dim name = chart.Name
    Dim chartType = chart.ChartType
    If chartType = eChartType.PieExploded3D Then
        Dim myPieChart = chart.As.Chart.PieChart
        Dim series = myPieChart.Series
    End If
    ' etc
Next
```

### Shapes and pictures in charts.

As of EPPlus 8.1 you can use the drawings property on the chart to add, manipulate and remove shapes and pictures from charts. Charts are limited to shapes and pictures. They can be manipulated and grouped like regular drawings. However, you cannot add shapes and pictures to the newer extended charts released with Office 2016 (for example Sunburst or TreeMap charts), as they don't support this functionality.

```csharp
//Get chart
var chart = ws.Drawings[0] as ExcelChartStandard;
//Get shape inside chart and adjust size
var shape = chart.Drawings[0]
shape.SetSize(150);
//Add a shape to chart
var myShape = chart.Drawings.AddShape("MyShape", eShapeStyle.Diamond);
//Add a picture
var myPic = chart.Drawings.AddPicture("MyPic", "myPicture.jpg");
```

```vbnet
'Get chart
Dim chart = TryCast(ws.Drawings(0), ExcelChartStandard)
'Get shape inside chart and adjust size
Dim shape = chart.Drawings(0)
shape.SetSize(150)
'Add a shape to chart
Dim myShape = chart.Drawings.AddShape("MyShape", eShapeStyle.Diamond)
'Add a picture
Dim myPic = chart.Drawings.AddPicture("MyPic", "myPicture.jpg")
```

## See also

- [Styling charts](<https://epplussoftware.com/docs/charts-and-drawing-objects/charts/styling-charts/index.md>)
- [Using chart templates](<https://epplussoftware.com/docs/charts-and-drawing-objects/charts/using-chart-templates/index.md>)
- [Chart samples](<https://epplussoftware.com/docs/charts-and-drawing-objects/charts/chart-samples/index.md>)
- [List of supported charts](<https://epplussoftware.com/docs/charts-and-drawing-objects/charts/supported-charts/index.md>)
