-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChartHelper.cs
146 lines (121 loc) · 5.4 KB
/
ChartHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using iDiTect.Excel.Drawing.Chart;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iDiTect.Excel.Demo
{
public static class ChartHelper
{
public static void AddChart()
{
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("ChartSheet");
// Inset chart data
worksheet.Cells["A1"].Value = "Name";
worksheet.Cells["A2"].Value = "Orange";
worksheet.Cells["A3"].Value = "Apple";
worksheet.Cells["A4"].Value = "Banana";
worksheet.Cells["B1"].Value = "Price";
worksheet.Cells["B2"].Value = 12;
worksheet.Cells["B3"].Value = 16;
worksheet.Cells["B4"].Value = 9;
worksheet.Cells["C1"].Value = "Quantity";
worksheet.Cells["C2"].Value = 50;
worksheet.Cells["C3"].Value = 64;
worksheet.Cells["C4"].Value = 72;
// Set x-series and y-series data from range of cells
Range xSeries = worksheet.Cells["A2:A4"];
Range ySeries1 = worksheet.Cells["B2:B4"];
Range ySeries2 = worksheet.Cells["C2:C4"];
// Create a Column graph
var colChart = worksheet.Drawings.AddChart("chart1", ChartType.ColumnClustered);
// Set chart location and size
colChart.SetPosition(0, 200);
colChart.SetSize(400, 300);
//Add series
var series1 = colChart.Series.Add(xSeries, ySeries1);
series1.Header = "Price";
var series2 = colChart.Series.Add(xSeries, ySeries2);
series2.Header = "Quantity";
// Set chart title
colChart.Title.Text = "Sample Column Chart";
//Set chart style
colChart.Style = ChartStyle.Style1;
//Add a Line chart
var lineChart = worksheet.Drawings.AddChart("chart2", ChartType.Line);
// Set chart location and size
lineChart.SetPosition(0, 500);
lineChart.SetSize(400, 300);
//Add series
var series3 = lineChart.Series.Add(xSeries, ySeries1);
series3.Header = "Price";
//Set the max and min value for the Y axis
lineChart.YAxis.MaxValue = 18;
lineChart.YAxis.MinValue = 8;
//Set chart style manually
lineChart.Fill.Color = System.Drawing.Color.DarkCyan;
lineChart.Border.Width = 3;
lineChart.Border.Fill.Color = System.Drawing.Color.Black;
lineChart.PlotArea.Fill.Color = System.Drawing.Color.Yellow;
series3.Fill.Color = System.Drawing.Color.Blue;
series3.Border.Width = 2;
series3.Border.Fill.Color = System.Drawing.Color.Green;
//Add a 3D Pie chart
var pieChart = worksheet.Drawings.AddChart("chart3", ChartType.Pie3D);
// Set chart location and size
pieChart.SetPosition(0, 800);
pieChart.SetSize(400, 300);
//Add series
var series4 = pieChart.Series.Add(xSeries, ySeries1);
//3d Settings
pieChart.View3D.RotX = 45;
pieChart.View3D.Perspective = 10;
excelApp.Save("chart.xlsx");
}
public static void AddCombinationChart()
{
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("ChartSheet");
// Inset chart data
worksheet.Cells["A1"].Value = "Name";
worksheet.Cells["A2"].Value = "Orange";
worksheet.Cells["A3"].Value = "Apple";
worksheet.Cells["A4"].Value = "Banana";
worksheet.Cells["B1"].Value = "Price";
worksheet.Cells["B2"].Value = 12;
worksheet.Cells["B3"].Value = 16;
worksheet.Cells["B4"].Value = 9;
worksheet.Cells["C1"].Value = "Quantity";
worksheet.Cells["C2"].Value = 50;
worksheet.Cells["C3"].Value = 64;
worksheet.Cells["C4"].Value = 72;
// Set x-series and y-series data from range of cells
Range xSeries = worksheet.Cells["A2:A4"];
Range ySeries1 = worksheet.Cells["B2:B4"];
Range ySeries2 = worksheet.Cells["C2:C4"];
//Add a chart with two charttypes (Column and Line) and a secondary axis
var chart = worksheet.Drawings.AddChart("multi-type chart", ChartType.ColumnClustered);
// Set chart location and size
chart.SetPosition(300, 0);
chart.SetSize(400, 300);
//Add series
var series1 = chart.Series.Add(xSeries, ySeries1);
series1.Header = "Price";
//Add a Line series
var lineType = chart.PlotArea.ChartTypes.Add(ChartType.Line);
//By default the secondary XAxis is not visible
lineType.UseSecondaryAxis = true;
lineType.XAxis.Deleted = false;
lineType.XAxis.TickLabelPosition = TickLabelPosition.High;
var series2 = lineType.Series.Add(xSeries, ySeries2);
series2.Header = "Quantity";
// Set chart title
chart.Title.Text = "Multi-type Chart";
//Set chart style
chart.Style = ChartStyle.Style1;
excelApp.Save("multi-type-chart.xlsx");
}
}
}