【问题标题】:How to have mixed exponential/decimal formats for Y axis in WPF charting如何在 WPF 图表中为 Y 轴混合指数/十进制格式
【发布时间】:2017-11-22 12:36:07
【问题描述】:

在使用System.Windows.Controls.DataVisualization.Charting 时,任何人都可以建议如何在 WPF 中为 Y 轴标签自定义格式。

例如 - 显示值

【问题讨论】:

    标签: c# wpf xaml charts data-visualization


    【解决方案1】:

    使用自定义AxisLabelStyleConverter

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

    【讨论】:

    • 非常感谢!这完美地解决了问题!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    相关资源
    最近更新 更多