1- using EPPlus . Graphics ;
1+ using EPPlus . Fonts . OpenType . Utils ;
2+ using EPPlus . Graphics ;
23using EPPlusImageRenderer ;
34using EPPlusImageRenderer . RenderItems ;
45using EPPlusImageRenderer . Svg ;
5- using OfficeOpenXml . Drawing ;
66using OfficeOpenXml . Drawing . Chart ;
7- using OfficeOpenXml . FormulaParsing . Excel . Functions . Text ;
7+ using OfficeOpenXml . FormulaParsing . Excel . Functions . MathFunctions ;
88using OfficeOpenXml . Utils . TypeConversion ;
99using System ;
1010using System . Collections . Generic ;
11- using System . Linq ;
12- using System . Text ;
11+ using System . Drawing ;
1312
1413namespace EPPlus . Export . ImageRenderer . Svg . Chart . ChartTypeDrawers
1514{
@@ -32,13 +31,8 @@ public PieChartTypeDrawer(SvgChart chart, ExcelPieChart chartType) : base(chart,
3231
3332 foreach ( ExcelPieChartSerie serie in chartType . Series )
3433 {
35- List < object > valValue , catValue ;
36-
37- valValue = LoadSeriesValues ( serie . Series , serie . NumberLiteralsY , serie . StringLiteralsY ) ;
38- catValue = LoadSeriesValues ( serie . XSeries , serie . NumberLiteralsX , serie . StringLiteralsX ) ;
39-
40- catValues . Add ( catValue ) ;
41- valValues . Add ( valValue ) ;
34+ valValues = LoadSeriesValues ( serie . Series , serie . NumberLiteralsY , serie . StringLiteralsY ) ;
35+ catValues = LoadSeriesValues ( serie . XSeries , serie . NumberLiteralsX , serie . StringLiteralsX ) ;
4236
4337 serCounter ++ ;
4438 }
@@ -48,46 +42,106 @@ public PieChartTypeDrawer(SvgChart chart, ExcelPieChart chartType) : base(chart,
4842
4943 for ( int i = 0 ; i < valValues . Count ; i ++ )
5044 {
51- valValuesDoubles . Add ( ConvertUtil . GetValueDouble ( valValues [ i ] , false , true ) ) ;
45+ var origValue = valValues [ i ] ;
46+ var dValue = ConvertUtil . GetValueDouble ( origValue , false , true ) ;
47+ valValuesDoubles . Add ( dValue ) ;
5248 valTotal += valValuesDoubles [ i ] ;
5349 }
5450
5551 var cx = _svgChart . Plotarea . Rectangle . Bounds . Width / 2 ;
5652 var cy = _svgChart . Plotarea . Rectangle . Bounds . Height / 2 ;
5753
58- var radius = Math . Min ( _svgChart . Plotarea . Rectangle . Bounds . Height , _svgChart . Plotarea . Rectangle . Bounds . Width ) ;
54+ var radius = Math . Min ( cx , cy ) ;
5955
56+ groupItem . Bounds . Left = radius ;
57+ groupItem . Bounds . Top = radius ;
58+
59+ var prevAngle = - 90d ;
6060
6161 for ( int i = 0 ; i < valValues . Count ; i ++ )
6262 {
6363 valPercent . Add ( valValuesDoubles [ i ] / valTotal ) ;
64- circleSectorAngle . Add ( valPercent [ i ] / 360d ) ;
65- var xPoint = cx + ( radius * Math . Cos ( circleSectorAngle [ i ] ) ) ;
66- var yPoint = cy + ( radius * Math . Sin ( circleSectorAngle [ i ] ) ) ;
64+
65+ var angle = valPercent [ i ] * 360d ;
66+
67+ angle += prevAngle ;
68+
69+ circleSectorAngle . Add ( angle ) ;
70+
71+ var angleRadians = angle * ( Math . PI / 180.0d ) ;
72+
73+ var xPoint = cx + ( radius * Math . Cos ( angleRadians ) ) ;
74+ var yPoint = cy + ( radius * Math . Sin ( angleRadians ) ) ;
6775 endCoordOffsetFromCenterOfCircle . Add ( new Coordinate ( xPoint , yPoint ) ) ;
76+
77+ prevAngle = angle ;
6878 }
6979
7080 var count = Math . Min ( catValues . Count , valValues . Count ) ;
71- for ( var i = 0 ; i < catValues . Count ; i ++ )
81+
82+ for ( int i = 0 ; i < chartType . Series . Count ; i ++ )
7283 {
7384 var serie = ( ExcelPieChartSerie ) chartType . Series [ i ] ;
85+ for ( var j = 0 ; j < catValues . Count ; j ++ )
86+ {
87+ var total = ConvertUtil . GetValueDouble ( catValues [ j ] , false , true ) ;
7488
75- var total = ConvertUtil . GetValueDouble ( catValues [ i ] , false , true ) ;
76-
77- var dataPoints = new List < BoundingBox > ( ) ;
78-
79- //Add the slice.
80- AddSlice ( chartType , serie , catValues , valValues , dataPoints , count , i , radius ) ;
81-
89+ var dataPoints = new List < BoundingBox > ( ) ;
8290
91+ //Add the slice.
92+ AddSlice ( chartType , serie , catValues , valValues , dataPoints , count , j , radius ) ;
93+ }
8394 }
95+
8496 RenderItems . Add ( new SvgEndGroupItem ( ChartRenderer , null ) ) ;
8597 }
8698
8799 private void AddSlice ( ExcelPieChart chartType , ExcelPieChartSerie serie , List < object > catSeries , List < object > valSeries , List < BoundingBox > dataPoints , int seriesCount , int position , double radius )
88100 {
89- var slice = new SvgRenderEllipseItem ( ChartRenderer , _svgChart . Plotarea . Rectangle . Bounds ) ;
101+ var slice = new SvgRenderPathItem ( ChartRenderer , _svgChart . Plotarea . Rectangle . Bounds ) ;
102+
103+ var cx = _svgChart . Plotarea . Rectangle . Bounds . Width / 2 ;
104+ var cy = _svgChart . Plotarea . Rectangle . Bounds . Height / 2 ;
105+
106+ var moveCenter = new PathCommands ( PathCommandType . Move , slice , cx / _svgChart . Bounds . Width , cy / _svgChart . Bounds . Height ) ;
107+ Coordinate startPoint ;
108+ if ( position != 0 )
109+ {
110+ startPoint = new Coordinate ( endCoordOffsetFromCenterOfCircle [ position - 1 ] . X , endCoordOffsetFromCenterOfCircle [ position - 1 ] . Y ) ;
111+ }
112+ else
113+ {
114+ startPoint = new Coordinate ( cx , 0 ) ;
115+ }
116+
117+ var lineToStart = new PathCommands ( PathCommandType . Line , slice , startPoint . X / _svgChart . Bounds . Width , startPoint . Y / _svgChart . Bounds . Height ) ;
118+
119+ var lineToEndPoint = new PathCommands ( PathCommandType . Line , slice , endCoordOffsetFromCenterOfCircle [ position ] . X / _svgChart . Bounds . Width , endCoordOffsetFromCenterOfCircle [ position ] . Y / _svgChart . Bounds . Height ) ;
90120
121+ //var arcCommand = new PathCommands(PathCommandType.Arc, slice, new double[] { startPoint.X / _svgChart.Bounds.Width, startPoint.Y / _svgChart.Bounds.Height, 0, 0, 1, endCoordOffsetFromCenterOfCircle[position].X / _svgChart.Bounds.Width, endCoordOffsetFromCenterOfCircle[position].Y / _svgChart.Bounds.Height });
122+
123+ slice . Commands . Add ( moveCenter ) ;
124+ slice . Commands . Add ( lineToStart ) ;
125+
126+ slice . Commands . Add ( moveCenter ) ;
127+ slice . Commands . Add ( lineToEndPoint ) ;
128+
129+ if ( position == 0 )
130+ {
131+ serie . Fill . Color = Color . Red ;
132+ serie . Border . Fill . Color = Color . DarkOrange ;
133+ }
134+ else if ( position == 1 )
135+ {
136+ serie . Fill . Color = Color . Green ;
137+ serie . Border . Fill . Color = Color . DarkGreen ;
138+ }
139+ else if ( position == 2 )
140+ {
141+ serie . Fill . Color = Color . Blue ;
142+ serie . Border . Fill . Color = Color . DarkBlue ;
143+ }
144+ //slice.Commands.Add(arcCommand);
91145
92146 slice . SetDrawingPropertiesFill ( serie . Fill , chartType . StyleManager . Style . SeriesAxis . FillReference . Color ) ;
93147 slice . SetDrawingPropertiesBorder ( serie . Border , chartType . StyleManager . Style . SeriesAxis . BorderReference . Color , true ) ;
0 commit comments