需求,我该如何在以下代码中修改,增加一个“当主窗口传递一个值过来时,用户控件判断这个是否超过了设置的范围,超过了设置的范围该值的点就变成红色”,我该如何实现这一个功能
这是用户控件的前台代码
<UserControl x:Class="IZeroVisionVM.LineChart_UserControl_LeftOutSide"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Loaded="UserControl_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Label Content="左极耳边距" Margin="5,0,0,1" FontSize="10"></Label>
</Grid>
<Grid Grid.Row="1">
<lvc:CartesianChart x:Name="MyChart" 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}">
<lvc:Axis.Separator>
<lvc:Separator Step="0.5" />
</lvc:Axis.Separator>
</lvc:Axis>
</lvc:CartesianChart.AxisY>
</lvc:CartesianChart>
</Grid>
</Grid>
</UserControl>
这是用户控件的后台代码
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using Rapid.Core.IZVKernal;
using Rapid.UI.IZVKernal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace IZeroVisionVM
{
/// <summary>
/// LeftOutSide_UserControl.xaml 的交互逻辑
/// </summary>
public partial class LineChart_UserControl_LeftOutSide : UserControl
{
public double Max;
public double Min;
public double Standard;
public double LowerWarning;
public double UpperWarning;
public List<string> Labels { get; set; }
public SeriesCollection LineSeriesCollection { get; set; }
public Func<double, string> CustomFormatterY { get; set; }
public LineSeries lineSeries_上限;
public LineSeries lineSeries_上警告线;
public LineSeries lineSeries_标准;
public LineSeries lineSeries_下警告线;
public LineSeries lineSeries_下限;
public LineSeries lineseries;
public LineChart_UserControl_LeftOutSide()
{
InitializeComponent();
}
//绑定的X轴数据
public static ChartValues<double> ValueList { get; set; }
public void OnClick(double distance)
{
try
{
//向图表中添加数据
ValueList.Add(distance);
//这里如果移除数组,图表曲线会原地起伏,就没有X轴移动的动画效果了
if (ValueList.Count > 11)
{
ValueList.RemoveAt(0);
}
}
catch
{
}
}
public static readonly DependencyProperty UpperLimitValueProperty =
DependencyProperty.Register("UpperLimitValue", typeof(string), typeof(LineChart_UserControl_LeftOutSide));
public string UpperLimitValue
{
get { return (string)GetValue(UpperLimitValueProperty); }
set
{
SetValue(UpperLimitValueProperty, value);
//if (Max != Convert.ToSingle(UpperLimitValue))
//{
Max = Convert.ToSingle(UpperLimitValue);
UpdateDisplay();
UpdateChartLineSeries_限制(lineSeries_上限, "上限", 0);
//}
//else
//{
// UpdateDisplay();
//}
}
}
public static readonly DependencyProperty LowerLimitValueProperty =
DependencyProperty.Register("LowerLimitValue", typeof(string), typeof(LineChart_UserControl_LeftOutSide));
public string LowerLimitValue
{
get { return (string)GetValue(LowerLimitValueProperty); }
set
{
SetValue(LowerLimitValueProperty, value);
//if (Min != Convert.ToDouble(LowerLimitValue))
//{
Min = Convert.ToSingle(LowerLimitValue);
UpdateDisplay();
UpdateChartLineSeries_限制(lineSeries_下限, "下限", 4);
//}
//else
//{
// UpdateDisplay();
//}
}
}
public static readonly DependencyProperty StandardValueProperty =
DependencyProperty.Register("StandardValue", typeof(string), typeof(LineChart_UserControl_LeftOutSide));
public string StandardValue
{
get { return (string)GetValue(StandardValueProperty); }
set
{
SetValue(StandardValueProperty, value);
//if (Standard != Convert.ToDouble(StandardValue))
//{
Standard = Convert.ToSingle(StandardValue);
UpdateDisplay();
UpdateChartLineSeries_规格(lineSeries_标准, 2);
//}
//else
//{
// UpdateDisplay();
//}
}
}
public static readonly DependencyProperty UpperWarningValueProperty =
DependencyProperty.Register("UpperWarningValue", typeof(string), typeof(LineChart_UserControl_LeftOutSide));
public string UpperWarningValue
{
get { return (string)GetValue(UpperWarningValueProperty); }
set
{
SetValue(UpperWarningValueProperty, value);
//if (UpperWarning != Convert.ToDouble(UpperWarningValue))
//{
UpperWarning = Convert.ToSingle(UpperWarningValue);
UpdateDisplay();
UpdateChartLineSeries_警告(lineSeries_上警告线, 1);
//}
//else
//{
// UpdateDisplay();
//}
}
}
public static readonly DependencyProperty LowerWarningValueProperty =
DependencyProperty.Register("LowerWarningValue", typeof(string), typeof(LineChart_UserControl_LeftOutSide));
public string LowerWarningValue
{
get { return (string)GetValue(LowerWarningValueProperty); }
set
{
SetValue(LowerWarningValueProperty, value);
//if (LowerWarning != Convert.ToDouble(LowerWarningValue))
//{
LowerWarning = Convert.ToSingle(LowerWarningValue);
UpdateDisplay();
UpdateChartLineSeries_警告(lineSeries_下警告线, 3);
//}
//else
//{
// UpdateDisplay();
//}
}
}
private void UpdateDisplay()
{
UpdateChartAxes(Convert.ToSingle(UpperLimitValue), Convert.ToSingle(Convert.ToDouble(LowerLimitValue)));
MyChart.Update(true);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Mode();
this.DataContext = this;
}
private string CustomFormattersY(double val)
{
return string.Format("{0} mm", val);
}
public void UpdateChartAxes(double max, double min)
{
var yMax = max + 1;
var yMin = min - 1;
MyChart.AxisY[0].MaxValue = (double)Math.Round(yMax,2);
MyChart.AxisY[0].MinValue = (double)Math.Round(yMin,2);
}
private void UpdateChartLineSeries_限制(LineSeries lineSeries0, string name, int item)
{
if (item == 0)
{
LineSeriesCollection.Remove(lineSeries0);
lineSeries_上限 = lineSeries_限制(Convert.ToDouble(UpperLimitValue), name);
LineSeriesCollection.Insert(0, lineSeries_上限);
}
else
{
LineSeriesCollection.Remove(lineSeries0);
lineSeries_下限 = lineSeries_限制(Convert.ToDouble(LowerLimitValue), name);
LineSeriesCollection.Insert(4, lineSeries_下限);
}
}
private void UpdateChartLineSeries_警告(LineSeries lineSeries0, int item)
{
if (item == 1)
{
LineSeriesCollection.Remove(lineSeries0);
lineSeries_上警告线 = lineSeries_预警(Convert.ToDouble(UpperWarningValue));
LineSeriesCollection.Insert(1, lineSeries_上警告线);
}
else
{
LineSeriesCollection.Remove(lineSeries0);
lineSeries_下警告线 = lineSeries_预警(Convert.ToDouble(LowerWarningValue));
LineSeriesCollection.Insert(3, lineSeries_下警告线);
}
}
private void UpdateChartLineSeries_规格(LineSeries lineSeries0, int item)
{
LineSeriesCollection.Remove(lineSeries0);
lineSeries_标准 = lineSeries_规格(Convert.ToDouble(StandardValue));
LineSeriesCollection.Insert(2, lineSeries_标准);
}
public void Mode()
{
Labels = new List<string>();
Max = SolutionViewModel.GetInstance().SysInfo.LeftOutSideMarginUpperLimit;
Min = SolutionViewModel.GetInstance().SysInfo.LeftOutSideMarginLowerLimit;
Standard = SolutionViewModel.GetInstance().SysInfo.LeftOutSideMarginStandard;
LowerWarning = SolutionViewModel.GetInstance().SysInfo.LeftOutSideMarginLowerWarningLine;
UpperWarning = SolutionViewModel.GetInstance().SysInfo.LeftOutSideMarginUpperWarningLine;
UpdateChartAxes(Max, Min);
double max = Max;
double warningMax = UpperWarning;
double guige = Standard;
double warningMin = LowerWarning;
double min = Min;
LineChart_UserControl_LeftOutSide.ValueList = new ChartValues<double>();
LineSeriesCollection = new SeriesCollection();
CustomFormatterY = CustomFormattersY;
lineSeries_上限 = lineSeries_限制(max, "上限");
LineSeriesCollection.Add(lineSeries_上限);
lineSeries_上警告线 = lineSeries_预警(warningMax);
LineSeriesCollection.Add(lineSeries_上警告线);
lineSeries_标准 = lineSeries_规格(guige);
LineSeriesCollection.Add(lineSeries_标准);
lineSeries_下警告线 = lineSeries_预警(warningMin);
LineSeriesCollection.Add(lineSeries_下警告线);
lineSeries_下限 = lineSeries_限制(min, "下限");
LineSeriesCollection.Add(lineSeries_下限);
lineseries = new LineSeries();
lineseries.Title = "左极耳边距";
lineseries.Values = LineChart_UserControl_LeftOutSide.ValueList;
LineSeriesCollection.Add(lineseries);
MyChart.Background = Brushes.LightBlue;
}
private LineSeries lineSeries_限制(double max, string name)
{
LineSeries lineseries1 = new LineSeries();
lineseries1.Title = $"{name}:{max} mm";
lineseries1.Values = new ChartValues<double> { max, max, max, max, max, max, max, max, max, max, max };
lineseries1.Stroke = System.Windows.Media.Brushes.Red;
lineseries1.PointGeometry = null;
lineseries1.Fill = System.Windows.Media.Brushes.LightBlue;
return lineseries1;
}
private LineSeries lineSeries_预警(double warningMax)
{
LineSeries lineseries2 = new LineSeries();
lineseries2.Title = $"预警线:{warningMax} mm";
lineseries2.StrokeDashArray = new System.Windows.Media.DoubleCollection { 2 };
lineseries2.Values = new ChartValues<double> { warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax, warningMax };
lineseries2.Stroke = System.Windows.Media.Brushes.Yellow;
lineseries2.PointGeometry = null;
lineseries2.Fill = System.Windows.Media.Brushes.LightBlue;
return lineseries2;
}
private LineSeries lineSeries_规格(double guige)
{
LineSeries lineseries2 = new LineSeries();
lineseries2.Title = $"规格:{guige} mm";
lineseries2.Values = new ChartValues<double> { guige, guige, guige, guige, guige, guige, guige, guige, guige, guige, guige };
lineseries2.Stroke = System.Windows.Media.Brushes.Green;
lineseries2.PointGeometry = null;
lineseries2.Fill = System.Windows.Media.Brushes.LightBlue;
return lineseries2;
}
}
}