Search code examples
wpfchartstoolkit

How to add a vertical line at a specific datapoint in WPF Toolkit Chart


I have created a multi series line chart but I need to add a vertical line marker at the beginning and at the end of a cycle. I have researched everywhere and can't find how to do it. I need to do it from code behind because the value changes with every chart.

Any help is greatly appreciated.


Solution

  • The only way I found to add a vertical line was to add a datapoint at a specific x and y value. Notice that the two datapoints have the same X value. One starts at y value of 100 and the other one at 1200 thus making it a straight vertical line.

                //******************************************************
            //This is for plotting vertical lines
            //******************************************************
            lsStartCycle.IndependentValueBinding = new Binding("Key");
            lsStartCycle.DependentValueBinding = new Binding("Value");
    
            lsStartCycle.ItemsSource = new KeyValuePair<DateTime, int>[]
            {
            new KeyValuePair<DateTime,int>(MyFunctions.StartCycleTime, 100),
            new KeyValuePair<DateTime,int>(MyFunctions.StartCycleTime, 1200) };
    

    In XAML I added a new series.

                    <DVC:LineSeries Name="lsStartCycle" Title="Cycle Start"    
                            ItemsSource="{Binding}"
                            DataPointStyle="{StaticResource myLineDataPointStyle}"
                            PolylineStyle="{StaticResource DashedPolyLine}"
                            LegendItemStyle="{StaticResource HideLegend}"
                            IsSelectionEnabled="True">
                    </DVC:LineSeries>
    

    Note: My advice is to stay away from WPF Toolkit Chart. Use the MS Chart control instead, it is way way better. I ended up using that chart instead.