forked from Live-Charts/Live-Charts
/
Series.cs
607 lines (532 loc) · 22 KB
/
Series.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//The MIT License(MIT)
//Copyright(c) 2016 Alberto Rodriguez & LiveCharts Contributors
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using LiveCharts.Defaults;
using LiveCharts.Definitions.Points;
using LiveCharts.Definitions.Series;
using LiveCharts.Helpers;
using LiveCharts.Wpf.Charts.Base;
using LiveCharts.Wpf.Components;
using LiveCharts.Wpf.Converters;
namespace LiveCharts.Wpf
{
/// <summary>
/// Base WPF and WinForms series, this class is abstract
/// </summary>
public abstract class Series : FrameworkElement, ISeriesView
{
#region Constructors
/// <summary>
/// Initializes a new Instance of Series
/// </summary>
protected Series()
{
DefaultFillOpacity = 0.35;
SetCurrentValue(TitleProperty, "Series");
IsVisibleChanged += OnIsVisibleChanged;
IsFirstDraw = true;
}
/// <summary>
/// Initializes a new Instance of series, with a given configuration
/// </summary>
/// <param name="configuration"></param>
protected Series(object configuration)
{
Configuration = configuration;
SetValue(TitleProperty, "Series");
IsVisibleChanged += OnIsVisibleChanged;
IsFirstDraw = true;
}
#endregion
#region Properties
private IChartValues LastKnownValues { get; set; }
internal double DefaultFillOpacity { get; set; }
/// <summary>
/// Gets a value indicating whether this instance is first draw.
/// </summary>
/// <value>
/// <c>true</c> if this instance is first draw; otherwise, <c>false</c>.
/// </value>
public bool IsFirstDraw { get; internal set; }
/// <summary>
/// THe Model is set by every series type, it is the motor of the series, it is the communication with the core of the library
/// </summary>
public SeriesAlgorithm Model { get; set; }
/// <summary>
/// Gets the Actual values in the series, active or visible series only
/// </summary>
public IChartValues ActualValues
{
get
{
if (DesignerProperties.GetIsInDesignMode(this) && (Values == null || Values.Count == 0))
SetValue(ValuesProperty, GetValuesForDesigner());
return Values ?? new ChartValues<double>();
}
}
/// <summary>
/// Gets whether the series is visible
/// </summary>
public bool IsSeriesVisible
{
get { return Visibility == Visibility.Visible; }
}
/// <summary>
/// Gets the current chart points in the series
/// </summary>
public IEnumerable<ChartPoint> ChartPoints
{
get { return ActualValues.GetPoints(this); }
}
/// <summary>
/// The values property
/// </summary>
public static readonly DependencyProperty ValuesProperty = DependencyProperty.Register(
"Values", typeof (IChartValues), typeof (Series),
new PropertyMetadata(default(IChartValues), OnValuesInstanceChanged));
/// <summary>
/// Gets or sets chart values.
/// </summary>
[TypeConverter(typeof(NumericChartValuesConverter))]
public IChartValues Values
{
get { return ThreadAccess.Resolve<IChartValues>(this, ValuesProperty); }
set { SetValue(ValuesProperty, value); }
}
/// <summary>
/// The title property
/// </summary>
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
"Title", typeof (string), typeof (Series),
new PropertyMetadata(default(string), CallChartUpdater()));
/// <summary>
/// Gets or sets series title
/// </summary>
public string Title
{
get { return (string) GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
/// <summary>
/// The stroke property
/// </summary>
public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
"Stroke", typeof (Brush), typeof (Series),
new PropertyMetadata(default(Brush), CallChartUpdater()));
/// <summary>
/// Gets or sets series stroke, if this property is null then a SolidColorBrush will be assigned according to series position in collection and Chart.Colors property
/// </summary>
public Brush Stroke
{
get { return (Brush) GetValue(StrokeProperty); }
set { SetValue(StrokeProperty, value); }
}
/// <summary>
/// The stroke thickness property
/// </summary>
public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
"StrokeThickness", typeof (double), typeof (Series),
new PropertyMetadata(default(double), CallChartUpdater()));
/// <summary>
/// Gets or sets the series stroke thickness.
/// </summary>
public double StrokeThickness
{
get { return (double) GetValue(StrokeThicknessProperty); }
set { SetValue(StrokeThicknessProperty, value); }
}
/// <summary>
/// The fill property
/// </summary>
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
"Fill", typeof (Brush), typeof (Series),
new PropertyMetadata(default(Brush), CallChartUpdater()));
/// <summary>
/// Gets or sets series fill color, if this property is null then a SolidColorBrush will be assigned according to series position in collection and Chart.Colors property, also Fill property has a default opacity according to chart type.
/// </summary>
public Brush Fill
{
get { return (Brush) GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
/// <summary>
/// The data labels property
/// </summary>
public static readonly DependencyProperty DataLabelsProperty = DependencyProperty.Register(
"DataLabels", typeof (bool), typeof (Series),
new PropertyMetadata(default(bool), CallChartUpdater()));
/// <summary>
/// Gets or sets if series should include a label over each data point.
/// </summary>
public bool DataLabels
{
get { return (bool) GetValue(DataLabelsProperty); }
set { SetValue(DataLabelsProperty, value); }
}
/// <summary>
/// The labels template property
/// </summary>
public static readonly DependencyProperty DataLabelsTemplateProperty = DependencyProperty.Register(
"DataLabelsTemplate", typeof(DataTemplate), typeof(Series),
new PropertyMetadata(DefaultXamlReader.DataLabelTemplate(), CallChartUpdater()));
/// <summary>
/// Gets or sets the labels template.
/// </summary>
/// <value>
/// The labels template.
/// </value>
public DataTemplate DataLabelsTemplate
{
get { return (DataTemplate) GetValue(DataLabelsTemplateProperty); }
set { SetValue(DataLabelsTemplateProperty, value); }
}
/// <summary>
/// The font family property
/// </summary>
public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register(
"FontFamily", typeof (FontFamily), typeof (Series),
new PropertyMetadata(new FontFamily("Segoe UI")));
/// <summary>
/// Gets or sets labels font family
/// </summary>
public FontFamily FontFamily
{
get { return (FontFamily) GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
/// <summary>
/// The font size property
/// </summary>
public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register(
"FontSize", typeof (double),
typeof (Series), new PropertyMetadata(10d, CallChartUpdater()));
/// <summary>
/// Gets or sets labels font size
/// </summary>
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
/// <summary>
/// The font weight property
/// </summary>
public static readonly DependencyProperty FontWeightProperty = DependencyProperty.Register(
"FontWeight", typeof (FontWeight), typeof (Series),
new PropertyMetadata(FontWeights.Bold, CallChartUpdater()));
/// <summary>
/// Gets or sets labels font weight
/// </summary>
public FontWeight FontWeight
{
get { return (FontWeight)GetValue(FontWeightProperty); }
set { SetValue(FontWeightProperty, value); }
}
/// <summary>
/// The font style property
/// </summary>
public static readonly DependencyProperty FontStyleProperty = DependencyProperty.Register(
"FontStyle", typeof (FontStyle),
typeof (Series), new PropertyMetadata(FontStyles.Normal, CallChartUpdater()));
/// <summary>
/// Gets or sets labels font style
/// </summary>
public FontStyle FontStyle
{
get { return (FontStyle)GetValue(FontStyleProperty); }
set { SetValue(FontStyleProperty, value); }
}
/// <summary>
/// The font stretch property
/// </summary>
public static readonly DependencyProperty FontStretchProperty = DependencyProperty.Register(
"FontStretch", typeof (FontStretch),
typeof (Series), new PropertyMetadata(FontStretches.Normal, CallChartUpdater()));
/// <summary>
/// Gets or sets labels font stretch
/// </summary>
public FontStretch FontStretch
{
get { return (FontStretch)GetValue(FontStretchProperty); }
set { SetValue(FontStretchProperty, value); }
}
/// <summary>
/// The foreground property
/// </summary>
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register(
"Foreground", typeof (Brush),
typeof (Series), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(55, 71, 79))));
/// <summary>
/// Gets or sets labels text color.
/// </summary>
public Brush Foreground
{
get { return (Brush)GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
/// <summary>
/// The stroke dash array property
/// </summary>
public static readonly DependencyProperty StrokeDashArrayProperty = DependencyProperty.Register(
"StrokeDashArray", typeof(DoubleCollection), typeof(Series),
new PropertyMetadata(default(DoubleCollection)));
/// <summary>
/// Gets or sets the stroke dash array of a series, sue this property to draw dashed strokes
/// </summary>
public DoubleCollection StrokeDashArray
{
get { return (DoubleCollection)GetValue(StrokeDashArrayProperty); }
set { SetValue(StrokeDashArrayProperty, value); }
}
/// <summary>
/// The point geometry property
/// </summary>
public static readonly DependencyProperty PointGeometryProperty =
DependencyProperty.Register("PointGeometry", typeof (Geometry), typeof (Series),
new PropertyMetadata(DefaultGeometries.Circle, CallChartUpdater()));
/// <summary>
/// Gets or sets the point geometry, this shape will be drawn in the Tooltip, Legend, and if line series in every point also.
/// </summary>
public Geometry PointGeometry
{
get { return ((Geometry)GetValue(PointGeometryProperty)); }
set { SetValue(PointGeometryProperty, value); }
}
/// <summary>
/// The scales x at property
/// </summary>
public static readonly DependencyProperty ScalesXAtProperty = DependencyProperty.Register(
"ScalesXAt", typeof (int), typeof (Series), new PropertyMetadata(default(int), CallChartUpdater()));
/// <summary>
/// Gets or sets the axis where series is scaled at, the axis must exist in the collection
/// </summary>
public int ScalesXAt
{
get { return (int) GetValue(ScalesXAtProperty); }
set { SetValue(ScalesXAtProperty, value); }
}
/// <summary>
/// The scales y at property
/// </summary>
public static readonly DependencyProperty ScalesYAtProperty = DependencyProperty.Register(
"ScalesYAt", typeof (int), typeof (Series), new PropertyMetadata(default(int), CallChartUpdater()));
/// <summary>
/// Gets or sets the axis where series is scaled at, the axis must exist in the collection
/// </summary>
public int ScalesYAt
{
get { return (int) GetValue(ScalesYAtProperty); }
set { SetValue(ScalesYAtProperty, value); }
}
/// <summary>
/// The label point property
/// </summary>
public static readonly DependencyProperty LabelPointProperty = DependencyProperty.Register(
"LabelPoint", typeof (Func<ChartPoint, string>), typeof (Series), new PropertyMetadata(default(Func<ChartPoint, string>)));
/// <summary>
/// Gets or sets the label formatter for the data label and tooltip, this property is set by default according to the series
/// </summary>
public Func<ChartPoint, string> LabelPoint
{
get { return (Func<ChartPoint, string>) GetValue(LabelPointProperty); }
set { SetValue(LabelPointProperty, value); }
}
/// <summary>
/// The configuration property
/// </summary>
public static readonly DependencyProperty ConfigurationProperty = DependencyProperty.Register(
"Configuration", typeof (object), typeof (Series),
new PropertyMetadata(default(object), CallChartUpdater()));
/// <summary>
/// Gets or sets series mapper, if this property is set then the library will ignore the SeriesCollection mapper and global mappers.
/// </summary>
public object Configuration
{
get { return GetValue(ConfigurationProperty); }
set { SetValue(ConfigurationProperty, value); }
}
#endregion
#region Internal Helpers
internal ContentControl UpdateLabelContent(DataLabelViewModel content, ContentControl currentControl)
{
ContentControl control;
if (currentControl == null)
{
control = new ContentControl();
control.SetBinding(VisibilityProperty,
new Binding {Path = new PropertyPath(VisibilityProperty), Source = this});
Panel.SetZIndex(control, int.MaxValue - 1);
Model.Chart.View.AddToDrawMargin(control);
}
else
{
control = currentControl;
}
control.Content = content;
control.ContentTemplate = DataLabelsTemplate;
control.FontFamily = FontFamily;
control.FontSize = FontSize;
control.FontStretch = FontStretch;
control.FontStyle = FontStyle;
control.FontWeight = FontWeight;
control.Foreground = Foreground;
return control;
}
#endregion
#region Publics
/// <summary>
/// Gets the view of a given point
/// </summary>
/// <param name="point"></param>
/// <param name="label"></param>
/// <returns></returns>
public virtual IChartPointView GetPointView(ChartPoint point, string label)
{
throw new NotImplementedException();
}
/// <summary>
/// This method runs when the update starts
/// </summary>
public virtual void OnSeriesUpdateStart()
{
}
/// <summary>
/// Erases series
/// </summary>
public virtual void Erase(bool removeFromView = true)
{
Values.GetPoints(this).ForEach(p =>
{
if (p.View != null)
p.View.RemoveFromView(Model.Chart);
});
if (removeFromView) Model.Chart.View.RemoveFromView(this);
}
/// <summary>
/// This method runs when the update finishes
/// </summary>
public virtual void OnSeriesUpdatedFinish()
{
IsFirstDraw = false;
}
/// <summary>
/// Initializes the series colors if they are not set
/// </summary>
public virtual void InitializeColors()
{
var wpfChart = (Chart) Model.Chart.View;
if (Stroke != null && Fill != null) return;
var nextColor = wpfChart.GetNextDefaultColor();
if (Stroke == null)
{
var strokeBrush = new SolidColorBrush(nextColor);
strokeBrush.Freeze();
SetValue(StrokeProperty, strokeBrush);
}
if (Fill == null)
{
var fillBursh = new SolidColorBrush(nextColor) {Opacity = DefaultFillOpacity};
fillBursh.Freeze();
SetValue(FillProperty, fillBursh);
}
}
/// <summary>
/// Defines special elements to draw according to the series type
/// </summary>
public virtual void DrawSpecializedElements()
{
}
/// <summary>
/// Places specializes items
/// </summary>
public virtual void PlaceSpecializedElements()
{
}
/// <summary>
/// Gets the label point formatter.
/// </summary>
/// <returns></returns>
public Func<ChartPoint, string> GetLabelPointFormatter()
{
if (DesignerProperties.GetIsInDesignMode(this))
return x => "Label";
return LabelPoint;
}
#endregion
private static void OnValuesInstanceChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var series = (Series) dependencyObject;
if (series.Values != series.LastKnownValues && series.LastKnownValues != null)
{
series.LastKnownValues.GetPoints(series).ForEach(
x => { if (x.View != null) x.View.RemoveFromView(series.Model.Chart); });
}
CallChartUpdater()(dependencyObject, dependencyPropertyChangedEventArgs);
series.LastKnownValues = series.Values;
}
/// <summary>
/// Calls the chart updater.
/// </summary>
/// <param name="animate">if set to <c>true</c> [animate].</param>
/// <returns></returns>
protected static PropertyChangedCallback CallChartUpdater(bool animate = false)
{
return (o, args) =>
{
var wpfSeries = o as Series;
if (wpfSeries == null) return;
if (wpfSeries.Model == null) return;
if (wpfSeries.Model.Chart != null) wpfSeries.Model.Chart.Updater.Run(animate);
};
}
private Visibility? PreviousVisibility { get; set; }
private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (Model.Chart == null || PreviousVisibility == Visibility) return;
PreviousVisibility = Visibility;
if (PreviousVisibility != null) Model.Chart.Updater.Run(false, false);
if (Visibility == Visibility.Collapsed || Visibility == Visibility.Hidden)
{
Erase(false);
}
}
private static IChartValues GetValuesForDesigner()
{
var r = new Random();
var gvt = Type.GetType("LiveCharts.Geared.GearedValues`1, LiveCharts.Geared");
if (gvt != null) gvt = gvt.MakeGenericType(typeof(ObservableValue));
var obj = gvt != null
? (IChartValues) Activator.CreateInstance(gvt)
: new ChartValues<ObservableValue>();
obj.Add(new ObservableValue(r.Next(0, 100)));
obj.Add(new ObservableValue(r.Next(0, 100)));
obj.Add(new ObservableValue(r.Next(0, 100)));
obj.Add(new ObservableValue(r.Next(0, 100)));
obj.Add(new ObservableValue(r.Next(0, 100)));
return obj;
}
#region Obsoletes
#endregion
}
}