|
| 1 | +--- |
| 2 | +title: How to set the color for the dynamic series in sunburst chart| Syncfusion |
| 3 | +description: Learn how to apply custom colors to each data point in a Sunburst chart dynamically with the Syncfusion .NET Excel library (XlsIO) using C# and VB.NET. |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to set colors for dynamic series in a Sunburst chart? |
| 10 | + |
| 11 | +The examples below show how to assign custom colors to each data point in a Sunburst chart with Syncfusion XlsIO. Code is provided for C# (cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" |
| 15 | +{% endhighlight %} |
| 16 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 17 | +{ |
| 18 | + IApplication application = excelEngine.Excel; |
| 19 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 20 | + IWorkbook workbook = application.Workbooks.Open("../../../Data/Input.xlsx"); |
| 21 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 22 | + |
| 23 | + //Initialize chart |
| 24 | + IChartShape chart = worksheet.Charts.Add(); |
| 25 | + chart.ChartType = ExcelChartType.SunBurst; |
| 26 | + |
| 27 | + //Assign data |
| 28 | + chart.DataRange = worksheet["A1:B13"]; |
| 29 | + chart.IsSeriesInRows = false; |
| 30 | + |
| 31 | + //Apply chart elements |
| 32 | + //Set Chart Title |
| 33 | + chart.ChartTitle = "Transfer Summary"; |
| 34 | + |
| 35 | + //Set Datalabels |
| 36 | + IChartSerie serie1 = chart.Series[0]; |
| 37 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsCategoryName = false; |
| 38 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 39 | + |
| 40 | + Color[] seriecolor = new Color[12]; |
| 41 | + seriecolor[0] = Color.FromArgb(253, 182, 33); |
| 42 | + seriecolor[1] = Color.FromArgb(18, 151, 243); |
| 43 | + seriecolor[2] = Color.FromArgb(38, 7, 142); |
| 44 | + seriecolor[3] = Color.FromArgb(5, 60, 122); |
| 45 | + seriecolor[4] = Color.FromArgb(180, 70, 243); |
| 46 | + seriecolor[5] = Color.FromArgb(53, 12, 133); |
| 47 | + seriecolor[6] = Color.FromArgb(108, 11, 23); |
| 48 | + seriecolor[7] = Color.FromArgb(200, 70, 112); |
| 49 | + seriecolor[8] = Color.FromArgb(125, 200, 12); |
| 50 | + seriecolor[9] = Color.FromArgb(10, 150, 43); |
| 51 | + seriecolor[10] = Color.FromArgb(150, 82, 133); |
| 52 | + seriecolor[11] = Color.FromArgb(98, 15, 103); |
| 53 | + |
| 54 | + for (int i = 0; i < (serie1 as ChartSerieImpl).PointNumber; i++) |
| 55 | + { |
| 56 | + serie1.DataPoints[i].DataFormat.AreaProperties.ForegroundColor = seriecolor[i]; |
| 57 | + } |
| 58 | + |
| 59 | + //Set Legend |
| 60 | + chart.HasLegend = true; |
| 61 | + |
| 62 | + //Positioning the chart in the worksheet |
| 63 | + chart.TopRow = 8; |
| 64 | + chart.LeftColumn = 1; |
| 65 | + chart.BottomRow = 23; |
| 66 | + chart.RightColumn = 8; |
| 67 | + |
| 68 | + //Saving the workbook |
| 69 | + workbook.SaveAs("../../../Output/SunBurst.xlsx"); |
| 70 | +} |
| 71 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 72 | + using (ExcelEngine excelEngine = new ExcelEngine()) |
| 73 | + { |
| 74 | + IApplication application = excelEngine.Excel; |
| 75 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 76 | + IWorkbook workbook = application.Workbooks.Open("../../Data/Input.xlsx"); |
| 77 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 78 | + |
| 79 | + //Initialize chart |
| 80 | + IChartShape chart = worksheet.Charts.Add(); |
| 81 | + chart.ChartType = ExcelChartType.SunBurst; |
| 82 | + |
| 83 | + //Assign data |
| 84 | + chart.DataRange = worksheet["A1:B13"]; |
| 85 | + chart.IsSeriesInRows = false; |
| 86 | + |
| 87 | + //Apply chart elements |
| 88 | + //Set Chart Title |
| 89 | + chart.ChartTitle = "Transfer Summary"; |
| 90 | + |
| 91 | + //Set Datalabels |
| 92 | + IChartSerie serie1 = chart.Series[0]; |
| 93 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsCategoryName = false; |
| 94 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; |
| 95 | + |
| 96 | + Color[] seriecolor = new Color[12]; |
| 97 | + seriecolor[0] = Color.FromArgb(253, 182, 33); |
| 98 | + seriecolor[1] = Color.FromArgb(18, 151, 243); |
| 99 | + seriecolor[2] = Color.FromArgb(38, 7, 142); |
| 100 | + seriecolor[3] = Color.FromArgb(5, 60, 122); |
| 101 | + seriecolor[4] = Color.FromArgb(180, 70, 243); |
| 102 | + seriecolor[5] = Color.FromArgb(53, 12, 133); |
| 103 | + seriecolor[6] = Color.FromArgb(108, 11, 23); |
| 104 | + seriecolor[7] = Color.FromArgb(200, 70, 112); |
| 105 | + seriecolor[8] = Color.FromArgb(125, 200, 12); |
| 106 | + seriecolor[9] = Color.FromArgb(10, 150, 43); |
| 107 | + seriecolor[10] = Color.FromArgb(150, 82, 133); |
| 108 | + seriecolor[11] = Color.FromArgb(98, 15, 103); |
| 109 | + |
| 110 | + for (int i = 0; i < (serie1 as ChartSerieImpl).PointNumber; i++) |
| 111 | + { |
| 112 | + serie1.DataPoints[i].DataFormat.AreaProperties.ForegroundColor = seriecolor[i]; |
| 113 | + } |
| 114 | + |
| 115 | + //Set Legend |
| 116 | + chart.HasLegend = true; |
| 117 | + |
| 118 | + //Positioning the chart in the worksheet |
| 119 | + chart.TopRow = 8; |
| 120 | + chart.LeftColumn = 1; |
| 121 | + chart.BottomRow = 23; |
| 122 | + chart.RightColumn = 8; |
| 123 | + |
| 124 | + //Saving and closing the workbook |
| 125 | + workbook.SaveAs("../../Output/SunBurst.xlsx"); |
| 126 | + } |
| 127 | +{% endhighlight %} |
| 128 | + |
| 129 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 130 | +Using excelEngine As New ExcelEngine() |
| 131 | + |
| 132 | + Dim application As IApplication = excelEngine.Excel |
| 133 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 134 | + |
| 135 | + 'Open existing workbook with data |
| 136 | + Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/Input.xlsx") |
| 137 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 138 | + |
| 139 | + 'Create and configure Sunburst chart |
| 140 | + Dim chart As IChartShape = worksheet.Charts.Add() |
| 141 | + chart.ChartType = ExcelChartType.SunBurst |
| 142 | + |
| 143 | + 'Assign data |
| 144 | + chart.DataRange = worksheet("A1:B13") |
| 145 | + chart.IsSeriesInRows = False |
| 146 | + |
| 147 | + 'Chart Title |
| 148 | + chart.ChartTitle = "Transfer Summary" |
| 149 | + |
| 150 | + 'Data labels (show value only) |
| 151 | + Dim serie1 As IChartSerie = chart.Series(0) |
| 152 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsCategoryName = False |
| 153 | + serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = True |
| 154 | + |
| 155 | + 'Custom colors for each data point |
| 156 | + Dim serieColor(11) As Color |
| 157 | + serieColor(0) = Color.FromArgb(253, 182, 33) |
| 158 | + serieColor(1) = Color.FromArgb(18, 151, 243) |
| 159 | + serieColor(2) = Color.FromArgb(38, 7, 142) |
| 160 | + serieColor(3) = Color.FromArgb(5, 60, 122) |
| 161 | + serieColor(4) = Color.FromArgb(180, 70, 243) |
| 162 | + serieColor(5) = Color.FromArgb(53, 12, 133) |
| 163 | + serieColor(6) = Color.FromArgb(108, 11, 23) |
| 164 | + serieColor(7) = Color.FromArgb(200, 70, 112) |
| 165 | + serieColor(8) = Color.FromArgb(125, 200, 12) |
| 166 | + serieColor(9) = Color.FromArgb(10, 150, 43) |
| 167 | + serieColor(10) = Color.FromArgb(150, 82, 133) |
| 168 | + serieColor(11) = Color.FromArgb(98, 15, 103) |
| 169 | + |
| 170 | + For i As Integer = 0 To CType(serie1, ChartSerieImpl).PointNumber - 1 |
| 171 | + serie1.DataPoints(i).DataFormat.AreaProperties.ForegroundColor = serieColor(i) |
| 172 | + Next |
| 173 | + |
| 174 | + 'Legend |
| 175 | + chart.HasLegend = True |
| 176 | + |
| 177 | + 'Position the chart in the worksheet |
| 178 | + chart.TopRow = 8 |
| 179 | + chart.LeftColumn = 1 |
| 180 | + chart.BottomRow = 23 |
| 181 | + chart.RightColumn = 8 |
| 182 | + |
| 183 | + 'Save and close |
| 184 | + workbook.SaveAs("../../Output/SunBurst.xlsx") |
| 185 | + |
| 186 | +End Using |
| 187 | +{% endhighlight %} |
| 188 | +{% endtabs %} |
0 commit comments