gpt4 book ai didi

c# - 如何在 WPF 中为图表添加水平线 ("goal line")?

转载 作者:行者123 更新时间:2023-11-30 17:06:17 28 4
gpt4 key购买 nike

我正在使用 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

我怎样才能做到这一点?

最佳答案

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

我这样创建行:

<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>> 绑定(bind)在一起

enter image description here

关于c# - 如何在 WPF 中为图表添加水平线 ("goal line")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15488433/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com