Draw Trend Line to Chart Extents

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
JonM
Newbie
Newbie
Posts: 23
Joined: Tue Sep 14, 2004 4:00 am
Location: UK

Draw Trend Line to Chart Extents

Post by JonM » Mon Nov 09, 2015 11:04 am

Hi Guys

I am plotting a linear trend line with this:

Code: Select all

TrendFunction trendFunction =new TrendFunction();

trendLine = new Line(chart.Chart) { Function = trendFunction, DataSource = series };
Which is working nicely but the line is only drawn to the extents of the data in the series.

Is it possible for the trend line to continue so that either end touches the axes/chart extents? Similar to plotting with slope and Y intercept as opposed to a start and end point?

Many Thanks
J

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Draw Trend Line to Chart Extents

Post by Christopher » Tue Nov 10, 2015 9:02 am

Hello!

There's a similar question asked in ActiveX here.

If the given answer is not enough detail for you, please let us know and we can get a quick example together for you.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

JonM
Newbie
Newbie
Posts: 23
Joined: Tue Sep 14, 2004 4:00 am
Location: UK

Re: Draw Trend Line to Chart Extents

Post by JonM » Tue Nov 10, 2015 9:44 am

Hi Christopher

Thank you for getting back to me.

I already have some maths to calculate the slope and intercept - so that's all okay.

My reason for using TrendFunction is to move away from our own maths and rely on the TeeChart library - so it seems a shame to use a combination of both.

Please can you advise me further or create a sample on how best to achieve the trend line so it continues to draw beyond the data to the extents of the chart/to Y axis?

Whether a combination of TrendFunction and some of our own maths - perhaps to calculate and add some start/end points to the results of TrendFunction?

Or perhaps just go back to using our own maths?

Ideally I was hoping to stick with just using TrendFunction and there be a property somewhere that says "Continue Line".

Many Thanks
J

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Draw Trend Line to Chart Extents

Post by Christopher » Tue Nov 10, 2015 5:12 pm

You're welcome J.

There is no ContinueLine() method at present, but here is my attempt at one:

Code: Select all

    private void InitializeChart()
    {
      Line line1 = null, line2 = null;

      Action ContinueLine = () =>
      {
        SeriesXYPoint point1 = line2[0];
        SeriesXYPoint point2 = line2[1];

        double diffX = point1.X - point2.X;
        double diffY = point1.Y - point2.Y;
        double diff = diffX / diffY;

        SeriesXYPoint point3 = new SeriesXYPoint(line2, line2.Count - 1);
        point3.X = 2000;
        point3.Y = 2000 * diff;

      };

      tChart1.Aspect.View3D = false;
      line1 = new Line(tChart1.Chart);
      line1.FillSampleValues(1000);

      line2 = new Line(tChart1.Chart);
      TrendFunction trend = new TrendFunction();
      trend.UseDecimals = true;
      line2.Function = trend;
      line2.DataSource = line1;

      ContinueLine();
    }
I hope you find this code of some use.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

JonM
Newbie
Newbie
Posts: 23
Joined: Tue Sep 14, 2004 4:00 am
Location: UK

Re: Draw Trend Line to Chart Extents

Post by JonM » Fri Nov 13, 2015 3:58 pm

Thank you - your code sample helped, particularly using SeriesXYPoint to change the line points.

I took a different approach with the maths:

As we know the X values of the new points (i.e. the right or left edge of the chart) I used the slope and Y intercept to calculate the Y values.

y = mx + c

Post Reply