LiveCharts上的自定义课程?

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?