I am adding lineseries to a chart dynamically as follows.
foreach (KeyValuePair<string, List<KeyValuePair<DateTime, double>>> tempSeries in yieldSeries)
{
LineSeries lineSeries = new LineSeries();
lineSeries.DependentValuePath = "Value";
lineSeries.IndependentValuePath = "Key";
lineSeries.ItemsSource = tempSeries.Value;
lineSeries.Title = tempSeries.Key;
lineSeries.SetResourceReference(FrameworkElement.StyleProperty,"CommonLineSeries");
lineSeries.Tag = Brushes.Red;
lineSeries.Background = Brushes.Red;
yieldTrendChart.Series.Add(lineSeries);
}
I wish to assign specific color in specific order for the lineseries to attain this I have added a style for the polyline as follows
<Style x:Key="CommonLineSeries" TargetType="charting:LineSeries" BasedOn="{StaticResource {x:Type charting:LineSeries}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="charting:LineSeries">
<Canvas x:Name="PlotArea">
<Polyline Points="{TemplateBinding Points}" Stroke="{Binding Tag, RelativeSource={RelativeSource AncestorType={x:Type charting:LineSeries}}}" Style="{TemplateBinding PolylineStyle}"/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Problem The color is successfully assigned to the lines in the graph however the rectangle near the legend still shows the color assigned by .net randomly. How shall I assign the same color to the rectangle as I have assigned it to the line??
You need to edit LegendItem Style to change color of legend rectangle.
xaml
c#
{
}
Result
Update
c# Code
Result