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