2 Answers, 1 is accepted
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
}
}
}
}
_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
}
}
}
}