forked from microcharts-dotnet/Microcharts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointChart.cs
More file actions
266 lines (222 loc) · 9.98 KB
/
PointChart.cs
File metadata and controls
266 lines (222 loc) · 9.98 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) Aloïs DENIEL. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
namespace Microcharts
{
using System;
using System.Collections.Generic;
using System.Linq;
using SkiaSharp;
/// <summary>
/// 
///
/// Point chart.
/// </summary>
public class PointChart : Chart
{
#region Properties
public float PointSize { get; set; } = 14;
public PointMode PointMode { get; set; } = PointMode.Circle;
public byte PointAreaAlpha { get; set; } = 100;
private float ValueRange => this.MaxValue - this.MinValue;
public int ChartNr { get; set; } = 1;
#endregion
#region Methods
public float CalculateYOrigin(float itemHeight, float headerHeight)
{
if (this.MaxValue <= 0)
{
return headerHeight;
}
if (this.MinValue > 0)
{
return headerHeight + itemHeight;
}
return headerHeight + ((this.MaxValue / this.ValueRange) * itemHeight);
}
public override void DrawContent(SKCanvas canvas, int width, int height)
{
var valueLabelSizes = this.MeasureValueLabels();
var footerHeight = this.CalculateFooterHeight(valueLabelSizes);
var headerHeight = this.CalculateHeaderHeight(valueLabelSizes);
var itemSize = this.CalculateItemSize(width, height, footerHeight, headerHeight);
var origin = this.CalculateYOrigin(itemSize.Height, headerHeight);
var points = this.CalculatePoints(itemSize, origin, headerHeight);
this.DrawPointAreas(canvas, points, origin);
this.DrawPoints(canvas, points);
this.DrawFooter(canvas, points, itemSize, height, footerHeight);
this.DrawValueLabel(canvas, points, itemSize, height, valueLabelSizes);
}
protected SKSize CalculateItemSize(int width, int height, float footerHeight, float headerHeight)
{
var total = this.Entries.GroupBy(n => n.Label).Select(o => o.FirstOrDefault()).Count(); // check for unique labels
var w = (width - ((total + 1) * this.Margin)) / total;
var h = height - this.Margin - footerHeight - headerHeight;
return new SKSize(w, h);
}
protected SKPoint[] CalculatePoints(SKSize itemSize, float origin, float headerHeight)
{
var result = new List<SKPoint>();
var LineChartNr = this.Entries.GroupBy(n => n.LineChartNr).Select(o => o.FirstOrDefault()).OrderBy(p => p.LineChartNr).ToList(); // check for unique chart nr
ChartNr = LineChartNr.Count();
for (int l = 0; l < LineChartNr.Count(); l++)
{
for (int i = 0; i < this.Entries.Where(n => n.LineChartNr == LineChartNr.ElementAt(l).LineChartNr).Count(); i++)
{
var entry = this.Entries.Where(n => n.LineChartNr == LineChartNr.ElementAt(l).LineChartNr).ElementAt(i);
var x = this.Margin + (itemSize.Width / 2) + (i * (itemSize.Width + this.Margin));
var y = headerHeight + (((this.MaxValue - entry.Value) / this.ValueRange) * itemSize.Height);
var point = new SKPoint(x, y);
result.Add(point);
}
}
return result.ToArray();
}
protected void DrawFooter(SKCanvas canvas, SKPoint[] points, SKSize itemSize, int height, float footerHeight)
{
this.DrawLabels(canvas, points, itemSize, height, footerHeight);
}
protected void DrawLabels(SKCanvas canvas, SKPoint[] points, SKSize itemSize, int height, float footerHeight)
{
for (int i = 0; i < this.Entries.GroupBy(n => n.Label).Select(o => o.FirstOrDefault()).Count(); i++) // check for unique labels
{
var entry = this.Entries.ElementAt(i);
var point = points[i];
if (!string.IsNullOrEmpty(entry.Label))
{
using (var paint = new SKPaint())
{
paint.TextSize = this.LabelTextSize;
paint.IsAntialias = true;
paint.Color = entry.TextColor;
paint.IsStroke = false;
var bounds = new SKRect();
var text = entry.Label;
paint.MeasureText(text, ref bounds);
if (bounds.Width > itemSize.Width)
{
text = text.Substring(0, Math.Min(3, text.Length));
paint.MeasureText(text, ref bounds);
}
if (bounds.Width > itemSize.Width)
{
text = text.Substring(0, Math.Min(1, text.Length));
paint.MeasureText(text, ref bounds);
}
canvas.DrawText(text, point.X - (bounds.Width / 2), height - this.Margin + (this.LabelTextSize / 2), paint);
}
}
}
}
protected void DrawPoints(SKCanvas canvas, SKPoint[] points)
{
if (points.Length > 0 && PointMode != PointMode.None)
{
for (int i = 0; i < points.Length; i++)
{
var entry = this.Entries.ElementAt(i);
var point = points[i];
canvas.DrawPoint(point, entry.Color, this.PointSize, this.PointMode);
}
}
}
protected void DrawPointAreas(SKCanvas canvas, SKPoint[] points, float origin)
{
if (points.Length > 0 && this.PointAreaAlpha > 0)
{
for (int i = 0; i < points.Length; i++)
{
var entry = this.Entries.ElementAt(i);
var point = points[i];
var y = Math.Min(origin, point.Y);
using (var shader = SKShader.CreateLinearGradient(new SKPoint(0, origin), new SKPoint(0, point.Y), new[] { entry.Color.WithAlpha(this.PointAreaAlpha), entry.Color.WithAlpha((byte)(this.PointAreaAlpha / 3)) }, null, SKShaderTileMode.Clamp))
using (var paint = new SKPaint
{
Style = SKPaintStyle.Fill,
Color = entry.Color.WithAlpha(this.PointAreaAlpha),
})
{
paint.Shader = shader;
var height = Math.Max(2, Math.Abs(origin - point.Y));
canvas.DrawRect(SKRect.Create(point.X - (this.PointSize / 2), y, this.PointSize, height), paint);
}
}
}
}
protected void DrawValueLabel(SKCanvas canvas, SKPoint[] points, SKSize itemSize, float height, SKRect[] valueLabelSizes)
{
if (points.Length > 0)
{
int marginLabel = 0;
for (int i = 0; i < points.Length; i++)
{
if ((i > 1) && (i%(points.Length/this.ChartNr) == 0))
marginLabel += 20;
var entry = this.Entries.ElementAt(i);
var point = points[i];
var isAbove = point.Y > (this.Margin + (itemSize.Height / 2));
if (!string.IsNullOrEmpty(entry.ValueLabel))
{
using (new SKAutoCanvasRestore(canvas))
{
using (var paint = new SKPaint())
{
paint.TextSize = this.LabelTextSize;
paint.FakeBoldText = true;
paint.IsAntialias = true;
paint.Color = entry.Color;
paint.IsStroke = false;
var bounds = new SKRect();
var text = entry.ValueLabel;
paint.MeasureText(text, ref bounds);
canvas.RotateDegrees(90);
canvas.Translate(this.Margin, -point.X + (bounds.Height / 2) + marginLabel);
canvas.DrawText(text, 0, 0, paint);
}
}
}
}
}
}
protected float CalculateFooterHeight(SKRect[] valueLabelSizes)
{
var result = this.Margin;
if (this.Entries.Any(e => !string.IsNullOrEmpty(e.Label)))
{
result += this.LabelTextSize + this.Margin;
}
return result;
}
protected float CalculateHeaderHeight(SKRect[] valueLabelSizes)
{
var result = this.Margin;
if (this.Entries.Any())
{
var maxValueWidth = valueLabelSizes.Max(x => x.Width);
if (maxValueWidth > 0)
{
result += maxValueWidth + this.Margin;
}
}
return result;
}
protected SKRect[] MeasureValueLabels()
{
using (var paint = new SKPaint())
{
paint.TextSize = this.LabelTextSize;
return this.Entries.Select(e =>
{
if (string.IsNullOrEmpty(e.ValueLabel))
{
return SKRect.Empty;
}
var bounds = new SKRect();
var text = e.ValueLabel;
paint.MeasureText(text, ref bounds);
return bounds;
}).ToArray();
}
}
#endregion
}
}