首页 > 解决方案 > 如何使用 LiveCharts 在 LineSeries 中绑定值属性

问题描述

我想将 WPF 中每个 Line Series 的值绑定到 C# 中 SeriesCollection 类型的值的集合。目前,每行都分配有实心值。如何将其绑定到集合?一个例子是将第一行系列绑定到下面的系列集合。非常感激!

<lvc:CartesianChart>
                    <lvc:CartesianChart.Series>
                        <lvc:LineSeries Title="Pres" Values="4,7,2,9,3" Visibility="{Binding PresVisibility, Converter={StaticResource bvc}}"/>
                        <lvc:LineSeries Title="Temp" Values="6,2,6,3,8" Visibility="{Binding TempVisibility, Converter={StaticResource bvc}}"/>
                        <lvc:LineSeries Title="Flow" Values="7,2,8,3,9" Visibility="{Binding FlowVisibility, Converter={StaticResource bvc}}"/>
                        <lvc:LineSeries Title="Control" Values="6,2,6,3,8" Visibility="{Binding ControlVisibility, Converter={StaticResource bvc}}"/>
                        <lvc:LineSeries Title="Compressor" Values="7,2,8,3,9" Visibility="{Binding CompressorVisibility, Converter={StaticResource bvc}}"/>
                    </lvc:CartesianChart.Series>
                    <lvc:CartesianChart.AxisX>
                        <lvc:Axis Title="Time" Labels="{Binding CurrentLabels}"></lvc:Axis>
                    </lvc:CartesianChart.AxisX>
                    <lvc:CartesianChart.AxisY>
                        <lvc:Axis Name="yAxis" LabelFormatter="{Binding CurrentYFormatter}"></lvc:Axis>
                    </lvc:CartesianChart.AxisY>
                </lvc:CartesianChart>

在 C# 中:

public SeriesCollection PresSeries { get; set; }

PresSeries[0].Values.Add(ScrewState.GetScrewAll().V101.p);
            PresSeries[1].Values.Add(ScrewState.GetScrewAll().V102.p);
            PresSeries[2].Values.Add(ScrewState.GetScrewAll().P1.p);
            PresSeries[3].Values.Add(ScrewState.GetScrewAll().P2.p);
            PresSeries[4].Values.Add(ScrewState.GetScrewAll().P3.p);

标签: c#wpfxamldata-bindinglivecharts

解决方案


当您LineSeries在 XAML 中定义 时,您不必再定义 a SeriesCollection(您只是在 XAML 中完成了 -<CartesianChart.Series>SeriesCollection)。ChartValues在 C# 中定义实际的源集合就足够了(例如,在您的视图模型中):

主窗口.xaml

<lvc:CartesianChart x:Name="CartesianChart">
  <lvc:CartesianChart.DataContext>
    <local:LiveChartsModel />
  </lvc:CartesianChart.DataContext>

  <lvc:CartesianChart.Series>
    <lvc:LineSeries Title="Sine Graph"
                    Values="{Binding SineGraphValues}"
                    Configuration="{Binding DataMapper}"/>
  </lvc:CartesianChart.Series>
</lvc:CartesianChart>

LiveChartsModel.cs

public class LiveChartsModel : INotifyPropertyChanged
{
  public LiveChartsModel()
  {
    // Initialize the binding source
    this.SineGraphValues = new ChartValues<ObservablePoint>();

    // Plot a sine graph
    for (double x = 0; x <= 360; x++)
    {
      var point = new ObservablePoint() 
      { 
        X = x, 
        Y = Math.Sin(x * Math.PI / 180) 
      };

      this.SineGraphValues.Add(point);
    }

    // Setup the data mapper
    this.DataMapper = new CartesianMapper<ObservablePoint>()
      .X(point => point.X)
      .Y(point => point.Y)
      .Stroke(point => point.Y > 0.3 ? Brushes.Red : Brushes.LightGreen)
      .Fill(point => point.Y > 0.3 ? Brushes.Red : Brushes.LightGreen);
  }

  private object dataMapper;   
  public object DataMapper
  {
    get => this.dataMapper;
    set 
    { 
      this.dataMapper = value; 
      OnPropertyChanged();
    }
  }

  public ChartValues<ObservablePoint> SineGraphValues { get; }

  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

推荐阅读