
- Forum
- Programming Talk
- Microsoft .NET
- How to Create Excel Pie Charts for C#
How to Create Excel Pie Charts for C#
This is a discussion on How to Create Excel Pie Charts for C# within the Microsoft .NET forums, part of the Programming Talk category; Why We Create Excel Pie Charts? Create excel pie charts is a simple method to display your data to other ...
-
01-27-2011, 12:53 AM #1ghost999 Guest
How to Create Excel Pie Charts for C#
Why We Create Excel Pie Charts?
Create excel pie charts is a simple method to display your data to other individuals or a group. Excel pie charts can easily relay your messages that may otherwise go unnoticed by your audience. Pie charts are helpful for international audiences because they are universally known and easily explained. And it's extremely easy to create and use in Excel.
Pie charts, unlike other charts in Microsoft Excel, require the data in your worksheet be contained in only one row or column. By indicating a "category", an additional row or column can be used. Relative sizes are easy to compare as each piece of data is represented as a portion of a whole.
How to Create Excel Pie Charts in Microsoft Excel?
Every one of you can create excel pie charts in no time by using Microsoft Excel through the following steps:
* Launch Microsoft Office Excel and open your excel file which with the data that you want to base your pie chart on.
* Select the data that you want to base your chart on. The last cell that you want to select will not be chosen bu it will have bold outline borders around it.
* Click "Insert" button to open Chart Wizard window. Click on "Pie" in the right side column of Chart Type. Several sub-types of pie charts will be offered for you to choose. Pick a proper type which can best match with your data and click "Next"
* Prevew on this new interface. If the pie chart with wrong information or even no pie chart appear, click "Cancel" and select your data, try the above process again. If everything is OK, click "Next"
* On the new window, enter labels and a title for your pie chart. Click "Finish" when you see the pie chart appears as you want.
* On your spreadsheet, click the box and drag your mouse to manipulate the size of the chart. By right clicking the chart and box, you can get many options to edit the pie chart.
How to Use Spire.XLS for .NET to Creat Excel Pie Charts?
Spire.XLS presents you an easy way to create a pie chart in the Excel workbook. First, you should create a pie chart with the sheet.Charts.Add method. You may control the resource of the data and title of the chart by setting DataRange and ChartTitle properties. What's more, we create a object to operate more about the chart. You may set the label and value of the pie with the properties cs.CategoryLabels and cs.Values. If you want to hide the value of the pie, you may set the cs.DataPoints.DefaultDataPoint.DataLabels.HasValue property false. In this demo, in order to reflect the effect of the chart, we set the grid lines of the worksheet invisible by assigning the sheet.GridLinesVisible false.
[C#]
view sourceprint?
01 using Spire.Xls;
02 using System.Drawing;
03
04 namespace Saveas
05 {
06 class Program
07 {
08 static void Main(string[] args)
09 {
10 //Create a new workbook
11 Workbook workbook = new Workbook();
12
13 //Initialize worksheet
14 workbook.CreateEmptySheets(1);
15 Worksheet sheet = workbook.Worksheets[0];
16
17 //Set sheet name
18 sheet.Name = "Chart data";
19
20 //Set the grid lines invisible
21 sheet.GridLinesVisible = false;
22
23 //Create a chart
24 Chart chart = sheet.Charts.Add(ExcelChartType.Pie3D);
25
26 //Set region of chart data
27 chart.DataRange = sheet.Range["B2:B5"];
28 chart.SeriesDataFromRange = false;
29
30 //Set position of chart
31 chart.LeftColumn = 1;
32 chart.TopRow = 6;
33 chart.RightColumn = 9;
34 chart.BottomRow = 25;
35
36 //Chart title
37 chart.ChartTitle = "Sales by year";
38 chart.ChartTitleArea.IsBold = true;
39 chart.ChartTitleArea.Size = 12;
40
41 //Initialize the chart series
42 Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
43
44 //Chart Labels resource
45 cs.CategoryLabels = sheet.Range["A2:A5"];
46
47 //Chart value resource
48 cs.Values = sheet.Range["B2:B5"];
49
50 //Set the value visible in the chart
51 cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
52
53 //Year
54 sheet.Range["A1"].Value = "Year";
55 sheet.Range["A2"].Value = "2002";
56 sheet.Range["A3"].Value = "2003";
57 sheet.Range["A4"].Value = "2004";
58 sheet.Range["A5"].Value = "2005";
59
60 //Sales
61 sheet.Range["B1"].Value = "Sales";
62 sheet.Range["B2"].NumberValue = 4000;
63 sheet.Range["B3"].NumberValue = 6000;
64 sheet.Range["B4"].NumberValue = 7000;
65 sheet.Range["B5"].NumberValue = 8500;
66
67 //Style
68 sheet.Range["A1:B1"].Style.Font.IsBold = true;
69 sheet.Range["A2:B2"].Style.KnownColor = ExcelColors.LightYellow;
70 sheet.Range["A3:B3"].Style.KnownColor = ExcelColors.LightGreen1;
71 sheet.Range["A4:B4"].Style.KnownColor = ExcelColors.LightOrange;
72 sheet.Range["A5:B5"].Style.KnownColor = ExcelColors.LightTurquoise;
73
74 //Border
75 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
76 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
77 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
78 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
79 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
80 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
81 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
82 sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
83
84 //Number format
85 sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
86 chart.PlotArea.Fill.Visible = false;
87
88 //Save the file
89 workbook.SaveToFile("Sample.xls");
90
91 //Launch the file
92 System.Diagnostics.Process.Start("Sample.xls");
93 }
94 }
95 }

Reply With Quote





