gpt4 book ai didi

c# - 带有自定义轴标签的 xaml c# 图表

转载 作者:太空宇宙 更新时间:2023-11-03 21:21:00 24 4
gpt4 key购买 nike

我使用 System.Windows.Controls.DataVisualization.Charting 来绘制和显示带有 LineSeries 的图表,其 Y 值在 1 到 5 之间。我使用 向 LineSeries 添加值>System.Collections.Generic.KeyValuePair(TKey,TValue)。我不想在 Y 标签图表中显示从 1 到 5 的数字,而是显示从 E 到 A 的字母。

我怎样才能达到这个结果?

最佳答案

首先创建一个转换器来将数值转换为字母值:

 public class NumericToAlphaConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
switch (value.ToString())
{
case "1":
return "A";
case "2":
return "B";
case "3":
return "C";
case "4":
return "D";
case "5":
return "E";
default: return null;
}
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

使用该转换器定义自定义 Y 标签样式:

 <Style TargetType="chartingToolkit:AxisLabel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:AxisLabel">
<TextBlock DataContext="{TemplateBinding FormattedContent}" Text="{Binding Converter={StaticResource NumericToAlphaConverter}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

这里是打洞UI和后面对应的代码

<Window.Resources>
<wpfApplication12:NumericToAlphaConverter x:Key="NumericToAlphaConverter"/>
</Window.Resources>
<Grid>
<chartingToolkit:Chart Title="Line Series"
VerticalAlignment="Top" Margin="0" Height="254" >
<chartingToolkit:LineSeries x:Name="serie"
IndependentValueBinding="{Binding Path=Key}"
DependentValueBinding="{Binding Path=Value}"
IsSelectionEnabled="True"/>
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y"
Title="Y val"
Maximum="5"
Minimum="1"
>
<chartingToolkit:LinearAxis.AxisLabelStyle>
<Style TargetType="chartingToolkit:AxisLabel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="chartingToolkit:AxisLabel">
<TextBlock DataContext="{TemplateBinding FormattedContent}" Text="{Binding Converter={StaticResource NumericToAlphaConverter}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</chartingToolkit:LinearAxis.AxisLabelStyle>
</chartingToolkit:LinearAxis>
</chartingToolkit:Chart.Axes>

</chartingToolkit:Chart>
</Grid>

背后的代码:

public ObservableCollection<KeyValuePair<int, int>> LineSeriesData = new ObservableCollection<KeyValuePair<int, int>>()
{
new KeyValuePair<int, int>(12,1),
new KeyValuePair<int, int>(5,2),
new KeyValuePair<int, int>(5,3),
new KeyValuePair<int, int>(5,4)

};
public MainWindow()
{
InitializeComponent();
serie.ItemsSource = LineSeriesData;
}

enter image description here

关于c# - 带有自定义轴标签的 xaml c# 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30733783/

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