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
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
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());
}
Thank you so much for the quick reply!
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}"
MOVING AND POSITIONING LEGEND:
The xamChart™ control automatically places the Legend on the right-hand side of the chart Scene; however, you can also position the Legend manually.
You must set the MarginType property to Percent instead of the default value of Auto if you want to position the Legend manually. When you set the MarginType property to Percent, the bounding rectangle of the Legend will stretch to fill the entire xamChart client area. You can then set the Margin property to determine the amount of space between the edges of the Legend’s bounding rectangle and the corresponding edges of xamChart’s client area.
For example, let’s assume that you have a xamChart control with a width of 800 and a height of 600. If you set the Legend’s MarginType property to Percent, the Legend’s bounding rectangle will stretch to fill up the entire 800 x 600 area. If you set the left and bottom margins to 75, the left and bottom edges of the Legend’s bounding rectangle are pushed in by 75 percent relative to the bounding rectangle of the whole xamChart client area. The Legend’s bounding rectangle now has a new width of 200 and height of 150.
Note: The left, top, right, and bottom of the Margin property must be set to a value between 0 and 100. The sum of the left and right values as well as the sum of the top and bottom values cannot exceed 100.The following screen shots show the Legend’s bounding rectangle (red border) before and after setting the left and bottom margins to 75. Notice that the LegendItems always stay horizontally aligned to the left and vertically aligned to the center within the Legend.
The following example code demonstrates how to position the Legend:
… <igCA:XamChart.Legend> <igCA:Legend MarginType="Percent" Margin="75, 10, 0, 10" /> </igCA:XamChart.Legend> …
MarginType
"Percent"
Margin
"75, 10, 0, 10"
Regards,GoranS