4 Replies - 23893 Views - Last Post: 28 March 2012 - 02:06 PM Rate Topic: -----

#1 solorize  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 28-February 12

How to reset / clear an existing chart to display new data

Posted 26 March 2012 - 02:42 PM

Hi,

I currently have a chart that plots lines of a changing value every time it is called via a timer.

Everything is running as I expected and I am happy, but now I want to be able to press a
[STOP] button which will stop the timer and stop the graph from updating, (which I have coded and is now
working OK), BUT I then want the code under this [STOP] button to clear everything from the graph,
so when I re-start the timer, the graph is cleared and starts plotting fresh points on a clean graph.

The problem I currently have is that when I press the [STOP] button and then press the [START] button,
the new lines that get plotted are "added" to the existing lines that are already on the chart, instead
of having a new "blank" chart, with no data on it.

So what I am really asking is does anyone know if it is possible to reset a chart clearing everything
off it so it is ready to start displaying fresh data?

I was hoping it was something like: "Chart1.Clear" or something simple like that, but I can't seem
to find anything that will do it.

Thanks in Advance

Mark

Is This A Good Question/Topic? 0
  • +

Replies To: How to reset / clear an existing chart to display new data

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: How to reset / clear an existing chart to display new data

Posted 26 March 2012 - 02:51 PM

you can try with
Chart1.Series.Clear

OR
Chart1.ChartAreas.Clear

Can you show the code? how you drawing chart?
Was This Post Helpful? 0
  • +
  • -

#3 solorize  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 28-February 12

Re: How to reset / clear an existing chart to display new data

Posted 27 March 2012 - 02:11 PM

Hi sela007,

Thank you for replying.

I have shown part of my code below, which has been extracted from the whole
of my program, hopefully it shows you enough of how I am creating the chart.

I have excluded the Start and Stop buttons code, as all these do is Enable and Disable
the timer.

I did try using both the bits of code you stated in your post, the 1st one
"Chart3.Series.Clear" blanks out the chart so nothing at all is displayed, but when
I then press the start button the chart comes back showing all the previous lines plotted
and appends the new data onto it. So it does not fully clear everything off.

The 2nd bit of code "Chart3.ChartAreas.Clear" just throws the following error:

{"A chart element with the name 'ChartArea1' could not be found in the 'ChartAreaCollection'."}

Which is weird as if I look in my Chart3 Properties under ChartAreas there is a one called "ChartArea1"
please see attached image. Attached Image


Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1

    'Variable for Temperature / Series for plotting to Chart3
    Dim TempHiDotSeries As New Series
    Dim TempDotSeries As New Series
    Dim TempLowDotSeries As New Series
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        'Calls TemperaureGraphChart code
        Call TemperatureGraphChart()

    End Sub

	Private Sub TemperatureGraphChart()
        'Note below 3 lines for Variables have been inserted under Form1 Class at top of code so it doesn't mess up chart by
        'adding incremental series to legend. It has to be under Form1 Class so this Sub can see it!
        'Dim TempHiDotSeries As New Series
        'Dim TempDotSeries As New Series
        'Dim TempLowDotSeries As New Series


        'Sets type of Chart
        TempHiDotSeries.ChartType = SeriesChartType.Line
        TempDotSeries.ChartType = SeriesChartType.Line
        TempLowDotSeries.ChartType = SeriesChartType.Line



        'Create a new series and add data points to it.
        TempHiDotSeries.Name = "Today's Max Temp: " + Label27.Text & " " + Label15.Text
        TempDotSeries.Name = "Current Temp: " + Label3.Text & " " + Label15.Text
        TempLowDotSeries.Name = "Today's Min Temp: " + Label29.Text & " " + Label15.Text

        'Error Handler
        On Error Resume Next

        'Sets Y Axis Title, to Temperature and the Temperature unit from Label15
        Chart3.ChartAreas(0).AxisY.Title = "Temperature " + Label15.Text


        'plots the windspeed and adds Timestamp
        TempHiDotSeries.Points.AddXY(TimeString, Label27.Text)
        TempDotSeries.Points.AddXY(TimeString, Label3.Text)
        TempLowDotSeries.Points.AddXY(TimeString, Label29.Text)

        'resets AutoValues and resizes chart to the largest value.
        Chart3.ResetAutoValues()

        'Add the series to the Chart3 control.
        Chart3.Series.Add(TempHiDotSeries)
        Chart3.Series.Add(TempDotSeries)
        Chart3.Series.Add(TempLowDotSeries)
    End Sub
	End Class

Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: How to reset / clear an existing chart to display new data

Posted 27 March 2012 - 02:25 PM

please try this before going deeper in the code
chart3.Series[0].Points.Clear();

or
TempHiDotSeries.Points.Clear()


you have 3 'series'.. so try to clear all three series.

ofcourse use () instead []..
chart3.Series(0).Points.Clear()
chart3.Series(1).Points.Clear()
chart3.Series(2).Points.Clear()

This post has been edited by sela007: 27 March 2012 - 02:28 PM

Was This Post Helpful? 1
  • +
  • -

#5 solorize  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 28-February 12

Re: How to reset / clear an existing chart to display new data

Posted 28 March 2012 - 02:06 PM

View Postsela007, on 27 March 2012 - 02:25 PM, said:

you have 3 'series'.. so try to clear all three series.

ofcourse use () instead []..
chart3.Series(0).Points.Clear()
chart3.Series(1).Points.Clear()
chart3.Series(2).Points.Clear()


Thanks very much for your help, the above lines of code have done the trick
and I can now get my chart to clear correctly. :)

Regards,

Mark
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1