2016-11-04 69 views
4

我最近遇到一个名为LiveChart的工具,并决定对它进行测试。如何正确更新我的图表值? (实时)

不幸的是,我一直在解决如何实时更新图表值的问题。我很确定有这样一个干净而正确的方法,但我无法找到它。

我希望能够通过private void或按钮更新值。

在我的代码中,我用ToolStripMenu对它进行了测试。

[编号]:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using LiveCharts; 
using LiveCharts.WinForms; 
using LiveCharts.Wpf; 
using PokeShowdown_AccStats_T.Properties; 
using LiveCharts.Defaults; 

namespace PokeShowdown_AccStats_T 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      //int val1 = int.Parse(Settings.Default.Value1); 

      var value1 = new ObservableValue(3); 
      var value2 = new ObservableValue(7); 
      var value3 = new ObservableValue(10); 
      var value4 = new ObservableValue(2); 

      //value1.Value = 5; 

      cartesianChart1.Series.Add(new LineSeries 
      { 
       Values = new ChartValues<ObservableValue> { value1, value2, value3, value4 }, 
       StrokeThickness = 4, 
       StrokeDashArray = new System.Windows.Media.DoubleCollection(20), 
       Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(107, 185, 69)), 
       Fill = System.Windows.Media.Brushes.Transparent, 
       LineSmoothness = 0, 
       PointGeometry = null 
      }); 



      cartesianChart1.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(34, 46, 49)); 

      cartesianChart1.AxisX.Add(new Axis 
      { 
       IsMerged = true, 
       Separator = new Separator 
       { 
        StrokeThickness = 1, 
        StrokeDashArray = new System.Windows.Media.DoubleCollection(2), 
        Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(64, 79, 86)) 
       } 
      }); 
      cartesianChart1.AxisY.Add(new Axis 
      { 
       IsMerged = true, 
       Separator = new Separator 
       { 
        StrokeThickness = 1.5, 
        StrokeDashArray = new System.Windows.Media.DoubleCollection(4), 
        Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(64, 79, 86)) 
       } 
      }); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void changeValue1ToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Settings.Default.Value1 = "10"; 
      Settings.Default.Save(); 
      this.Text = Settings.Default.Value1; 

     } 

     private void changeValue1To3ToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Settings.Default.Value1 = "3"; 
      Settings.Default.Save(); 
      this.Text = Settings.Default.Value1; 

     } 
    } 
} 
+0

表支持数据绑定。使用数据绑定和更新数据源。 –

回答

6

实况图表试图保持它的简单。逻辑是使用您需要绘制的类型的泛型集合,然后像添加/删除或更新此集合中的任何元素一样简单,然后您的图表将被更新。

要回答你的问题,你通常需要:

public partial class Form1 : Form 
{ 
    private ObservableValue value1; 

    public Form1() 
    { 
     InitializeComponent(); 

     //int val1 = int.Parse(Settings.Default.Value1); 

     value1 = new ObservableValue(3); 
     //... 

     cartesianChart1.Series.Add(new LineSeries 
     { 
      Values = new ChartValues<ObservableValue> { value1, ... }, 
     }); 
    } 

    private void changeValue1ToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     value1.Value = 10; 
     Settings.Default.Value1 = "10"; 
     Settings.Default.Save(); 
     this.Text = Settings.Default.Value1; 

    } 
} 

那么库将处理动画和更新

enter image description here

2

注:的问题是关于LiveCharts。但是这个答案是根据MSChart发布的。要查看有关LiveCharts的答案,请参阅其他答案。

图表支持数据绑定。使用数据绑定和更新数据源,然后刷新图表。例如:

DataTable table = new DataTable(); 
Random random = new Random(); 
private void Form1_Load(object sender, EventArgs e) 
{ 
    table.Columns.Add("X", typeof(int)); 
    table.Columns.Add("Y", typeof(int)); 
    for (int i = 0; i < 10; i++) 
     table.Rows.Add(i+1, random.Next(100)); 
    chart1.Series[0].ChartType = 
     System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column; 
    chart1.Series[0].XValueMember = "X"; 
    chart1.Series[0].YValueMembers = "Y"; 
    chart1.DataSource = table; 
    chart1.ChartAreas[0].AxisX.Interval = 1; 
    chart1.ChartAreas[0].AxisX.Minimum = 0; 
    chart1.ChartAreas[0].AxisX.Maximum = 10; 
    chart1.ChartAreas[0].AxisY.Interval = 10; 
    chart1.ChartAreas[0].AxisY.Minimum = 0; 
    chart1.ChartAreas[0].AxisY.Maximum = 100; 
    chart1.DataBind(); 
    var timer = new Timer() { Interval= 300}; 
    timer.Tick += timer_Tick; 
    timer.Start(); 
} 
void timer_Tick(object sender, EventArgs e) 
{ 
    for (int i = 0; i < 10; i++) 
     table.Rows[i][1]= random.Next(100); 
    chart1.DataBind(); 
} 

enter image description here

+0

这不是OP所要求的,他是要求相邻实时图表 –

+0

@ bto.rdz似乎我错过了,并错误地删除了编辑中的livecharts标记。是的,问题是关于活图。 –

+0

@bto另外你/其他任何人downvoted的答案不需要donwvote答案只是淡化它。这篇文章对于那些想要使用mschart进行技巧的读者会很有用。你可以发布一个好的答案,而不需要淡化其他答案。 –