Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/7f432b6fbf303a5ca9d1d259a2d12100 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/7f432b6fbf303a5ca9d1d259a2d12100 to your computer and use it in GitHub Desktop.
Create Tornado Charts in Excel using C#
// Create a new Excel Workbook
Workbook wb = new Workbook();
Worksheet worksheet = wb.Worksheets[0];
// Add sample values to cells
worksheet.Cells["A1"].PutValue("Products");
worksheet.Cells["A2"].PutValue("Product A");
worksheet.Cells["A3"].PutValue("Product B");
worksheet.Cells["A4"].PutValue("Product C");
worksheet.Cells["A5"].PutValue("Product D");
worksheet.Cells["A6"].PutValue("Product E");
worksheet.Cells["B1"].PutValue("2021-2022");
worksheet.Cells["B2"].PutValue(-100);
worksheet.Cells["B3"].PutValue(-80);
worksheet.Cells["B4"].PutValue(-75);
worksheet.Cells["B5"].PutValue(-60);
worksheet.Cells["B6"].PutValue(-48);
worksheet.Cells["C1"].PutValue("2023-2024");
worksheet.Cells["C2"].PutValue(95);
worksheet.Cells["C3"].PutValue(80);
worksheet.Cells["C4"].PutValue(72);
worksheet.Cells["C5"].PutValue(65);
worksheet.Cells["C6"].PutValue(45);
ChartCollection charts = worksheet.Charts;
// Add bar chart
int index = charts.Add(ChartType.BarStacked, 8, 1, 24, 8);
Chart chart = charts[index];
// Set data for bar chart
chart.SetChartDataRange("A1:C6", true);
// Set properties for bar chart
chart.Title.Text = "Tornado chart";
chart.Style = 2;
chart.PlotArea.Area.ForegroundColor = Color.White;
chart.PlotArea.Border.Color = Color.White;
chart.Legend.Position = LegendPositionType.Bottom;
chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.CategoryAxis.IsPlotOrderReversed = true;
chart.GapWidth = 50;
// Show data labels
DataLabels datalabels;
for (int i = 0; i < chart.NSeries.Count; i++)
{
datalabels = chart.NSeries[i].DataLabels;
//Set the position of DataLabels
datalabels.Position = LabelPositionType.Center;
//Show the value in the DataLabels
datalabels.ShowValue = true;
datalabels.Font.Color = Color.White;
datalabels.NumberFormat = "#,##0;#,##0";
}
Axis valueAxis = chart.ValueAxis;
valueAxis.TickLabels.NumberFormat = "#,##0;#,##0";
// Save the file
wb.Save("D:\\Files\\chart_out.xlsx");
// Load en existing Excel file with data
Workbook wb = new Workbook("D:\\Files\\sample.xlsx");
// Access the worksheet
Worksheet sheet = wb.Worksheets[0];
// Get charts
ChartCollection charts = sheet.Charts;
// Add bar chart
int index = charts.Add(ChartType.BarStacked, 8, 1, 24, 8);
Chart chart = charts[index];
// Set data for bar chart
chart.SetChartDataRange("A1:C7", true);
// Set properties for bar chart
chart.Title.Text = "Tornado chart";
chart.Style = 2;
chart.PlotArea.Area.ForegroundColor = Color.White;
chart.PlotArea.Border.Color = Color.White;
chart.Legend.Position = LegendPositionType.Bottom;
chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.CategoryAxis.IsPlotOrderReversed = true;
chart.GapWidth = 10;
Axis valueAxis = chart.ValueAxis;
valueAxis.TickLabels.NumberFormat = "#,##0;#,##0";
// Save the file
wb.Save("D:\\Files\\out.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment