Page 1 of 1

.NET2 Problems refreshing charts

Posted: Thu Apr 19, 2007 4:45 am
by 9640436
Hi,

I'm using a timer on my Windows form to refresh the chart every few minutes. This is done by having the timer to call for the following function:

Code: Select all

        public void Reload()
        {
            RegionDataHelper dh = new RegionDataHelper();
            DataTable dt;
            tChart1.AutoRepaint = false;
            tChart1.Aspect.View3D = false;
            
            Steema.TeeChart.Styles.Line priceLine = new Steema.TeeChart.Styles.Line(tChart1.Chart);
            Steema.TeeChart.Styles.Line demandLine = new Steema.TeeChart.Styles.Line(tChart1.Chart);


            priceLine.VertAxis = Steema.TeeChart.Styles.VerticalAxis.Right;
            priceLine.Title = "RRP";
            priceLine.Color = Color.Blue;            
            demandLine.Title = "Demand";
            demandLine.Color = Color.Brown;
            demandLine.LinePen.Width = 3;

            tChart1.Header.Text = RegionName;
            tChart1.Legend.Alignment = Steema.TeeChart.LegendAlignments.Bottom;
            tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MM/yy HH:mm";

            dt = dh.GetData(RegionName);

            foreach (DataRow dr in dt.Rows)
            {
                priceLine.Add(Convert.ToDateTime(dr["settlementdate"].ToString()), Single.Parse(dr["rrp"].ToString()));
                demandLine.Add(Convert.ToDateTime(dr["settlementdate"].ToString()), Single.Parse(dr["totaldemand"].ToString()));
            }

            colourLine1 = new ColorLine(tChart1.Chart);
            colourLine1.Active = true;
            colourLine1.Axis = tChart1.Axes.Bottom;
            colourLine1.AllowDrag = true;
            colourLine1.Value = DateTime.Now.ToOADate();
            tChart1.AutoRepaint = true;
            tChart1.Refresh();
        }
The problem is when the timer invokes the function in subsequent times, it'll add a new legend to the chart. It could be adding new series into the charts as well, but I've yet to test that.

As you could see, I have tried setting AutoRepaint to false but it didn't work.

Please advise. Thanks.

Posted: Thu Apr 19, 2007 10:12 am
by narcis
Hi dave,

Could you please send us a simple example project we can run "as-is" to reproduce the problem here?

You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Thanks in advance.

BTW: Considering the nature of your application, reading the two threads below may be helpful for you.

http://www.teechart.net/support/viewtopic.php?t=5127
http://www.teechart.net/support/viewtopic.php?t=3052

Posted: Mon Apr 23, 2007 7:31 am
by 9640436
Hi Narcis,

I've just sent the files to you. I've put it under my name Poh, Dave is my boss actually.

Cheers.

Posted: Mon Apr 23, 2007 9:53 am
by narcis
Hi Poh,

Thanks for the example.

Your problem happens because you are adding new series to the chart every time the timer event is fired.

You have 2 options here:

1. Remove all chart's series every time the event is fired:

Code: Select all

        public void Reload()
        {
            tChart1.AutoRepaint = false;
            tChart1.Aspect.View3D = false;
            tChart1.RemoveAllSeries();
            Steema.TeeChart.Styles.Line firstLine = new Steema.TeeChart.Styles.Line(tChart1.Chart);
            Steema.TeeChart.Styles.Line secondLine = new Steema.TeeChart.Styles.Line(tChart1.Chart);

            firstLine.Title = "First Line";
            firstLine.Color = Color.Blue;
            secondLine.Title = "Second Line";
            secondLine.Color = Color.Yellow;
            firstLine.FillSampleValues(100);
            secondLine.FillSampleValues(100);
            tChart1.Header.Text = "Test Chart";

            tChart1.AutoRepaint = true;
            tChart1.Invalidate();
            //tChart1.Refresh();
        }
2. If series already exist just clear them:

Code: Select all

        public void Reload()
        {
            tChart1.AutoRepaint = false;
            tChart1.Aspect.View3D = false;

						Steema.TeeChart.Styles.Line firstLine;
						Steema.TeeChart.Styles.Line secondLine;

						if (tChart1.Series.Count == 0)
						{
							firstLine = new Steema.TeeChart.Styles.Line(tChart1.Chart);
							secondLine = new Steema.TeeChart.Styles.Line(tChart1.Chart);
						}
						else
						{
							firstLine = (Steema.TeeChart.Styles.Line)tChart1[0];
							secondLine = (Steema.TeeChart.Styles.Line)tChart1[1];
							firstLine.Clear();
							secondLine.Clear();
						}

            firstLine.Title = "First Line";
            firstLine.Color = Color.Blue;
            secondLine.Title = "Second Line";
            secondLine.Color = Color.Yellow;
            firstLine.FillSampleValues(100);
            secondLine.FillSampleValues(100);
            tChart1.Header.Text = "Test Chart";

            tChart1.AutoRepaint = true;
            tChart1.Invalidate();
            //tChart1.Refresh();
        }

Posted: Tue Apr 24, 2007 12:25 am
by 9640436
Thanks Narcis, it's working great!

Posted: Tue Apr 24, 2007 1:52 am
by 9640436
Oopps, I spoke too soon. In my actual code, I have plotted a vertical line using Colorline function, which was omitted from my sample code.

The additional code is at the end of the code:

Code: Select all

...
            colourLine1 = new ColorLine(tChart1.Chart);
            colourLine1.Active = true;
            colourLine1.Axis = tChart1.Axes.Bottom;
            colourLine1.AllowDrag = false;            
            colourLine1.Value = DateTime.Now.ToOADate();
            tChart1.AutoRepaint = true;
            tChart1.Invalidate();   
I've used tChart1.Series.RemoveAllSeries() as suggested and it seemed to stop the series from replotting, but I'm not sure what I should use to stop the ColorLine from replotting.

Please advise. Thanks.

Posted: Tue Apr 24, 2007 3:09 am
by 9640436
I think I found the solution. I used tChart1.Tools.Clear() and it seemed to do the trick.

Cheers!