如何在WPF图表中为Y轴添加混合指数/十进制格式

时间:2017-11-22 12:36:08

标签: c# wpf xaml charts data-visualization

使用System.Windows.Controls.DataVisualization.Charting时,是否有人可以建议如何在WPF中为Y轴标签定制格式。

例如 - 显示值的指数格式< 0.01否则将值舍入为0.1,0.2等。

1 个答案:

答案 0 :(得分:1)

使用自定义AxisLabelStyleConverter

enter image description here

<强> XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp87"
        xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
        x:Class="WpfApp87.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="475" Width="525">

    <Window.Resources>
        <local:ValueToExponentialConverter x:Key="conv1"/>
    </Window.Resources>

    <Grid>

        <chartingToolkit:Chart Title="Sample Chart">
            <chartingToolkit:Chart.DataContext>
                <PointCollection>
                    <Point X="1" Y="-0.010"/>
                    <Point X="2" Y="-0.008"/>
                    <Point X="3" Y="-0.006"/>
                    <Point X="4" Y="-0.004"/>
                    <Point X="5" Y="-0.002"/>
                    <Point X="6" Y="0.0"/>
                    <Point X="7" Y="0.002"/>
                    <Point X="8" Y="0.004"/>
                    <Point X="9" Y="0.006"/>
                    <Point X="10" Y="0.008"/>
                    <Point X="11" Y="0.010"/>
                    <Point X="12" Y="0.012"/>
                    <Point X="13" Y="0.014"/>
                    <Point X="14" Y="0.016"/>
                    <Point X="15" Y="0.018"/>
                    <Point X="16" Y="0.020"/>
                </PointCollection>
            </chartingToolkit:Chart.DataContext>
            <chartingToolkit:Chart.Axes>
                <chartingToolkit:LinearAxis Orientation="Y" 
                                            Location="Left" 
                                            Interval="0.002"
                                            Minimum="-0.01"
                                            Maximum="0.02"
                                            ShowGridLines="True">
                    <chartingToolkit:LinearAxis.AxisLabelStyle>
                        <Style TargetType="chartingToolkit:AxisLabel">
                            <Setter Property="StringFormat" Value="{Binding Converter={StaticResource conv1}}"/>
                            <Setter Property="FontSize" Value="14"/>
                        </Style>
                    </chartingToolkit:LinearAxis.AxisLabelStyle>
                </chartingToolkit:LinearAxis>
            </chartingToolkit:Chart.Axes>
            <chartingToolkit:ColumnSeries DependentValuePath="Y" 
                                          IndependentValuePath="X" 
                                          ItemsSource="{Binding}"/>
        </chartingToolkit:Chart>

    </Grid>
</Window>

<强>转换器:

public class ValueToExponentialConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double d = (double)value;

        if (d == 0 || 0.0099 < d)
            return Math.Round(d, 3).ToString();
        else return d.ToString("0.#E+0");
    }

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

}