WPF charting toolkit y-axis labels appear as zero for small numbers

senor_cardgage picture senor_cardgage · Feb 13, 2012 · Viewed 12.3k times · Source

I've created a chart using the WPF toolkit (3.5) charting toolkit and I can't get the y-axis labels to display small numbers (e.g. .001). I have set the minimum and maximum values to .001 and .009 respectively for the y-axis and although the chart is graphically correct, the y-axis range labels show either "0" or ".01". I'm guessing that this is a limitation of the chart control in the 3.5 toolkit but I'm hoping that I'm missing something. Here's some example code:

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>

And the code behind:

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

Answer

punker76 picture punker76 · Feb 13, 2012

you can change the label style to get the effect

<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>

hope this helps...