This is a migrated thread and some comments may be shown as answers.

Legend Item Click

2 Answers 132 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Nate East
Top achievements
Rank 1
Nate East asked on 18 Sep 2009, 09:03 PM
Is there an event for a Legend Item Click like there is for Chart Item Click?

Nate East

2 Answers, 1 is accepted

Sort by
0
Yusuf
Top achievements
Rank 1
answered on 19 Sep 2009, 12:21 PM
I also have a similiar requirement... I was thinking on lines of customizing the Chart layout and have this legends rendered as small buttons which can be clicked by the user... I am not sure if I am right track on this ?
0
Nate East
Top achievements
Rank 1
answered on 21 Sep 2009, 07:11 PM
So what I ended up doing was modifying the Legend Style.  I put a button in with an opacity of 0 to make it not visible, but still clickable.  You have to create this style and set the chart to use it in code:

_chart.DefaultView.ChartLegend.LegendItemStyle = this.CustomLegendStyle;

You'll notice that I bind the Tag property of the button to my label property for use in code.  The eventhandler code is at the bottom.

<Style x:Name="CustomLegendStyle" TargetType="chart:ChartLegendItem">
                <Setter Property="Template" >
                    <Setter.Value>
                        <ControlTemplate TargetType="chart:ChartLegendItem">
                        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Top">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Rectangle x:Name="PART_LegendItemMarker"  
                                    Height="15"   
                                    Width="30"  
                                    StrokeThickness="1"  
                                    RadiusX="2"  
                                    RadiusY="2"  
                                    HorizontalAlignment="Left"   
                                    VerticalAlignment="Bottom"  
                                    Margin="0,0,0,5"  
                                    Style="{TemplateBinding ItemStyle}" />
                            <TextBlock  
                                    Grid.Column="1"  
                                    Padding="0"  
                                    Margin="5,5,5,7"  
                                    VerticalAlignment="Center"  
                                    Height="14"    
                                    Foreground="White"
                                    Text="{TemplateBinding Label}" />
                            <Button x:Name="btnLegendLabel"
                                    Margin="0,2,0,2"
                                    Grid.ColumnSpan="2"
                                    Opacity="0"
                                    Cursor="Hand"
                                    Tag="{TemplateBinding Label}"
                                    Click="btnLegendLabel_Click"/>
                        </Grid>
                    </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

//Kinda specific to my use case, but I hope you can see what I'm doing here.
private void btnLegendLabel_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn.Tag.ToString() != "Requirements")
            {
                //Loop through all the dataseries to find the dataseries click on.
                foreach (DataSeries ds in _chart.DefaultView.ChartArea.DataSeries)
                {
                    if (ds.LegendLabel == btn.Tag.ToString())
                    {
                        //Do Something Here
                    }
                }
            }
        }
Tags
Chart
Asked by
Nate East
Top achievements
Rank 1
Answers by
Yusuf
Top achievements
Rank 1
Nate East
Top achievements
Rank 1
Share this question
or