locked
Addind checkbox into legend RRS feed

  • Question

  • I'd like to add check box in the legend near each series.
    In that way I want enable the option to display/hide  series.
    Is there anyway to define the legend as checkboxes?
    Thursday, January 22, 2009 10:12 AM

Answers

  • There is no built-in functionality like that but there is a way to implement this feature either in Windows Forms or ASP.NET control. Here is the main steps:

    You need to switch from automatically generated legend to custom legend items where
    1. you will have full control over the content.
    2. For each series create a custom legend item with 3 cells: Image, Item Key and Item Text.
    3. The first cell will be showing the checkbox image (either checked or unchecked state).
    4. You will also need to either handle the click event on that image (WinForms) or provide a PostBack value (ASP.NET Ajax) 
    5. Handle the event by hiding/showing the series and toggling the legend item cell image.

    Check samples for more information on different aspects of this approach: http://code.msdn.microsoft.com/mschart

    Alex.


    http://blogs.msdn.com/alexgor
    Thursday, January 22, 2009 5:35 PM

All replies

  • There is no built-in functionality like that but there is a way to implement this feature either in Windows Forms or ASP.NET control. Here is the main steps:

    You need to switch from automatically generated legend to custom legend items where
    1. you will have full control over the content.
    2. For each series create a custom legend item with 3 cells: Image, Item Key and Item Text.
    3. The first cell will be showing the checkbox image (either checked or unchecked state).
    4. You will also need to either handle the click event on that image (WinForms) or provide a PostBack value (ASP.NET Ajax) 
    5. Handle the event by hiding/showing the series and toggling the legend item cell image.

    Check samples for more information on different aspects of this approach: http://code.msdn.microsoft.com/mschart

    Alex.


    http://blogs.msdn.com/alexgor
    Thursday, January 22, 2009 5:35 PM
  • I don't know how to 4. provide a postback value (ASP.NET AJAX).
    is there a pointer to where I may get more information on that?
    Thursday, November 19, 2009 9:59 AM
  • I am now able to add click event into the chart.
    Indicate which checkbox the client clicked by PostBackValue.
    I can not hide a series by turning the color to transparent.
    But how can I persist the state/color of the series after postback?
    and how do I restore back to the original color suppose I am using one palette?
    Tuesday, November 24, 2009 5:36 AM
  • Hi Alex,

     

    I'm not able to follow step 3. how do add image of type checkbox??

     

    --Speed

    Tuesday, April 6, 2010 4:51 PM
  • Hello, I know it's out of date but this is my solution this problem:

            private void UpdateChart()
            {
                cMainChart.Series.Clear();
               
                foreach (BDataFile bdFile in bdGridViewAdapter.BDataFiles())
                {
                    Series bdFileSeries = new Series("SeriesName");
    
                    bdFileSeries.ChartType = SeriesChartType.Line;
                    bdFileSeries.SetCustomProperty("CHECK", "☑");
                    // Somehow add the data points
                    foreach (...)
                    {
                        bdFileSeries.Points.AddXY(...);
                    }
    
                    cMainChart.Series.Add(bdFileSeries);
                }
    
                cMainChart.Legends[0].CellColumns.Clear();
    
                cMainChart.Legends[0].CellColumns.Add(new LegendCellColumn() {
                    Name = "chbx",
                    ColumnType = LegendCellColumnType.Text,
                    Text = "#CUSTOMPROPERTY(CHECK)",
                    Font = new Font("Segoe UI", 12, FontStyle.Bold)
                });
    
                cMainChart.Legends[0].CellColumns.Add(new LegendCellColumn() {
                    Name = "symbol",
                    ColumnType = LegendCellColumnType.SeriesSymbol
                });
    
                cMainChart.Legends[0].CellColumns.Add(new LegendCellColumn() {
                    Name = "title",
                    ColumnType = LegendCellColumnType.Text,
                    Text = "#LEGENDTEXT"
                });
    
                cMainChart.ApplyPaletteColors();
    
                foreach(Series series in cMainChart.Series)
                {
                    cMainChart.Series[series.Name].SetCustomProperty(
                        "COLOR", 
                        ColorTranslator.ToHtml(series.Color));
                }
    
            }
    
    
     private void cMainChart_MouseDown(object sender, MouseEventArgs e)
            {
                HitTestResult result = cMainChart.HitTest(e.X, e.Y);
    
                if (result != null && result.Object != null 
                    && result.Object is LegendItem && e.Button == MouseButtons.Left)
                {
                    LegendItem legendItem = (LegendItem)result.Object;
    
                    Series series = cMainChart.Series[legendItem.SeriesName];
    
                    if (series.GetCustomProperty("CHECK").Equals("☑")) {
                        series.SetCustomProperty("CHECK", "☐");
                        series.Color = Color.FromArgb(0, series.Color);
                    }
                    else {
                        series.SetCustomProperty("CHECK", "☑");
                        series.Color = ColorTranslator.FromHtml(
                                    series.GetCustomProperty("COLOR"));
                    }
                }
            }
    

    Friday, December 4, 2015 7:56 PM