|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | -using Aspose.Cells.Charts; |
6 | | -using Aspose.Cells.Rendering; |
7 | | - |
8 | | -namespace Aspose.Cells.Examples.CSharp.Articles.ManageChartsAndShapes |
9 | | -{ |
10 | | - public class CreatePieChartWithLeaderLines |
11 | | - { |
12 | | - public static void Run() |
13 | | - { |
14 | | - // ExStart:CreateWorkbook |
15 | | - // The path to the documents directory. |
16 | | - string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
17 | | - |
18 | | - // Create an instance of Workbook in XLSX format |
19 | | - Workbook workbook = new Workbook(FileFormatType.Xlsx); |
20 | | - |
21 | | - // Access the first worksheet |
22 | | - Worksheet worksheet = workbook.Worksheets[0]; |
23 | | - |
24 | | - // Add two columns of data |
25 | | - worksheet.Cells["A1"].PutValue("Retail"); |
26 | | - worksheet.Cells["A2"].PutValue("Services"); |
27 | | - worksheet.Cells["A3"].PutValue("Info & Communication"); |
28 | | - worksheet.Cells["A4"].PutValue("Transport Equip"); |
29 | | - worksheet.Cells["A5"].PutValue("Construction"); |
30 | | - worksheet.Cells["A6"].PutValue("Other Products"); |
31 | | - worksheet.Cells["A7"].PutValue("Wholesale"); |
32 | | - worksheet.Cells["A8"].PutValue("Land Transport"); |
33 | | - worksheet.Cells["A9"].PutValue("Air Transport"); |
34 | | - worksheet.Cells["A10"].PutValue("Electric Appliances"); |
35 | | - worksheet.Cells["A11"].PutValue("Securities"); |
36 | | - worksheet.Cells["A12"].PutValue("Textiles & Apparel"); |
37 | | - worksheet.Cells["A13"].PutValue("Machinery"); |
38 | | - worksheet.Cells["A14"].PutValue("Metal Products"); |
39 | | - worksheet.Cells["A15"].PutValue("Cash"); |
40 | | - worksheet.Cells["A16"].PutValue("Banks"); |
41 | | - |
42 | | - worksheet.Cells["B1"].PutValue(10.4); |
43 | | - worksheet.Cells["B2"].PutValue(5.2); |
44 | | - worksheet.Cells["B3"].PutValue(6.4); |
45 | | - worksheet.Cells["B4"].PutValue(10.4); |
46 | | - worksheet.Cells["B5"].PutValue(7.9); |
47 | | - worksheet.Cells["B6"].PutValue(4.1); |
48 | | - worksheet.Cells["B7"].PutValue(3.5); |
49 | | - worksheet.Cells["B8"].PutValue(5.7); |
50 | | - worksheet.Cells["B9"].PutValue(3); |
51 | | - worksheet.Cells["B10"].PutValue(14.7); |
52 | | - worksheet.Cells["B11"].PutValue(3.6); |
53 | | - worksheet.Cells["B12"].PutValue(2.8); |
54 | | - worksheet.Cells["B13"].PutValue(7.8); |
55 | | - worksheet.Cells["B14"].PutValue(2.4); |
56 | | - worksheet.Cells["B15"].PutValue(1.8); |
57 | | - worksheet.Cells["B16"].PutValue(10.1); |
58 | | - |
59 | | - // Create a pie chart and add it to the collection of charts |
60 | | - int id = worksheet.Charts.Add(ChartType.Pie, 3, 3, 23, 13); |
61 | | - |
62 | | - // Access newly created Chart instance |
63 | | - Chart chart = worksheet.Charts[id]; |
64 | | - |
65 | | - // Set series data range |
66 | | - chart.NSeries.Add("B1:B16", true); |
67 | | - |
68 | | - // Set category data range |
69 | | - chart.NSeries.CategoryData = "A1:A16"; |
70 | | - |
71 | | - // Turn off legend |
72 | | - chart.ShowLegend = false; |
73 | | - |
74 | | - // Access data labels |
75 | | - DataLabels dataLabels = chart.NSeries[0].DataLabels; |
76 | | - |
77 | | - // Turn on category names |
78 | | - dataLabels.ShowCategoryName = true; |
79 | | - |
80 | | - // Turn on percentage format |
81 | | - dataLabels.ShowPercentage = true; |
82 | | - |
83 | | - // Set position |
84 | | - dataLabels.Position = LabelPositionType.OutsideEnd; |
85 | | - |
86 | | - // Set separator |
87 | | - dataLabels.Separator = DataLablesSeparatorType.Comma; |
88 | | - // ExEnd:CreateWorkbook |
89 | | - |
90 | | - // ExStart:TurnOnLeaderLines |
91 | | - // Turn on leader lines |
92 | | - chart.NSeries[0].HasLeaderLines = true; |
93 | | - |
94 | | - // Calculete chart |
95 | | - chart.Calculate(); |
96 | | - |
97 | | - // You need to move DataLabels a little leftward or rightward depending on their position to show leader lines |
98 | | - int DELTA = 100; |
99 | | - for (int i = 0; i < chart.NSeries[0].Points.Count; i++) |
100 | | - { |
101 | | - int X = chart.NSeries[0].Points[i].DataLabels.X; |
102 | | - // If it is greater than 2000, then move the X position a little right otherwise move the X position a little left |
103 | | - if (X > 2000) |
104 | | - chart.NSeries[0].Points[i].DataLabels.X = X + DELTA; |
105 | | - else |
106 | | - chart.NSeries[0].Points[i].DataLabels.X = X - DELTA; |
107 | | - } |
108 | | - // ExEnd:TurnOnLeaderLines |
109 | | - |
110 | | - // ExStart:SaveChartInImageAndWorkbookInXLSX |
111 | | - // In order to save the chart image, create an instance of ImageOrPrintOptions |
112 | | - ImageOrPrintOptions anOption = new ImageOrPrintOptions(); |
113 | | - |
114 | | - // Set image format |
115 | | - anOption.ImageType = Drawing.ImageType.Png; |
116 | | - |
117 | | - // Set resolution |
118 | | - anOption.HorizontalResolution = 200; |
119 | | - anOption.VerticalResolution = 200; |
120 | | - |
121 | | - // Render chart to image |
122 | | - chart.ToImage(dataDir + "output_out.png", anOption); |
123 | | - |
124 | | - // Save the workbook to see chart inside the Excel |
125 | | - workbook.Save(dataDir + "output_out.xlsx"); |
126 | | - // ExEnd:SaveChartInImageAndWorkbookInXLSX |
127 | | - } |
128 | | - } |
129 | | -} |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Aspose.Cells.Charts; |
| 6 | +using Aspose.Cells.Rendering; |
| 7 | + |
| 8 | +namespace Aspose.Cells.Examples.CSharp.Articles.ManageChartsAndShapes |
| 9 | +{ |
| 10 | + public class CreatePieChartWithLeaderLines |
| 11 | + { |
| 12 | + public static void Run() |
| 13 | + { |
| 14 | + // ExStart:CreateWorkbook |
| 15 | + // The path to the documents directory. |
| 16 | + string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
| 17 | + |
| 18 | + // Create an instance of Workbook in XLSX format |
| 19 | + Workbook workbook = new Workbook(FileFormatType.Xlsx); |
| 20 | + |
| 21 | + // Access the first worksheet |
| 22 | + Worksheet worksheet = workbook.Worksheets[0]; |
| 23 | + |
| 24 | + // Add two columns of data |
| 25 | + worksheet.Cells["A1"].PutValue("Retail"); |
| 26 | + worksheet.Cells["A2"].PutValue("Services"); |
| 27 | + worksheet.Cells["A3"].PutValue("Info & Communication"); |
| 28 | + worksheet.Cells["A4"].PutValue("Transport Equip"); |
| 29 | + worksheet.Cells["A5"].PutValue("Construction"); |
| 30 | + worksheet.Cells["A6"].PutValue("Other Products"); |
| 31 | + worksheet.Cells["A7"].PutValue("Wholesale"); |
| 32 | + worksheet.Cells["A8"].PutValue("Land Transport"); |
| 33 | + worksheet.Cells["A9"].PutValue("Air Transport"); |
| 34 | + worksheet.Cells["A10"].PutValue("Electric Appliances"); |
| 35 | + worksheet.Cells["A11"].PutValue("Securities"); |
| 36 | + worksheet.Cells["A12"].PutValue("Textiles & Apparel"); |
| 37 | + worksheet.Cells["A13"].PutValue("Machinery"); |
| 38 | + worksheet.Cells["A14"].PutValue("Metal Products"); |
| 39 | + worksheet.Cells["A15"].PutValue("Cash"); |
| 40 | + worksheet.Cells["A16"].PutValue("Banks"); |
| 41 | + |
| 42 | + worksheet.Cells["B1"].PutValue(10.4); |
| 43 | + worksheet.Cells["B2"].PutValue(5.2); |
| 44 | + worksheet.Cells["B3"].PutValue(6.4); |
| 45 | + worksheet.Cells["B4"].PutValue(10.4); |
| 46 | + worksheet.Cells["B5"].PutValue(7.9); |
| 47 | + worksheet.Cells["B6"].PutValue(4.1); |
| 48 | + worksheet.Cells["B7"].PutValue(3.5); |
| 49 | + worksheet.Cells["B8"].PutValue(5.7); |
| 50 | + worksheet.Cells["B9"].PutValue(3); |
| 51 | + worksheet.Cells["B10"].PutValue(14.7); |
| 52 | + worksheet.Cells["B11"].PutValue(3.6); |
| 53 | + worksheet.Cells["B12"].PutValue(2.8); |
| 54 | + worksheet.Cells["B13"].PutValue(7.8); |
| 55 | + worksheet.Cells["B14"].PutValue(2.4); |
| 56 | + worksheet.Cells["B15"].PutValue(1.8); |
| 57 | + worksheet.Cells["B16"].PutValue(10.1); |
| 58 | + |
| 59 | + // Create a pie chart and add it to the collection of charts |
| 60 | + int id = worksheet.Charts.Add(ChartType.Pie, 3, 3, 23, 13); |
| 61 | + |
| 62 | + // Access newly created Chart instance |
| 63 | + Chart chart = worksheet.Charts[id]; |
| 64 | + |
| 65 | + // Set series data range |
| 66 | + chart.NSeries.Add("B1:B16", true); |
| 67 | + |
| 68 | + // Set category data range |
| 69 | + chart.NSeries.CategoryData = "A1:A16"; |
| 70 | + |
| 71 | + // Turn off legend |
| 72 | + chart.ShowLegend = false; |
| 73 | + |
| 74 | + // Access data labels |
| 75 | + DataLabels dataLabels = chart.NSeries[0].DataLabels; |
| 76 | + |
| 77 | + // Turn on category names |
| 78 | + dataLabels.ShowCategoryName = true; |
| 79 | + |
| 80 | + // Turn on percentage format |
| 81 | + dataLabels.ShowPercentage = true; |
| 82 | + |
| 83 | + // Set position |
| 84 | + dataLabels.Position = LabelPositionType.OutsideEnd; |
| 85 | + |
| 86 | + // Set separator |
| 87 | + dataLabels.SeparatorType = DataLabelsSeparatorType.Comma; |
| 88 | + // ExEnd:CreateWorkbook |
| 89 | + |
| 90 | + // ExStart:TurnOnLeaderLines |
| 91 | + // Turn on leader lines |
| 92 | + chart.NSeries[0].HasLeaderLines = true; |
| 93 | + |
| 94 | + // Calculete chart |
| 95 | + chart.Calculate(); |
| 96 | + |
| 97 | + // You need to move DataLabels a little leftward or rightward depending on their position to show leader lines |
| 98 | + int DELTA = 100; |
| 99 | + for (int i = 0; i < chart.NSeries[0].Points.Count; i++) |
| 100 | + { |
| 101 | + int X = chart.NSeries[0].Points[i].DataLabels.X; |
| 102 | + // If it is greater than 2000, then move the X position a little right otherwise move the X position a little left |
| 103 | + if (X > 2000) |
| 104 | + chart.NSeries[0].Points[i].DataLabels.X = X + DELTA; |
| 105 | + else |
| 106 | + chart.NSeries[0].Points[i].DataLabels.X = X - DELTA; |
| 107 | + } |
| 108 | + // ExEnd:TurnOnLeaderLines |
| 109 | + |
| 110 | + // ExStart:SaveChartInImageAndWorkbookInXLSX |
| 111 | + // In order to save the chart image, create an instance of ImageOrPrintOptions |
| 112 | + ImageOrPrintOptions anOption = new ImageOrPrintOptions(); |
| 113 | + |
| 114 | + // Set image format |
| 115 | + anOption.ImageType = Drawing.ImageType.Png; |
| 116 | + |
| 117 | + // Set resolution |
| 118 | + anOption.HorizontalResolution = 200; |
| 119 | + anOption.VerticalResolution = 200; |
| 120 | + |
| 121 | + // Render chart to image |
| 122 | + chart.ToImage(dataDir + "output_out.png", anOption); |
| 123 | + |
| 124 | + // Save the workbook to see chart inside the Excel |
| 125 | + workbook.Save(dataDir + "output_out.xlsx"); |
| 126 | + // ExEnd:SaveChartInImageAndWorkbookInXLSX |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments