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

Adding vertical line annotation to Chart Control in WinForms

Hello, could you advise how to add red vertical line annotation as per attached? (target line)


Also I'd like to invert the legend order of series so that it shows the same order as displayed in chart..





Thank you


7 Replies

MD Moneeshram Dhanabal Syncfusion Team February 20, 2023 11:41 AM UTC

Hi Jacob,


Query 1: Add vertical line annotation as per image.


The Vertical line can only be achieved by using the StripLine property and for the stripline you have to define start and end value as shown in the code snippet below.

If both start and end values are same then stripline will not be displayed, because the stripline width is equal to the difference of start and end value.

            ChartStripLine stripLine = new ChartStripLine();

stripLine.Vertical = false;

stripLine.Enabled = true;

stripLine.Start = 420;

            stripLine.End = 421;

            stripLine.Text = "";

stripLine.ZOrder = ChartStripLineZorder.Over;

stripLine.Interior = new BrushInfo(Color.Red);

            this.chartControl1.PrimaryXAxis.StripLines.Add(stripLine);


Query 2: Inverse the legend Item order.


The order of the legend item can be inversed using the FilterItems event. In this event will iterate the Legend Items in reverse order and add it to the item again as shown in the code snippet below.

private void InitializeComponent()

{

              this.chartControl1.Legend.FilterItems += Legend_FilterItems;

}


private void Legend_FilterItems(object sender, ChartLegendFilterItemsEventArgs e)

{

            ChartLegendItemsCollection items = new ChartLegendItemsCollection();

            for (int i = e.Items.Count - 1; i >= 0; i--)

items.Add(e.Items[i]);

 

            e.Items = items;

}


I have attached a demo sample based on your requirement for reference please check and let us know if need any further assistance.


Regards,

Moneeshram


Attachment: F_180622_57644470.zip


JA Jakub February 21, 2023 11:00 AM UTC

Hi Moneeshram,


This is great! Thanks!


The only problem I am having is with implementing the 2nd query in VB.NET


Do you have working example in VB.net on how to reverse the legend?



Thank you so much for time and patience


Me.chartControl1.Legend.FilterItems +=New LegendFilterItemsEventHandler(Legend_FilterItems)


  Private Sub Legend_FilterItems(ByVal sender As Object, ByVal e As ChartLegendFilterItemsEventArgs)

    'This creates an new instance of the ChartLegendItemCollection

    Dim item As ChartLegendItemsCollection = New ChartLegendItemsCollection()

        Dim i As Integer

        For i = e.Items.Count - 1 To 0 Step i - 1

item.Add(e.Items(i))

        Next

    e.Items =item

End Sub





MD Moneeshram Dhanabal Syncfusion Team February 22, 2023 06:36 AM UTC

Query: How to reverse the legend in VB.Net?


We can reverse the legend order in the VB by using the code snippet as shown below.

Me.chartControl1.Legend.FilterItems +=New LegendFilterItemsEventHandler(Legend_FilterItems)

 

Private Sub Legend_FilterItems(ByVal sender As Object, ByVal e As ChartLegendFilterItemsEventArgs)

        'This creates an new instance of the ChartLegendItemCollection

        Dim item As New ChartLegendItemsCollection()

 

        For i As Integer = e.Items.Count - 1 To 0 Step -1

item.Add(e.Items(i))

        Next

 

        e.Items = item

 

End Sub


Also please check the below attached UG document for reference and let us know if need any
further assistance.

UG Link: https://help.syncfusion.com/windowsforms/chart/chart-legend-and-legend-items?cs-save-lang=1&cs-lang=vb#customizing-items-through-event


Regards,

Moneeshram



JA Jakub February 22, 2023 11:06 AM UTC

Hi Moneeshram,



This is exactly what I've tried, however I am a little embarrassed as I cannot get it to work.. I am just learning to code.


I get the following errors and even when trying to follow the advice on the error message I still cannot structure the code properly to make it work.






MD Moneeshram Dhanabal Syncfusion Team February 23, 2023 06:31 AM UTC

We would like to inform you that the issue is raised because in VB you should not trigger the event as in C#, as shown in the given image. In VB, you have to trigger the handler as shown in the code snippet below.

Private Sub InitializeComponent()

        'ChartControl1 code . . .

        AddHandler Me.ChartControl1.Legend.FilterItems, AddressOf Legend_FilterItems

End Sub

 

Private Sub Legend_FilterItems(sender As Object, e As ChartLegendFilterItemsEventArgs)

        Dim item As New ChartLegendItemsCollection()

 

        For i As Integer = e.Items.Count - 1 To 0 Step -1

item.Add(e.Items(i))

        Next

 

        e.Items = item

End Sub


Please check and let us know if you need any further assistance.


Regards,

Moneeshram.



JA Jakub February 24, 2023 04:47 PM UTC

Thank you, issue resolved!


Thanks so much!



PR Preethi Rajakandham Syncfusion Team February 27, 2023 06:47 AM UTC

Hi Jakub,

We are glad to know that the reported problem has been resolved. Please let us know if you have any further queries on this. We are happy to help.

Regards,

Preethi R


Loader.
Live Chat Icon For mobile
Up arrow icon