Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Chart Legend Items interactive in C#

I have created a chart at run time using win forms (C#.Net Framework 3.5).

enter image description here

I want to make the legend items of this chart interactive.

My requirement is, when a user clicks on Color item present in legend - a color pallet should open and when user selects a color from pallet the selected color should be applied to the outer series data item.

How do I achieve this? In short how do I add a click event handler for a legend item?

Any help is appreciated. NB

like image 447
Nilesh Barai Avatar asked Mar 23 '23 19:03

Nilesh Barai


1 Answers

Found the answer finally... Posting code here so that it would be helpful for others.

private void HeapStatsChart_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            HitTestResult result = HeapStatsChart.HitTest(e.X, e.Y);
            if (result != null && result.Object != null)
            {
                // When user hits the LegendItem
                if (result.Object is LegendItem)
                {
                    // Legend item result
                    LegendItem legendItem = (LegendItem)result.Object;
                    ColorDialog Colour = new ColorDialog();
                    if (Colour.ShowDialog() == DialogResult.OK)
                    {
                        HeapChartColorPref[Convert.ToInt16(legendItem.Name.Substring(4))].color = Colour.Color;
                        GenerateHeapStatsChart(HeapChartColorPref);
                    }
                }
            }
        }
like image 153
Nilesh Barai Avatar answered Apr 26 '23 15:04

Nilesh Barai