c# - 如何使用 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);
解决方案
当您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));
}
推荐阅读
- python - Alpha vantage AAPL 价格与其他数据源不一致
- java - 具有名为 Item 的类的 Arraylist
- html - 为什么定义给类的样式不能在元素?
- adobe - 如何在 adobe xd 中打开链接?
- postgresql - PostgreSQL 上的 JdbcChannelMessageStore 性能问题
- python - 为什么使用 datetime.striptime() 为 matplotlib 转换日期不显示整个日期
- html - 如何为我的输入获得一个中间位置并且跨度在左侧
- javascript - 如何使用 HTML/JavaScript 添加多个元素
- node.js - 错误:默认 Firebase 应用已存在。真的吗?
- laravel - 如何不将 Laravel 的 bootstrap/cache/config.php 上传到 AWS [已解决]