Specify minimum and maximum axis for WPF chart

David picture David · Mar 19, 2013 · Viewed 8.5k times · Source

This is a seemingly simple question but I cannot find a simple answer. I want to specify the minimum and maximum for the existing Y axis on the chart.

Here's the chart:

<Window x:Class="TempDataAnalyzer.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" 
    Loaded="Window_Loaded">
<Grid>
     <chartingToolkit:Chart  Name="lineChart" Title="Temperature" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <chartingToolkit:LineSeries  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding}" IsSelectionEnabled="True"/>
     </chartingToolkit:Chart>
</Grid>

Now, I'm adding values ranging between 0 to 71 on the Y axis:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        List<KeyValuePair<int, int>> entries = new List<KeyValuePair<int, int>>();
        entries.Add(new KeyValuePair<int, int>(0, 0));
        entries.Add(new KeyValuePair<int, int>(1, 23));
        entries.Add(new KeyValuePair<int, int>(2, 45));
        entries.Add(new KeyValuePair<int, int>(3, 46));
        entries.Add(new KeyValuePair<int, int>(4, 71));

        lineChart.DataContext = entries;
    }

}

However, I want the chart Y axis to show me a range between 0 and 100, regardless of what values I add. This is to keep it consistent with different charts on the same page.

enter image description here

Answer

MarcinJuraszek picture MarcinJuraszek · Mar 19, 2013

Add this XAML code inside your Chart declaration:

<chartingToolkit:Chart.Axes>
    <chartingToolkit:LinearAxis Orientation="Y" Minimum="0" Maximum="100"/>
</chartingToolkit:Chart.Axes>