这个是单个 livechart 表格实现效果如下:
遇到的问题:
① 如何把这单个表格已经实现功能了的,加载进主窗口;
② 然后由于主窗口的DataContext已经被一个单例设计的模式类占用,我该如何更新表格,如何绑定表格的数据源;
③ 由于我WPF不是很熟练,因此恳请 码友 们详细的解答一下。
我要把这单个的livechart表格加载到主窗口上,共5个livechart表格,并且5个表格需要可以独立更新表格的,我要实现的效果如下:
窗口代码如下
<Window x:Class="WpfApp_实时显示折线图.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:local="clr-namespace:WpfApp_实时显示折线图"
mc:Ignorable="d"
Title="Window2" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="AntiqueWhite">
<Button Content="测试" Margin="0,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
<Grid Grid.Row="1">
<lvc:CartesianChart Series="{Binding LineSeriesCollection}" LegendLocation="Right">
<lvc:CartesianChart.AxisX>
<lvc:Axis Labels="{Binding Labels}" FontSize="14">
<lvc:Axis.Separator>
<lvc:Separator Step="1" />
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisX>
<lvc:CartesianChart.AxisY>
<lvc:Axis LabelFormatter="{Binding CustomFormatterY}" MaxValue="{Binding AxisYMax}" MinValue="{Binding AxisYMin}" Position="LeftBottom">
</lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</Grid>
</Grid>
</Window>
窗口后台代码:
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Windows;
namespace WpfApp_实时显示折线图
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window
{
private Mode Modes = new Mode();
public Window2()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = Modes;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Modes.OnClick();
}
public class Mode : ViewModelBase
{
public List<string> Labels { get; set; }
public SeriesCollection LineSeriesCollection { get; set; }
private double axisYMax;
public double AxisYMax
{
get { return axisYMax; }
set { axisYMax = value; this.OnPropertyChanged("AxisYMax"); }
}
private double axisYMin;
public double AxisYMin
{
get { return axisYMin; }
set { axisYMin = value; this.OnPropertyChanged("AxisYMin"); }
}
private Random Randoms = new Random();
public Func<double, string> CustomFormatterX { get; set; }
public Func<double, string> CustomFormatterY { get; set; }
//绑定的X轴数据
private ChartValues<double> ValueList { get; set; }
private string CustomFormattersY(double val)
{
return string.Format("{0} mm", val);
}
public void OnClick()
{
int yValue = Randoms.Next(2, 1000);
//向图表中添加数据
ValueList.Add(yValue);
//这里如果移除数组,图表曲线会原地起伏,就没有X轴移动的动画效果了
if (ValueList.Count > 11)
{
ValueList.RemoveAt(0);
}
}
public Mode()
{
Labels = new List<string>();
AxisYMax = 1000 + 100;
double max = AxisYMax - 100;
double warningMax = 750;
double guige = 550;
double warningMin = 350;
AxisYMin = 0 - 100;
double min = AxisYMin + 100;
ValueList = new ChartValues<double>();
LineSeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = $"上限:{max} mm",
Values = new ChartValues<double> { max, max, max, max, max, max, max, max, max, max, max},
Stroke = System.Windows.Media.Brushes.Red,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"预警线:{warningMax} mm",
StrokeDashArray = new System.Windows.Media.DoubleCollection {2},
Values = new ChartValues<double> { warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax},
Stroke = System.Windows.Media.Brushes.Blue,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"规格:{guige} mm",
Values = new ChartValues<double> { guige, guige, guige, guige, guige, guige, guige, guige, guige, guige, guige},
Stroke = System.Windows.Media.Brushes.Green,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"预警线:{warningMin} mm",
StrokeDashArray = new System.Windows.Media.DoubleCollection {2},
Values = new ChartValues<double> { warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin, warningMin},
Stroke = System.Windows.Media.Brushes.Blue,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
},
new LineSeries
{
Title = $"下限:{min} mm",
Values = new ChartValues<double> { min, min, min, min, min, min, min, min, min, min, min},
Stroke = System.Windows.Media.Brushes.Red,
PointGeometry = null,
Fill = System.Windows.Media.Brushes.LightBlue,
}
};
CustomFormatterY = CustomFormattersY;
LineSeries lineseries = new LineSeries();
lineseries.Title = "极耳中心距";
//lineseries.Stroke = System.Windows.Media.Brushes.Purple;
lineseries.DataLabels = true;
lineseries.Values = ValueList;
LineSeriesCollection.Add(lineseries);
}
}
}
}
属性更新 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp_实时显示折线图
{
public class ViewModelBase
{
public virtual event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 属性值变化时发生
/// </summary>
/// <param name="propertyName"></param>
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// 属性值变化时发生
/// </summary>
/// <param name="propertyName"></param>
protected virtual void OnPropertyChanged<T>(System.Linq.Expressions.Expression<Func<T>> propertyExpression)
{
var propertyName = (propertyExpression.Body as System.Linq.Expressions.MemberExpression).Member.Name;
this.OnPropertyChanged(propertyName);
}
}
}