var mapper = Mappers.Xy<MeasureModel>()
//.X(model => model.currentTime.Ticks/TimeSpan.FromDays(1).Ticks) //使用 Time.作为X轴的标签
.X(model=>model.Time)
.Y(model => model.Value); // 作为Y轴的值
//全局保存映射器
Charting.For<MeasureModel>(mapper);
//添加三条曲线
OscChart.Series.Add(new LineSeries
{
Values = new ChartValues<MeasureModel> {
new MeasureModel(1,133),
//new MeasureModel(2,533),
//new MeasureModel(3,333),
//new MeasureModel(4,533),
//new MeasureModel(5,322),
},
//ScalesYAt = 0,
Title="张力(N)",
//DataLabels = true
});
OscChart.Series.Add(new LineSeries
{
Values = new ChartValues<MeasureModel> {
new MeasureModel(1,230),
//new MeasureModel(2,330),
//new MeasureModel(3,323),
//new MeasureModel(4,230),
//new MeasureModel(5,130),
},
//ScalesYAt = 0,
Title="压力(kpa)",
//DataLabels = true
});
OscChart.Series.Add(new LineSeries
{
Values = new ChartValues<MeasureModel> {
new MeasureModel(1, 230),
//new MeasureModel(2,330),
//new MeasureModel(3,323),
//new MeasureModel(4,230),
//new MeasureModel(5,130),
},
//ScalesYAt = 0,
Title = "温度(℃)",
//DataLabels = true
});
OscChart.Series.Add(new LineSeries
{
Values = new ChartValues<MeasureModel> {
new MeasureModel(1, 234),
//new MeasureModel(2,300),
//new MeasureModel(3,200),
//new MeasureModel(4,433),
//new MeasureModel(5,200),
},
Title = "液位(%)",
//DataLabels =true
//ScalesYAt = 2
});
OscChart.AxisY.Add(new Axis
{
Foreground = System.Windows.Media.Brushes.DodgerBlue,
Title = "Blue Axis"
});
//OscChart.AxisY.Add(new Axis
//{
// Foreground = System.Windows.Media.Brushes.IndianRed,
// Title = "Red Axis",
// Position = AxisPosition.RightTop
//});
//OscChart.AxisY.Add(new Axis
//{
// Foreground = System.Windows.Media.Brushes.DarkOliveGreen,
// Title = "Green Axis",
// Position = AxisPosition.RightTop
//});
OscChart.AxisX.Add(new Axis
{
DisableAnimations = true,
LabelFormatter = value => value.ToString(),
Separator = new LiveCharts.Wpf.Separator() { Step = 10 }
//Separator = new System.Windows.Controls. Separator
//{
// Step = 1
//}
});
while (worker.CancellationPending==false)
{
var r = new Random();
_trend = r.Next(10, 300);
//更新纵坐标数据
Dispatcher.Invoke(() =>
{
this.Title = (DateTime.Now - DateTime.MinValue.AddYears(100)).TotalSeconds.ToString();
OscChart.Series[0].Values.Add(new MeasureModel(time, _trend));
//OscChart.Series[0].Values.Add(new MeasureModel( _trend));
_trend = r.Next(10, 300);
OscChart.Series[1].Values.Add(new MeasureModel(time, _trend));
_trend = r.Next(10, 300);
OscChart.Series[2].Values.Add(new MeasureModel(time, _trend));
_trend = r.Next(10, 300);
OscChart.Series[3].Values.Add(new MeasureModel(time, _trend));
});
System.Threading.Thread.Sleep(1000);
}