如何解决LiveCharts上的自定义课程?
我试图基于MVVM模式在WPF应用程序上学习和实现LiveChart,但是我很难理解如何正确实现它。 假设我有一个这样的自定义类。我知道这个问题有点令人困惑,但是我很难理解它的工作原理,有人可以给我一个简单的示例,以关于如何基于MVVM模式绘制自定义类的例子来帮助我吗? / p>
ErrorPrt类别
public class ErrorPrt
{
public ErrorPrt()
{
prtName = string.Empty;
Count = -1;
}
public string prtName { get; set; }
public int Count { get; set; }
}
声明
private string[] Labels { get; set; }
public SeriesCollection seriesCollection;
private SeriesCollection SeriesCollection
{
get { return seriesCollection; }
set { seriesCollection = value; OnPropertyChanged("SeriesCollection"); }
}
public SeriesCollection dispalyChart(ErrorPrt[] err)
{
SeriesCollection series = new SeriesCollection();
List<int> vs1 = new List<int>();
foreach (ErrorPrt e in err)
{
vs1.Add(e.Count);
}
series.Add(new ColumnSeries
{
Title = "REPORT",Values = new ChartValues<ErrorPrt> (err)
});
return series;
}
public string[] GetLabels(ErrorPrt[] err)
{
string[] Labels = new string[err.Length];
int j = 0;
foreach(var e in err)
{
Labels[j] = e.prtName;
j++;
}
return Labels;
}
编辑
作为视图模型全局声明的图表值
class BackupStatsviewmodel : INotifyPropertyChanged
{
//OMITTED CODE
ChartValues<DataModel> values = new ChartValues<DataModel>();
private void InitializeBarChartData(ErrorPrt[] arr)
{
for (int i = 0; i < arr.Count(); i++)
values.Add(new DataModel() { Label = $"PRT {arr[i].prtName}",Value = arr[i].Count });
// Initialize the DataModel items
//for (double value = 0; value < 10; value++)
//{
// values.Add(new DataModel() { Label = $"Column {value + 1}",Value = value + 10 });
//}
// Create a labels collection from the DataModel items
this.ColumnLabels = new ObservableCollection<string>(values.Select(dataModel => dataModel.Label));
var dataMapper = new CartesianMapper<DataModel>()
.Y(dataModel => dataModel.Value)
.Fill(dataModel => dataModel.Value > 15.0 ? Brushes.Red : Brushes.Green);
this.ChartDataSets = new SeriesCollection
{
new ColumnSeries
{
Values = values,Configuration = dataMapper
}
};
}
}
}
解决方法
以下示例是一个最小的MVVM示例,该示例基于自定义数据模型绘制ColumnSeries
(条形图):
DataModel.cs
class DataModel : INotifyPropertyChanged
{
private double value;
public double Value
{
get => this.value;
set
{
this.value = value;
OnPropertyChanged();
}
}
private string label;
public string Label
{
get => this.label;
set
{
this.label = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
}
ViewModel.cs
class ViewModel : INotifyPropertyChanged
{
public SeriesCollection ChartDataSets { get; set; }
public ObservableCollection<string> ColumnLabels { get; set; }
public ViewModel()
{
InitializeBarChartData();
}
private void InitializeBarChartData()
{
// Initialize the DataModel items
var values = new ChartValues<DataModel>();
for (double value = 0; value < 10; value++)
{
values.Add(new DataModel() {Label = $"Column {value + 1}",Value = value + 10});
}
// Create a labels collection from the DataModel items
this.ColumnLabels = new ObservableCollection<string>(values.Select(dataModel => dataModel.Label));
// Define a data mapper,which tells the Chart how to extract data from the model
// and how to map it to the corresponding axis. The mapper also allows
// to define a predicate which will be applied to color each data item (Fill,Stroke)
var dataMapper = new CartesianMapper<DataModel>()
.Y(dataModel => dataModel.Value)
.Fill(dataModel => dataModel.Value > 15.0 ? Brushes.Red : Brushes.Green);
this.ChartDataSets = new SeriesCollection
{
new ColumnSeries
{
Values = values,Configuration = dataMapper
}
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
}
MainWindow.xaml
<Window>
<Window.DataContext>
<ViewModel />
</Window.DataContext>
<CartesianChart Series="{Binding ChartDataSets}">
<CartesianChart.AxisX>
<Axis Labels="{Binding ColumnLabels}" />
</CartesianChart.AxisX>
</CartesianChart>
</Window>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。