指定 WPF 图表的最小和最大轴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15497076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:09:41  来源:igfitidea点击:

Specify minimum and maximum axis for WPF chart

c#.netwpfcustomizationwpftoolkit

提问by David

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.

这是一个看似简单的问题,但我找不到简单的答案。我想为图表上现有的 Y 轴指定最小值和最大值。

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:

现在,我在 Y 轴上添加 0 到 71 之间的值:

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.

但是,我希望图表 Y 轴显示 0 到 100 之间的范围,而不管我添加什么值。这是为了与同一页面上的不同图表保持一致。

enter image description here

在此处输入图片说明

回答by MarcinJuraszek

Add this XAMLcode inside your Chartdeclaration:

将此XAML代码添加到您的Chart声明中:

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