Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify minimum and maximum axis for WPF chart

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

like image 567
David Avatar asked Mar 19 '13 10:03

David


1 Answers

Add this XAML code inside your Chart declaration:

<chartingToolkit:Chart.Axes>
    <chartingToolkit:LinearAxis Orientation="Y" Minimum="0" Maximum="100"/>
</chartingToolkit:Chart.Axes>
like image 103
MarcinJuraszek Avatar answered Nov 10 '22 02:11

MarcinJuraszek