我如何添加一个水平线(QUOT;球门线")的图表在WPF? [英] How can I add a horizontal line ("goal line") for a chart in WPF?

查看:748
本文介绍了我如何添加一个水平线(QUOT;球门线")的图表在WPF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF工具包(的http://www.$c$cproject.com/Articles/196502/WPF-Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co)创建的线形图。

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.

下面是我在做什么:

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

}

我要画一个球门线在Y,比如说一个指定的值,在这种情况下 - 35:

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

我怎样才能做到这一点?

How can I achieve this?

推荐答案

我已经做了我的一些项目,类似的事情。

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

我创造这样的行:

<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&LT; KeyValuePair&LT;字符串,INT&GT;&GT;

这篇关于我如何添加一个水平线(QUOT;球门线&QUOT;)的图表在WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆