簡體   English   中英

從數據表動態更新 LiveCharts

[英]Update LiveCharts from datatable dynamically

我已經閱讀文檔好幾天了,但無論我嘗試什么,我都無法讓它工作。 我有基本行圖,並希望顯示為圖形所花費的時間。 我的酒吧標題和價值不斷變化(添加更多項目)。 我可以用我當前的代碼添加欄,但我無法為每個添加的欄添加標題。 只有第一個標題/第一個欄標題可見,所有其他/即將到來的標題都不可見。

如何以適當的方式添加標題和價值? (我已經熟悉文檔https://lvcharts.net/App/examples/v1/wf/Basic%20Row

這是我的代碼(您可以從注釋掉的部分中看到已經嘗試過的內容):

    public static SeriesCollection SeriesCollection { get; set; }
    public static string[] Labels { get; set; }
    public static List<string> LabelsList { get; set; }
    public static Func<double, string> Formatter { get; set; }

    public AppUsageBarGraph()
    {
        InitializeComponent();

        LabelsList = new List<string>();

        SeriesCollection = new SeriesCollection
        {
            new RowSeries
            {
                Values = new ChartValues<double> { },
                DataLabels = true
            }
        };

        DataContext = this;
    }

    public static void UpdateChart()
    {
        SeriesCollection[0].Values.Clear();
        LabelsList.Clear();

        //Labels = MainProcess.ActivityLogGrouped.Rows.Cast<DataRow>().Select(row => row["Window Title"].ToString()).ToArray();

        foreach (DataRow row in MainProcess.ActivityLogGrouped.Rows)
        {
            SeriesCollection[0].Values.Add(Convert.ToDouble(row["Time Spent"]));
            //SeriesCollection[0]. = row["Time Spent"].ToString());
            LabelsList.Add(row["Window Title"].ToString());
        }

        //MessageBox.Show(Labels[0].ToString());

        Labels = LabelsList.ToArray(); 


        //foreach (var item in Labels)
        //{
        //    MessageBox.Show(item);
        //}

        //Labels = new[]
        //        {
        //              "Shea Ferriera",
        //              "Maurita Powel",
        //              "Scottie Brogdon",
        //              "Teresa Kerman",
        //              "Nell Venuti",
        //              "Anibal Brothers",
        //              "Anderson Dillman"
        //           };

        //Formatter = value => value.ToString("N");
    }

關鍵是使用ObservableCollection<string>而不是string[]

我還建議使用模型來封裝實際的圖表數據點。 為此,我引入了類DataModel

以下示例顯示如何將值和標簽動態綁定到圖表。 我應該說將所有內容都設為public static是一種非常糟糕的代碼設計。

主窗口.xaml

<Window>
  <Window.DataContext>
    <ViewModel />
  </Window.DataContext>

  <wpf:CartesianChart Height="500">
    <CartesianChart.Series>
      <RowSeries Values="{Binding ChartModel.RowSeries}"
                 Configuration="{Binding ChartModel.RowSeriesConfiguration}"/>
    </CartesianChart.Series>
    <CartesianChart.AxisY>
      <Axis Labels="{Binding ChartModel.RowSeriesLabels}" />
    </CartesianChart.AxisY>
  </CartesianChart>
</Window>

視圖模型.cs

public class ViewModel : INotifyPropertyChanged
{
  public ViewModel()
  {
    this.ChartModel = new ChartModel();
  }

  public void UpdateChart()
  {
    foreach (DataRow row in MainProcess.ActivityLogGrouped.Rows)
    {
      if (double.TryParse(row["Time Spent"], out double value)
      {
        string label = row["Window Title"];
        var newDataModel = new DataModel(value, label);

        this.ChartModel.RowSeries.Add(newDataModel);            
        this.ChartModel.RowSeriesLabels.Add(newDataModel.Label);
      }
    }
  }

  public ChartModel ChartModel { get; set; }

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

圖表模型.cs

public class ChartModel : INotifyPropertyChanged
{
  public ChartModel()
  {
    // Initialize chart
    this.RowSeries = new ChartValues<DataModel>()
    {
      new DataModel(20, "Shea Ferriera"),
      new DataModel(100, "Maurita Powel"),
      new DataModel(60, "Scottie Brogdon"),
    };

    // Create labels
    this.RowSeriesLabels = new ObservableCollection<string>();
    foreach (DataModel dataModel in this.RowSeries)
    {
      this.RowSeriesLabels.Add("dataModel.Label");
    }

    // DatModel to value mapping
    this.RowSeriesConfiguration = new CartesianMapper<DataModel>()
      .X(dataModel => dataModel.Value);
  }

  public CartesianMapper<DataModel> RowSeriesConfiguration { get; set; }
  public ChartValues<DataModel> RowSeries { get; set; }
  public ObservableCollection<string> RowSeriesLabels { get; set; }

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

數據模型.cs

public class DataModel
{
  public DataModel(double value, string label)
  {
    this.Value = value;
    this.Label = label;
  }

  public double Value { get; set; }
  public string Label { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM