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

ploting a target horizontal line on a line chart

Hello ,

I have a line chart graph.For every such graph i wish to draw a horizontal line indicating the target for that particular chart data.Is there any property from which i can acheive this requirement?

5 Replies

LA Lavanya Anaimuthu Syncfusion Team August 31, 2018 05:26 AM UTC

Hi Urvashi,  
  
Greetings from Syncfusion.  
  
We have analyzed your requirement and it can be achieved by using HorizontalLineAnnotation. You can learn more on this from our UG document below link.  
  
  
Also we have prepared a sample for your requirement and it can be downloaded from the below link. 
  
  
Hope this helps.  
  
Thanks,  
Lavanya Anaimuthu.


UR Urvashi August 31, 2018 05:38 AM UTC

Hi Lavnya,

Thanks for your prompt reply . It partially achieves my requirement. Whwn i show this goal  / Target line as shown in sample 100 is the goal.Can i edit that text and style it according to my requirement .I need to show a string value there,while here its shows a numeric value with default background color.


LA Lavanya Anaimuthu Syncfusion Team August 31, 2018 10:02 AM UTC

Hi Urvashi,   
  
We can achieved you requirement by using Text and LabelStyle properties, which are available in HorizontalLineAnnotation. Also we have modified the sample for your requirement and it can be downloaded from the link below  
  
  
Please let us know if you have any other queries. 
  
Thanks,  
Lavanya Anaimuthu. 



UR Urvashi August 31, 2018 10:11 AM UTC

Hi Lavanya,

I want to show the text at an axis level.Meaning 100 is the goal line so,the text which is shown on y-axis,I wish to edit /show my updated text there.The sample text is coming above line.and i cant put the text on the left side before the Horizontal Line Annotation.I hope you're getting my point.


MP Michael Prabhu M Syncfusion Team September 3, 2018 11:11 AM UTC

Hi Urvashi, 

We have achieved your requirement by using ViewAnnotation and ActualRangechanged event. Also, we have modified the sample based on it and it can be downloaded from the link below. 

Screenshot: 
Please refer below code snippet. 
  
Code snippet [XAML]:   
<chart:SfChart.ChartAnnotations> 
                <chart:HorizontalLineAnnotation Y1="100" StrokeWidth="3" > 
                </chart:HorizontalLineAnnotation> 
                <chart:ViewAnnotation x:Name="viewAnnotation"  CoordinateUnit="Pixels" > 
                    <chart:ViewAnnotation.View> 
                            <Entry Text="Editor" /> 
                    </chart:ViewAnnotation.View> 
                </chart:ViewAnnotation> 
</chart:SfChart.ChartAnnotations> 
  
Code Snippet for Android and iOS: 
  
private void NumericalAxis_ActualRangeChanged(object sender, ActualRangeChangedEventArgs e) 
        { 
            if (Device.RuntimePlatform != Device.UWP) 
            { 
                double x1 = chart.ValueToPoint(chart.PrimaryAxis, 0); 
                double y1 = chart.ValueToPoint(chart.SecondaryAxis, 100); 
  
                if (!double.IsNaN(x1) && !double.IsNaN(y1)) 
                { 
                    viewAnnotation.X1 = x1; 
                    viewAnnotation.Y1 = y1; 
                } 
            } 
        } 
  
For UWP Annotation Horizontal and Vertical Alignment was not working. So, Editor is displayed just below the line annotation. We have already fixed this issue and this fix will be available in our Volume 3  which is expected to rolled out at the end of September. 
  
Code snippet for UWP [CustomRenderer.CS]:    
public class CustomRenderer : SfChartRenderer 
    { 
        protected override void OnElementChanged(ElementChangedEventArgs<SfChart> e) 
        { 
            base.OnElementChanged(e); 
  
            Control.Series[0].Loaded += Series_Loaded; 
        } 
  
        private void Series_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
        { 
            foreach (var annotation in Control.Annotations) 
            { 
                if (annotation is Native.ImageAnnotation) 
                { 
                    (annotation as Native.ImageAnnotation).X1 = Control.ValueToPoint(Control.PrimaryAxis, 0.5); 
                    (annotation as Native.ImageAnnotation).Y1 = Control.ValueToPoint(Control.SecondaryAxis, 100); 
                    (annotation as Native.ImageAnnotation). = Windows.UI.Xaml.VerticalAlignment.Top; 
                } 
            } 
        } 
        
    } 

Thanks, 
Michael  


Loader.
Up arrow icon