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
using FLY.Thick.Base.IService;
using LiveCharts;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Unity;
namespace FLY.Thick.Base.UI
{
/// <summary>
/// PgGrid.xaml 的交互逻辑
/// </summary>
public partial class PgGrid : Page
{
PgGridVm viewModel;
/// <summary>
///
/// </summary>
public PgGrid()
{
InitializeComponent();
InitializeChart();
}
void InitializeChart()
{
//写在xaml ,设计模式会引发 最大值最小值=0 异常, 所以用代码方式编写
//
//异常内容:
//LiveChartsException: One axis has an invalid range, it is or it tends to zero, please ensure your axis has a valid range
var axis = chart1.AxisX[0];
axis.SetBinding(LiveCharts.Wpf.Axis.LabelFormatterProperty, nameof(PgGridVm.XFormatter));
axis.SetBinding(LiveCharts.Wpf.Axis.MaxValueProperty, nameof(PgGridVm.XUpper));
axis.SetBinding(LiveCharts.Wpf.Axis.MinValueProperty, nameof(PgGridVm.XLower));
axis = chart1.AxisY[0];
axis.SetBinding(LiveCharts.Wpf.Axis.LabelFormatterProperty, nameof(PgGridVm.YFormatter));
axis.SetBinding(LiveCharts.Wpf.Axis.MaxValueProperty, nameof(PgGridVm.YUpper));
axis.SetBinding(LiveCharts.Wpf.Axis.MinValueProperty, nameof(PgGridVm.YLower));
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="container"></param>
/// <param name="initParam"></param>
/// <param name="gageService"></param>
/// <param name="flyADService"></param>
[InjectionMethod]
public void Init(
IUnityContainer container,
IInitParamService initParam,
ITDGageService gageService,
IFlyADService flyADService
)
{
viewModel = new PgGridVm();
viewModel.Init(initParam, gageService, flyADService);
this.DataContext = viewModel;
container.BuildUp(mircoGage);
}
private void UIElement_OnMouseMove(object sender, MouseEventArgs e)
{
if (!isDown)
return;
var vm = viewModel;
var series = vm.SeriesInfos.First(info => info.IsSelected).Series;
var chart = (LiveCharts.Wpf.CartesianChart)sender;
//lets get where the mouse is at our chart
var mouseCoordinate = e.GetPosition(chart);
//now that we know where the mouse is, lets use
//ConverToChartValues extension
//it takes a point in pixes and scales it to our chart current scale/values
var p = chart.ConvertToChartValues(mouseCoordinate);
//var series = chart.Series[0];
var closetsPoint = series.ClosestPointTo(p.X, AxisOrientation.X);
if (closetsPoint == null)
return;
xEnd = closetsPoint.X;
vm.MeasureValuePos = (int)(Math.Min(xBegin, xEnd) * vm.PosOfGrid);
vm.MeasureWidthPos = (int)((Math.Abs(xEnd - xBegin) + 1) * vm.PosOfGrid);
}
private double xBegin;
private double xEnd;
private bool isDown = false;
private void UIElement_MouseDown(object sender, MouseButtonEventArgs e)
{
isDown = true;
var vm = viewModel;
var series = vm.SeriesInfos.First(info => info.IsSelected).Series;
var chart = (LiveCharts.Wpf.CartesianChart)sender;
var mouseCoordinate = e.GetPosition(chart);
var p = chart.ConvertToChartValues(mouseCoordinate);
//var series = chart.Series[0];
var closetsPoint = series.ClosestPointTo(p.X, AxisOrientation.X);
if (closetsPoint == null)
{
isDown = false;
return;
}
xBegin = closetsPoint.X;
vm.MeasureValuePos = (int)(xBegin * vm.PosOfGrid);
}
private void UIElement_MouseUp(object sender, MouseButtonEventArgs e)
{
isDown = false;
var vm = viewModel;
var series = vm.SeriesInfos.First(info => info.IsSelected).Series;
var chart = (LiveCharts.Wpf.CartesianChart)sender;
//lets get where the mouse is at our chart
var mouseCoordinate = e.GetPosition(chart);
//now that we know where the mouse is, lets use
//ConverToChartValues extension
//it takes a point in pixes and scales it to our chart current scale/values
var p = chart.ConvertToChartValues(mouseCoordinate);
//for X in this case we will only highlight the closest point.
//lets use the already defined ClosestPointTo extension
//it will return the closest ChartPoint to a value according to an axis.
//here we get the closest point to p.X according to the X axis
//var series = chart.Series[0];
var closetsPoint = series.ClosestPointTo(p.X, AxisOrientation.X);
if (closetsPoint == null)
return;
xEnd = closetsPoint.X;
vm.MeasureValuePos = (int)(Math.Min(xBegin, xEnd) * vm.PosOfGrid);
vm.MeasureWidthPos = (int)((Math.Abs(xEnd - xBegin) + 1) * vm.PosOfGrid);
}
}
}