5

I want to use MSDN charts to represent realtime data i'm getting from a telnet application. For testing purpose i have added a button to alter the chart manually. I manually made the chart and it has 0 to 5 points on the X axis with values different values on the X. The series is named by it's default "Series1".

I tried the following:

chart1.Series["Series1"].Points.ElementAt(0).SetValueY(40); //Nothing happens

chart1.Series["Series1"].Points.ElementAt(1).SetValueXY(1, 20); //Nothing happens

chart1.Series["Series1"].Points[0].SetValueY(40); //Nothing happens

chart1.Series["Series1"].Points.ElementAt(1).YValues.SetValue(10, 0); //Nothing happens

chart1.Series["Series1"].Points.Clear(); //Removes all points like it should.

So how do i change datapoint entries on runtime?

-EDIT- If i modify a point using chart1.Series["Series1"].Points.ElementAt(0).SetValueY(40); and add a point after this with chart1.Series["Series1"].Points.AddXY(1, 40); the modified point does snap into it's modified place. The conclusion is that modifying does change the points Y value but the graph does not get refreshed. The function AddXY() seems to autorefresh. I cannot seem to find a way to call Refresh() manually.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99

4 Answers4

10

Call chart1.Refresh() after changing the value; it will force a redraw of the chart, picking up the new values.

mmathis
  • 1,610
  • 18
  • 29
  • How do i change the value? Is it one of the above i tried? – Madmenyo Feb 21 '14 at 16:22
  • Any of them should work. `chart1.Series["Series1"].Points.ElementAt(1).YValues = new {10, 0};` should also work. Can you see the values get updated in the debugger? – mmathis Feb 21 '14 at 16:41
  • 3
    chart1.refresh does not exist. Which is strange because it is listed as a member here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datavisualization.charting.chart(v=vs.110).aspx – Madmenyo Feb 23 '14 at 20:45
  • 1
    `Refresh()` does exist. Are you using WinForms? And do you see the values get updated when you step through the code in the debugger? – mmathis Feb 24 '14 at 16:38
  • Yes it seems to exist according to MSDN, but my chart1 does not have a refresh method... Chart1.Refresh() simply does not work. I am using win form yes. I have to check back if i actually see the value actually changes, good point. Funny thing is, when using "chart1.Series["Series1"].Points.Clear();" All points disappear on the spot, without refresh. – Madmenyo Feb 24 '14 at 17:24
  • I am really baffled as to why i do not have acces to Refresh(). Anyway after adding a point the modified point snaps in place. So this really is just a refresh issue and for AddXY() this refresh is automatically called. – Madmenyo Feb 25 '14 at 10:16
  • I started over completely, since it was not much work. Now refresh did not pop up in the intellisense but it did function unlike before. I might have added an extra using statement which triggered this: using System.Windows.Forms.DataVisualization.Charting; however i was already using using System.Windows.Forms.DataVisualization; – Madmenyo Feb 25 '14 at 10:58
  • 6
    Was looking for the same thing, the Refresh method actually exists but you have to type it out since autocompletion doesn't show it. – A.J.Bauer Sep 17 '14 at 08:20
1

I've just found out that SetValueY() does not update the maximum interval in the Y axis. Therefore, if your current maximum is 0, it will not show anything higher than 0.

Eddie
  • 11
  • 1
0

I do this:

    public static void Refresh(this Chart chart) // update changed data
    {
        chart.Series[0].Points.AddXY(1, 1);
        chart.Update();
        chart.Series[0].Points.RemoveAt(chart.Series[0].Points.Count-1);
    }

chart1.Refresh();

0

DataTable dtChartDataSource = Input from your side.

foreach (DataColumn dc in dtChartDataSource.Columns)
{
   //a series to the chart
 if (chart.Series.FindByName(dc.ColumnName) == null)
 {
      series = dc.ColumnName;
      chart.Series.Add(series);
      chart.Series[series].ChartType = SeriesChartType.Column;

    foreach (DataRow dr in dtChartDataSource.Rows)
    {
        double dataPoint = 0;
        double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint);

        Yourchart.Series[seriesName].Points.AddXY("customStringsOnAxis", dataPoints);
    }
 }
}

It will add the x axis data and Y axis values to the Column chart.

Hope its helps

Abu Muhammad
  • 421
  • 2
  • 13