user2784281
user2784281

Reputation: 89

How to have mixed exponential/decimal formats for Y axis in WPF charting

Can anyone suggest how to have customized formats for the Y axis labels in WPF when using System.Windows.Controls.DataVisualization.Charting.

For example - show exponential format for values < 0.01 else round the values to 0.1, 0.2 etc.

Upvotes: 2

Views: 559

Answers (1)

jsanalytics
jsanalytics

Reputation: 13188

Use a custom AxisLabelStyle and a Converter:

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>

Converter:

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

}

Upvotes: 1

Related Questions