我该如何在WPF图表中添加水平线(“目标线”)?

3
我将使用WPF工具包(http://www.codeproject.com/Articles/196502/WPF-Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co)创建一条折线图。
以下是我的操作步骤:
<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;
    }

}

enter image description here

我需要在Y轴的特定值处绘制一个“目标线”,比如,在这种情况下是-35:

enter image description here

我该怎么做才能实现这个?

2个回答

2

我在一些项目中已经做过类似的事情。

我创建的行就像这样:

<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>

我使用MVVM模式,并将"LineSeries"与ObservableCollection<KeyValuePair<string, int>>绑定。

enter image description here


抱歉,我接受得有点太快了。您的答案样式化了数据点,删除了圆圈并添加了品红色。请看最后一张带有红线的截图。那就是我想要的...保留数据点不做任何修改,并在设定点(例如:当Y = 35)添加物理线。 - David
@@David:我刚刚更新了我的答案。我认为现在它应该解决你的问题了 :-) - Franck Charlier

0
请使用以下代码:
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接