Hello,
I'm trying to customize the legend but I'm stuck, is there anyway to move the legend under the chart? How can I apply styles/templates to legend items?
Thanks,
Pierre-Yves Troel
USING A DATATEMPLATE TO STYLE THE LEGEND:
Before You Begin:LegendItem has a LegendItemTemplate object associated with it. The LegendItemTemplate object exposes properties that you can use within a DataTemplate to create a customized look for your LegendItems.
What You Will Accomplish:You will create a simple DataTemplate to style the LegendItems in the Legend.
Follow these Steps:
When you set the UseDataTemplate property to True, the xamChart control will not use the default visual tree for its LegendItems. You must supply a DataTemplate that defines the visual tree for the LegendItems; otherwise, xamChart will just display the fully qualified namespace of the LegendItemTemplate object.
In XAML:
… <igCA:XamChart.Legend> <igCA:Legend UseDataTemplate="True" /> </igCA:XamChart.Legend> …
<
igCA:XamChart.Legend
>
igCA:Legend
UseDataTemplate
="
True"
/>
</
… <Window.Resources> <!--TODO: Create DataTemplate here--> </Window.Resources> …
Window.Resources
<!--TODO: Create DataTemplate here-->
Set the DataTemplate’s DataType property to '{x:Type igCA:LegendItemTemplate}'. By setting the DataType property to LegendItemTemplate, the DataTemplate will apply to all LegendItems.
Set the BorderBrush property to a binding expression that binds to the Fill property of the LegendItemTemplate object. This will keep the BorderBrush property of the Border bound to the Fill property of the DataPoint.
Set the Text property to a binding expression that binds to the Text property of the LegendItemTemplate object. This will keep the Text property of the LegendItem bound to the Label property of the Series or DataPoint.
… <DataTemplate DataType="{x:Type igCA:LegendItemTemplate}"> <Border VerticalAlignment="Center" HorizontalAlignment="Stretch" BorderThickness="4" CornerRadius="4" BorderBrush="{Binding Path=Fill}" > <TextBlock Text="{Binding Path=Text}" /> </Border> </DataTemplate> …
DataTemplate
DataType
=
"{x:Type igCA:LegendItemTemplate}"
Border
VerticalAlignment
"Center"
HorizontalAlignment
"Stretch"
BorderThickness
"4"
CornerRadius
BorderBrush
"{Binding Path=Fill}"
TextBlock
Text
"{Binding Path=Text}"
Can I do this in code behind?
My xamCharts are dynamically created, if I position the legend in code behind it seems not work.
="ADAPTModule_TransHistory_NS.UserControl1"
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
="http://schemas.microsoft.com/winfx/2006/xaml"
="http://infragistics.com/Chart"
="450">
}">
}}}">
="Bottom"/>
="Auto">
="Center">
="Center"/>
<!-- Chart control -->
="ColumnChart2D" >
<!-- Legend -->
<!--<igCA:XamChart.Legend>
<igCA:Legend MarginType="Percent" Margin="5, 85, 5, 5"/>
</igCA:XamChart.Legend>-->
<!-- Scene -->
<!--<igCA:XamChart.Scene>
<igCA:Scene BorderBrush="Black">
<igCA:Scene.GridArea>
<igCA:GridArea/>
</igCA:Scene.GridArea>
</igCA:Scene>
</igCA:XamChart.Scene>-->
<!-- Data points -->
Series
="Cars"
="line"
="4"
="#FFD70005">
="Febrary"/>
="March"/>
="April"/>
="May"/>
="00:00:02"/>
="Trucks"
="#FF6CA224">
="4" />
<!-- Axes -->
Axis
="PrimaryX"/>
="PrimaryY">
public partial class UserControl1 : UserControl
{
public UserControl1()
InitializeComponent();
ColumnChart2D.Legend =
new Legend();
ColumnChart2D.Legend.MarginType = Infragistics.Windows.Chart.
MarginType.Percent;
ColumnChart2D.Legend.Margin =
new Thickness(5, 85, 5, 5);
//ColumnChart2D.Legend.Template = ct; // FindResource("MyControlTemplate") as ControlTemplate;
//ControlTemplate ct = new ControlTemplate(this.Resources["LegendTemplate"].GetType());
}
creating a DataTemplate in C# code is a bit tricky, but it is possible: http://stackoverflow.com/questions/248362/how-do-i-build-a-datatemplate-in-c-code