Skip to content

Commit 7544b8f

Browse files
committed
Added code for treemap
1 parent 72cd24a commit 7544b8f

File tree

1 file changed

+50
-1
lines changed
  • Charts/Create-treemap-chart/.NET/Create-treemap-chart

1 file changed

+50
-1
lines changed

Charts/Create-treemap-chart/.NET/Create-treemap-chart/Program.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,56 @@ static void Main(string[] args)
1212
//Create a new Word document.
1313
using (WordDocument document = new WordDocument())
1414
{
15-
15+
// Add a section & a paragraph to the document.
16+
IWSection section = document.AddSection();
17+
IWParagraph paragraph = section.AddParagraph();
18+
19+
// Create and append TreeMap chart to the paragraph.
20+
WChart chart = paragraph.AppendChart(446, 270);
21+
chart.ChartType = OfficeChartType.TreeMap;
22+
23+
// Set chart title.
24+
chart.ChartTitle = "Food Sales - Treemap Chart";
25+
26+
// Set headers.
27+
chart.ChartData.SetValue(1, 1, "Meal");
28+
chart.ChartData.SetValue(1, 2, "Category");
29+
chart.ChartData.SetValue(1, 3, "Item");
30+
chart.ChartData.SetValue(1, 4, "Sales");
31+
32+
// Add data rows.
33+
string[,] data = {
34+
{"Breakfast", "Beverage", "coffee", "20"},
35+
{"Breakfast", "Beverage", "tea", "9"},
36+
{"Breakfast", "Food", "waffles", "12"},
37+
{"Breakfast", "Food", "pancakes", "35"},
38+
{"Breakfast", "Food", "eggs", "24"},
39+
{"Lunch", "Beverage", "coffee", "10"},
40+
{"Lunch", "Beverage", "iced tea", "45"},
41+
{"Lunch", "Food", "soup", "16"},
42+
{"Lunch", "Food", "sandwich", "36"},
43+
{"Lunch", "Food", "salad", "70"},
44+
{"Lunch", "Food", "pie", "45"},
45+
{"Lunch", "Food", "cookie", "25"}
46+
};
47+
48+
for (int i = 0; i < data.GetLength(0); i++)
49+
{
50+
chart.ChartData.SetValue(i + 2, 1, data[i, 0]);
51+
chart.ChartData.SetValue(i + 2, 2, data[i, 1]);
52+
chart.ChartData.SetValue(i + 2, 3, data[i, 2]);
53+
chart.ChartData.SetValue(i + 2, 4, int.Parse(data[i, 3]));
54+
}
55+
56+
chart.DataRange = chart.ChartData[2, 1, 13, 4];
57+
// Set DataLabels.
58+
IOfficeChartSerie serie = chart.Series[0];
59+
serie.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
60+
61+
// Set legend.
62+
chart.HasLegend = true;
63+
chart.Legend.Position = OfficeLegendPosition.Bottom;
64+
1665
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
1766
{
1867
//Save the Word document to the file stream.

0 commit comments

Comments
 (0)