How can I add a horizontal line (“goal line”) for a chart in WPF?

那年仲夏 提交于 2020-01-24 10:49:10

问题


I'm using the WPF toolkit (http://www.codeproject.com/Articles/196502/WPF-Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co) to create a line graph.

Here's what I'm doing:

<Window x:Class="TempDataAnalyzer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" Loaded="Window_Loaded">
    <Grid>
         <chartingToolkit:Chart  Name="lineChart" Title="Temperature" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <chartingToolkit:LineSeries  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True"/>
         </chartingToolkit:Chart>
    </Grid>
</Window>

C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        List<KeyValuePair<int, int>> entries = new List<KeyValuePair<int, int>>();
        entries.Add(new KeyValuePair<int, int>(0, 0));
        entries.Add(new KeyValuePair<int, int>(1, 23));
        entries.Add(new KeyValuePair<int, int>(2, 45));
        entries.Add(new KeyValuePair<int, int>(3, 46));
        entries.Add(new KeyValuePair<int, int>(4, 71));

        lineChart.DataContext = entries;
    }

}

I need to draw a "goal line" at a specified value of Y, say, in this case - 35:

How can I achieve this?


回答1:


I've already do similar things in some of my projects.

I create the line like that :

<chartingToolkit:Chart Name="chart1" Title="Chart Title">
    <chartingToolkit:LineSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}">
        <chartingToolkit:LineSeries.PolylineStyle>
            <Style TargetType="Polyline">
                <Setter Property="StrokeDashArray" Value="5 5 5" />
                <Setter Property="StrokeThickness" Value="2"/>
            </Style>
        </chartingToolkit:LineSeries.PolylineStyle>
        <chartingToolkit:LineSeries.DataPointStyle>
            <Style TargetType="{x:Type chartingToolkit:LineDataPoint}">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="Template" Value="{x:Null}" />
            </Style>
        </chartingToolkit:LineSeries.DataPointStyle>
    </chartingToolkit:LineSeries>
</chartingToolkit:Chart>

I use it with the MVVM pattern, and I bind the "LineSeries" with an ObservableCollection<KeyValuePair<string, int>>




回答2:


Use this:

<chartingToolkit:LineSeries.DataPointStyle>
    <Style TargetType="chartingToolkit:LineDataPoint">

        <!-- <Setter Property="Template" Value="{x:Null}"/> -->
        <Setter Property="Opacity" Value="0" />
        <Setter Property="Background" Value="Red"/>
        <Setter Property="BorderBrush" Value="Red"/>
        <Setter Property="Width" Value="2" />
        <Setter Property="Height"  Value="2" />
    </Style>
</chartingToolkit:LineSeries.DataPointStyle>
<chartingToolkit:LineSeries.PolylineStyle>
    <Style TargetType="Polyline">
        <Setter Property="Opacity" Value="1" />
        <Setter Property="StrokeThickness" Value="1"/>
        <Setter Property="Stroke" Value="Red" />
    </Style>
</chartingToolkit:LineSeries.PolylineStyle>


来源:https://stackoverflow.com/questions/15488433/how-can-i-add-a-horizontal-line-goal-line-for-a-chart-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!