1

In my WPF based application, I am using the Data Visualization Charting component available in the WPF Toolkit. I want to draw dashed lines, similar to the illustration from this SO answer:

Solid and dashed lines

Unfortunately, this only works with Windows Forms, since the BorderDashStyle property only exists in the Windows Forms version of the DataVisualization.Charting component and not in the WPF Toolkit equivalent.

How should I go about to generate dashed lines with the WPF Toolkit charting component?

Community
  • 1
  • 1
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114

2 Answers2

3

I searched for an analogous solution for the Silverlight Toolkit charting component, and found this.

Luckily, it turns out that the same approach can be applied in WPF. By setting the property LineSeries.PolylineStyle to a System.Windows.Shapes.Polyline style with a suitable Shape.StrokeDashArray property setting, the desired line dash can be obtained.

Programmatically, it can be accomplished with something like this:

var series = new LineSeries
    {
         ItemsSource = calcData,
         IndependentValuePath = "X",
         DependentValuePath = "Y",
         PolylineStyle = GetDashedLineStyle()
    };

...

Style GetDashedLineStyle()
{
    var style = new Style(typeof(Polyline));
    style.Setters.Add(new Setter(Shape.StrokeDashArrayProperty, 
                      new DoubleCollection(new[] { 5.0 })));
    return style;
}
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • 1
    Useful - thanks for answering your own question! I also used this [link](http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/RoundStrokeDashCapPolyline.htm) – tdc Oct 09 '14 at 21:05
  • after 3 hours of searching found this. Thank you.. Copied from the link referenced above – RasikaSam Apr 08 '15 at 04:58
1

Another way to add to xaml in WPF :

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

...


    <Window.Resources>
        <Style x:Key="DashedPolyLine" TargetType="{x:Type Polyline}">                        
            <Setter Property="StrokeDashArray" Value="2 3 2" />
        </Style>        
    </Window.Resources>

...

<chartingToolkit:LineSeries  Title="Title" DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding BindingValue}" PolylineStyle="{StaticResource DashedPolyLine}"/>
VDR
  • 11
  • 3