سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

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();

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top