LineChart with Markers
I came across a forum post in silverlight forums looking for a way to get markers in a linechart. I wanted to try it myself.
This is the end result. As the points are random, you might have to hit refresh, in case you dont see the lines.

I changed the template to include a canvas
<ControlTemplate TargetType=”charting:Chart”>
<Border Background=”{TemplateBinding Background}”
BorderBrush=”{TemplateBinding BorderBrush}”
BorderThickness=”{TemplateBinding BorderThickness}”
Padding=”10″>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=”Auto” />
<RowDefinition Height=”*” />
</Grid.RowDefinitions>
<datavis:Title Content=”{TemplateBinding Title}”
Style=”{TemplateBinding TitleStyle}” />
<!– Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto –>
<Grid Grid.Row=”1″
Margin=”0,15,0,15″>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”*” />
<ColumnDefinition Width=”Auto” />
</Grid.ColumnDefinitions>
<datavis:Legend x:Name=”Legend”
Title=”{TemplateBinding LegendTitle}”
Style=”{TemplateBinding LegendStyle}”
Grid.Column=”1″ />
<Grid x:Name=”ChartArea”
Style=”{TemplateBinding ChartAreaStyle}”>
<Grid x:Name=”PlotArea”
Style=”{TemplateBinding PlotAreaStyle}”>
<Grid x:Name=”GridLinesContainer” />
<Grid x:Name=”SeriesContainer”>
</Grid>
<Border BorderBrush=”#FF919191″
BorderThickness=”1″ />
<Canvas Background=”Transparent”
x:Name=”c1″></Canvas>
</Grid>
</Grid>
</Grid>
</Grid>
</Border>
</ControlTemplate>
and in the code behind once we have the chart rendered . In the Layout updated method of the chart. I call the following function. which adds the markers
void Highlight(List<Order> orderList)
{
LineSeries ls = this.chart1.Series[0] as LineSeries;
Grid plotArea = chart1.GetChildrenByType<Grid>().Where(g => g.Name == “PlotArea”).SingleOrDefault<Grid>();
Canvas c1 = chart1.GetChildrenByType<Canvas>().Where(c => c.Name == “c1″).SingleOrDefault<Canvas>();
foreach (Order o in orderList)
{
Point p = ls.Points[orders.IndexOf(o)];
Line ln = new Line();
ln.Stroke = new SolidColorBrush(Colors.Black);
ln.StrokeThickness = 1.0;
DoubleCollection dashCollection = new DoubleCollection();
dashCollection.Add(2);
dashCollection.Add(4);
ln.StrokeDashArray = dashCollection;
ln.X1 = p.X;
ln.X2 = p.X;
ln.Y1 = -10.0;
ln.Y2 = plotArea.ActualHeight;
TextBlock txt = new TextBlock();
txt.Text = o.OrderDate.ToShortDateString();
txt.Foreground = new SolidColorBrush(Colors.Blue);
Grid chartArea = plotArea.Parent as Grid;
txt.SetValue(Canvas.LeftProperty, ln.X1 + 5.0);
txt.SetValue(Canvas.TopProperty, ln.Y1 – 10);
c1.Children.Add(txt);
c1.Children.Add(ln);
}
}
you can download the sample here
