We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Trendline slope and intercept are always zero

Just as the title says: I can draw trendlines no problem, but when I try to access the Slope and Intercept properties, they're always zero.

My code produces the attached graph, and the following Debug.WriteLine() output:

Reference: Slope = 0, Intercept = 0
Measurement: Slope = 0, Intercept = 0

Here's the relevant code to add the trendlines and get to Slope and Intercept:

ReferenceScatterSeries.Trendlines.Clear();
MeasurementScatterSeries.Trendlines.Clear();

Trendline Trendline_R = new Trendline()
{
    Label = "Reference Cell",
    IsTrendlineVisible = true,
    Type = TrendlineType.Linear
};
Trendline Trendline_M = new Trendline()
{
    Label = "Measurement Cell",
    IsTrendlineVisible = true,
    Type = TrendlineType.Linear
};

ReferenceScatterSeries.Trendlines.Add(Trendline_R);
MeasurementScatterSeries.Trendlines.Add(Trendline_M);

Debug.WriteLine("Reference: Slope = " + Trendline_R.Slope.ToString() + ", Intercept = " + Trendline_R.Intercept.ToString());
Debug.WriteLine("Measurement: Slope = " + Trendline_M.Slope.ToString() + ", Intercept = " + Trendline_M.Intercept.ToString());

Attachment: Capture_3db2fd00.zip

1 Reply

MK Muneesh Kumar G Syncfusion Team June 17, 2019 06:44 AM UTC

Hi Ben, 
 
Greetings from Syncfusion.  
 
We have analyzed your requirement and you can achieve this by getting trendline’s Slope and Intercept in SfChart’s loaded event as per the below code snippet.  
 
Code snippet 
 
  <syncfusion:SfChart x:Name="Chart" Margin="20" Loaded="Chart_Loaded"> 
 
 
 
 
Trendline Trendline_R, Trendline_M; 
        public MainPage() 
        { 
            this.InitializeComponent(); 
 
            Trendline_R = new Trendline() 
            { 
                Label = "Reference Cell", 
                IsTrendlineVisible = true, 
                Type = TrendlineType.Linear 
            }; 
 
            Trendline_M = new Trendline() 
            { 
                Label = "Measurement Cell", 
                IsTrendlineVisible = true, 
                Type = TrendlineType.Linear 
            }; 
             
            series1.Trendlines.Add(Trendline_R); 
            series2.Trendlines.Add(Trendline_M); 
 
         
        } 
 
        private void Chart_Loaded(object sender, RoutedEventArgs e) 
        { 
            Debug.WriteLine("Reference: Slope = " + Trendline_R.Slope.ToString() + ", Intercept = " + Trendline_R.Intercept.ToString()); 
            Debug.WriteLine("Measurement: Slope = " + Trendline_M.Slope.ToString() + ", Intercept = " + Trendline_M.Intercept.ToString()); 
 
        } 
 
 
Here we get the required details in loaded event, because SfChart needs some time to calculate their trendlines properties.  
 
We have prepared a sample based on this, please find the sample from the following location.  
 
 
Please let us know if you have any other queries.  
 
Regards, 
Muneesh Kumar G. 


Loader.
Live Chat Icon For mobile
Up arrow icon