-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathNumericAxisExample.xaml.cs
109 lines (96 loc) · 3.72 KB
/
NumericAxisExample.xaml.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
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Telerik.Windows.Controls.ChartView;
namespace AxisLikeAnnotations
{
public partial class NumericAxisExample : UserControl
{
public const string CustomAxisPositionKey = "CustomAxisPosition";
public const string CustomAxisElementKey = "CustomAxisElement";
public NumericAxisExample()
{
InitializeComponent();
this.DataContext = GetData();
this.chart.SizeChanged += Chart_SizeChanged;
}
private void Chart_SizeChanged(object sender, SizeChangedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() => {
UpdateAxis();
}));
}
private void UpdateAxis()
{
ClearCustomAxis();
var line = CreateLine();
this.chart.Annotations.Add(line);
var step = this.verticalAxis.ActualMajorStep;
var min = this.verticalAxis.ActualRange.Minimum + step;
var max = this.verticalAxis.ActualRange.Maximum;
for (double i = min; i <= max; i += step)
{
var tick = CreateTick(i);
this.chart.Annotations.Add(tick);
}
}
private CartesianCustomAnnotation CreateTick(double value)
{
var tick = new CartesianCustomAnnotation();
tick.ClipToPlotArea = false;
tick.Content = CreateTickVisual(value);
tick.VerticalValue = value;
tick.HorizontalValue = CustomAxisPositionKey;
tick.HorizontalAlignment = HorizontalAlignment.Left;
tick.VerticalAlignment = VerticalAlignment.Center;
tick.Tag = CustomAxisElementKey;
return tick;
}
private UIElement CreateTickVisual(double value)
{
var tick = new Rectangle() { Fill = Brushes.Black, Width = 5, Height = 1, Margin = new Thickness(3, 0, 0, 0) };
var label = new TextBlock() { Text = value.ToString() };
var panel = new StackPanel() { Orientation = Orientation.Horizontal };
panel.Children.Add(label);
panel.Children.Add(tick);
return panel;
}
public CartesianGridLineAnnotation CreateLine()
{
var annotation = new CartesianGridLineAnnotation();
annotation.Stroke = Brushes.Black;
annotation.Axis = this.horizontalAxis;
annotation.Value = CustomAxisPositionKey;
annotation.Tag = CustomAxisElementKey;
return annotation;
}
private void ClearCustomAxis()
{
var axisElements = this.chart.Annotations.Where(x => x.Tag != null && x.Tag.Equals(CustomAxisElementKey)).ToList();
foreach (var item in axisElements)
{
this.chart.Annotations.Remove(item);
}
}
public ObservableCollection<PlotInfo> GetData()
{
var data = new ObservableCollection<PlotInfo>();
for (int i = 1; i <= 5; i++)
{
var value = PlotInfo.RandomNumberGenerator.Next(100, 300);
data.Add(new PlotInfo() { Value = value, Category = "A" + i });
}
data.Add(new PlotInfo() { Value = null, Category = CustomAxisPositionKey });
for (int i = 1; i <= 5; i++)
{
var value = PlotInfo.RandomNumberGenerator.Next(100, 300);
data.Add(new PlotInfo() { Value = value, Category = "B" + i });
}
return data;
}
}
}