forked from AsBuiltReport/AsBuiltReport.Chart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDonutChart.cs
More file actions
140 lines (120 loc) · 5.34 KB
/
Copy pathDonutChart.cs
File metadata and controls
140 lines (120 loc) · 5.34 KB
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
using ScottPlot;
using System;
using System.IO;
using System.Collections.Generic;
namespace AsBuiltReportChart
{
internal class Donut : Chart
{
public Donut() { }
public object Chart(double[] values, string[] labels, string filename = "output", int width = 400, int height = 300)
{
if (values.Length == labels.Length)
{
Plot myPlot = new Plot();
if (EnableCustomColorPalette)
{
if (_customColorPalette != null && _customColorPalette.Length > 0)
{
myPlot.Add.Palette = colorPalette = new ScottPlot.Palettes.Custom(_customColorPalette);
}
else
{
throw new InvalidOperationException("CustomColorPalette is empty. Please provide valid color values.");
}
}
else
{
// Set ScottPlot native color palette
if (colorPalette != null)
{
myPlot.Add.Palette = colorPalette;
}
}
List<PieSlice> slices = new List<PieSlice>();
for (int i = 0; i < values.Length; i++)
{
slices.Add(new PieSlice()
{
Value = values[i],
LabelText = HideValues ? null : values[i].ToString(),
LabelFontSize = LabelFontSize,
LabelFontColor = GetDrawingColor(LabelFontColor),
LabelBold = LabelBold,
LabelFontName = FontName,
LegendText = EnableLegend ? labels[i] : null,
FillColor = colorPalette.GetColor(i)
});
}
var pie = myPlot.Add.Pie(slices);
pie.DonutFraction = DonutFraction;
// hide unnecessary plot components
myPlot.Axes.Frameless();
myPlot.HideGrid();
if (EnableLegend)
{
// Legend Font Properties
myPlot.Legend.FontName = FontName;
myPlot.Legend.FontSize = LegendFontSize;
myPlot.Legend.FontColor = GetDrawingColor(LegendFontColor);
// Legend box Style Properties
myPlot.Legend.OutlineColor = GetDrawingColor(LegendBorderColor);
myPlot.Legend.OutlineWidth = LegendBorderSize;
myPlot.Legend.OutlinePattern = LegendBorderStyleMap[LegendBorderStyle];
myPlot.Legend.Orientation = LegendOrientationMap[LegendOrientation];
myPlot.Legend.Alignment = LegendAlignmentMap[LegendAlignment];
}
if (EnableChartBorder)
{
myPlot.FigureBorder = new LineStyle()
{
Color = GetDrawingColor(ChartBorderColor),
Width = ChartBorderSize,
Pattern = ChartBorderStyleMap[ChartBorderStyle],
};
}
// Set title properties
if (Title != null)
{
myPlot.Title(Title);
myPlot.Axes.Title.Label.FontSize = TitleFontSize;
myPlot.Axes.Title.Label.ForeColor = GetDrawingColor(TitleFontColor);
myPlot.Axes.Title.Label.Bold = TitleFontBold;
myPlot.Axes.Title.Label.FontName = FontName;
}
// Set margins settings
myPlot.Axes.Margins(left: AxesMarginsLeft, right: AxesMarginsRight, bottom: AxesMarginsDown, top: AxesMarginsTop);
// Set background colors
if (FigureBackgroundColor.HasValue)
{
myPlot.FigureBackground.Color = GetDrawingColor(FigureBackgroundColor.Value);
}
if (DataBackgroundColor.HasValue)
{
myPlot.DataBackground.Color = GetDrawingColor(DataBackgroundColor.Value);
}
// Set label distance from the center of the donut slices
pie.SliceLabelDistance = _labelDistance;
if (DonutCenterText != null)
{
var annotation = myPlot.Add.Annotation(DonutCenterText);
annotation.Alignment = Alignment.MiddleCenter;
annotation.LabelStyle.FontSize = 12;
annotation.LabelStyle.BackgroundColor = Colors.White;
annotation.LabelStyle.BorderColor = Colors.White;
annotation.LabelShadowColor = Colors.Transparent;
}
// Apply watermark if enabled
ApplyWatermark(myPlot);
// Set filetpath to save
string Filepath = _outputFolderPath ?? Directory.GetCurrentDirectory();
// Set filename
return SaveInFormat(myPlot, width, height, Filepath, filename, Format);
}
else
{
throw new ArgumentException("Error: Values and labels must be equal.");
}
}
}
}