gpt4 book ai didi

c# - WPF 图表工具包 y 轴标签对于小数字显示为零

转载 作者:太空狗 更新时间:2023-10-29 21:29:25 24 4
gpt4 key购买 nike

我使用 WPF 工具包 (3.5) 制图工具包创建了一个图表,但我无法让 y 轴标签显示较小的数字(例如 .001)。我已将 y 轴的最小值和最大值分别设置为 .001 和 .009,尽管图表在图形上是正确的,但 y 轴范围标签显示“0”或“.01”。我猜这是 3.5 工具包中图表控件的限制,但我希望我遗漏了一些东西。下面是一些示例代码:

XAML:

    <Window x:Class="WpfChartApplication.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">
<Grid>
<chartingToolkit:Chart Name="chart1">
<chartingToolkit:LineSeries
Title="Rates"
ItemsSource="{Binding Rates}"
IndependentValueBinding="{Binding Time}"
DependentValueBinding="{Binding Value}"
>
<chartingToolkit:LineSeries.DependentRangeAxis>
<chartingToolkit:LinearAxis
Orientation="Y"
Title="Y Value"
ShowGridLines="True"
Maximum=".009"
Minimum=".001"/>
</chartingToolkit:LineSeries.DependentRangeAxis>
</chartingToolkit:LineSeries>

<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="X"
Title="X Value"
ShowGridLines="True"
/>
</chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>
</Grid>

以及背后的代码:

    using System.Collections.Generic;
using System.Windows;

namespace WpfChartApplication
{
public partial class MainWindow : Window
{


public MainWindow()
{
InitializeComponent();

var cVm = new ChartViewModel();
chart1.DataContext = cVm;
}
}

public class ChartViewModel
{
public List<Rate> Rates { get; set; }

public ChartViewModel()
{
Rates = new List<Rate>();

Rates.Add(new Rate(1, .001));
Rates.Add(new Rate(2, .003));
Rates.Add(new Rate(3, .001));
Rates.Add(new Rate(4, .002));
Rates.Add(new Rate(5, .001));
Rates.Add(new Rate(6, .001));
Rates.Add(new Rate(7, .003));
Rates.Add(new Rate(8, .007));
Rates.Add(new Rate(9, .009));
Rates.Add(new Rate(10, .008));
}
}

public class Rate
{
public Rate(int time, double value)
{
Time = time;
Value = value;
}

public int Time { get; set; }
public double Value { get; set; }
}
}

最佳答案

您可以更改标签样式以获得效果

<Style x:Key="NumericAxisLabelStyle" TargetType="{x:Type chartingToolkit:NumericAxisLabel}">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="StringFormat" Value="{}{0:0.###}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type chartingToolkit:NumericAxisLabel}">

<TextBlock Text="{TemplateBinding FormattedContent}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<chartingToolkit:Chart Name="chart1">
<chartingToolkit:LineSeries Title="Rates"
ItemsSource="{Binding Rates}"
IndependentValueBinding="{Binding Time}"
DependentValueBinding="{Binding Value}">
<chartingToolkit:LineSeries.DependentRangeAxis>
<chartingToolkit:LinearAxis Orientation="Y"
Title="Y Value"
ShowGridLines="True"
AxisLabelStyle="{StaticResource NumericAxisLabelStyle}"
Maximum=".009"
Minimum=".001" />
</chartingToolkit:LineSeries.DependentRangeAxis>
</chartingToolkit:LineSeries>
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="X" Title="X Value" ShowGridLines="True" />
</chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>

希望这有助于...

关于c# - WPF 图表工具包 y 轴标签对于小数字显示为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9263861/

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