forked from microcharts-dotnet/Microcharts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineChart.cs
More file actions
190 lines (165 loc) · 7.54 KB
/
LineChart.cs
File metadata and controls
190 lines (165 loc) · 7.54 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
// 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.Linq;
using SkiaSharp;
/// <summary>
/// 
///
/// Line chart.
/// </summary>
public class LineChart : PointChart
{
#region Constructors
public LineChart()
{
this.PointSize = 10;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the size of the line.
/// </summary>
/// <value>The size of the line.</value>
public float LineSize { get; set; } = 3;
/// <summary>
/// Gets or sets the line mode.
/// </summary>
/// <value>The line mode.</value>
public LineMode LineMode { get; set; } = LineMode.Straight; //LineMode.Spline;
/// <summary>
/// Gets or sets the alpha of the line area.
/// </summary>
/// <value>The line area alpha.</value>
public byte LineAreaAlpha { get; set; } = 32;
#endregion
#region Methods
public override void DrawContent(SKCanvas canvas, int width, int height)
{
var valueLabelSizes = MeasureValueLabels();
var footerHeight = CalculateFooterHeight(valueLabelSizes);
var headerHeight = CalculateHeaderHeight(valueLabelSizes);
var itemSize = CalculateItemSize(width, height, footerHeight, headerHeight);
var origin = CalculateYOrigin(itemSize.Height, headerHeight);
var points = this.CalculatePoints(itemSize, origin, headerHeight);
this.DrawArea(canvas, points, itemSize, origin);
this.DrawLine(canvas, points, itemSize);
this.DrawPoints(canvas, points);
this.DrawFooter(canvas, points, itemSize, height, footerHeight);
this.DrawValueLabel(canvas, points, itemSize, height, valueLabelSizes);
}
protected void DrawLine(SKCanvas canvas, SKPoint[] points, SKSize itemSize)
{
if (points.Length > 1 && this.LineMode != LineMode.None)
{
using (var paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.White,
StrokeWidth = this.LineSize,
IsAntialias = true,
})
{
using (var shader = this.CreateGradient(points))
{
paint.Shader = shader;
//var path = new SKPath();
int pointsPerChart = points.Length / this.ChartNr;
var last = (this.LineMode == LineMode.Spline) ? pointsPerChart - 1 : pointsPerChart;
int line = 0;
for (int l = 0; l < this.ChartNr; l++)
{
var path = new SKPath();
path.MoveTo(points[line*pointsPerChart]);
for (int i = 0 + (pointsPerChart*line); i < last + (pointsPerChart*line); i++)
{
if (this.LineMode == LineMode.Spline)
{
var entry = this.Entries.ElementAt(i);
var nextEntry = this.Entries.ElementAt(i + 1);
var cubicInfo = this.CalculateCubicInfo(points, i, itemSize);
path.CubicTo(cubicInfo.control, cubicInfo.nextControl, cubicInfo.nextPoint);
}
else if (this.LineMode == LineMode.Straight)
{
path.LineTo(points[i]);
}
}
canvas.DrawPath(path, paint);
line++;
}
}
}
}
}
protected void DrawArea(SKCanvas canvas, SKPoint[] points, SKSize itemSize, float origin)
{
if (this.LineAreaAlpha > 0 && points.Length > 1)
{
using (var paint = new SKPaint
{
Style = SKPaintStyle.Fill,
Color = SKColors.White,
IsAntialias = true,
})
{
using (var shader = this.CreateGradient(points, this.LineAreaAlpha))
{
paint.Shader = shader;
//var path = new SKPath();
int pointsPerChart = points.Length / this.ChartNr;
var last = (this.LineMode == LineMode.Spline) ? pointsPerChart - 1 : pointsPerChart;
int line = 0;
for (int l = 0; l < this.ChartNr; l++)
{
var path = new SKPath();
path.MoveTo(points[line * pointsPerChart].X, origin);
path.LineTo(points[line * pointsPerChart]);
for (int i = 0 + (pointsPerChart * line); i < last + (pointsPerChart * line); i++)
{
if (this.LineMode == LineMode.Spline)
{
var entry = this.Entries.ElementAt(i);
var nextEntry = this.Entries.ElementAt(i + 1);
var cubicInfo = this.CalculateCubicInfo(points, i, itemSize);
path.CubicTo(cubicInfo.control, cubicInfo.nextControl, cubicInfo.nextPoint);
}
else if (this.LineMode == LineMode.Straight)
{
path.LineTo(points[i]);
}
}
path.LineTo(points.Last().X, origin);
path.Close();
canvas.DrawPath(path, paint);
line++;
}
}
}
}
}
private (SKPoint point, SKPoint control, SKPoint nextPoint, SKPoint nextControl) CalculateCubicInfo(SKPoint[] points, int i, SKSize itemSize)
{
var point = points[i];
var nextPoint = points[i + 1];
var controlOffset = new SKPoint(itemSize.Width * 0.8f, 0);
var currentControl = point + controlOffset;
var nextControl = nextPoint - controlOffset;
return (point, currentControl, nextPoint, nextControl);
}
private SKShader CreateGradient(SKPoint[] points, byte alpha = 255)
{
var startX = points.First().X;
var endX = points.Last().X;
var rangeX = endX - startX;
return SKShader.CreateLinearGradient(
new SKPoint(startX, 0),
new SKPoint(endX, 0),
this.Entries.Select(x => x.Color.WithAlpha(alpha)).ToArray(),
null,
SKShaderTileMode.Clamp);
}
#endregion
}
}