在Chart上获取LinearAxis的自动间隔值(System.Windows.Controls.DataVisualization.Charting)

时间:2016-10-10 20:25:56

标签: c# .net wpf xaml charts

我有一个使用System.Windows.Controls.DataVisualization.Charting的.NET项目。

创建新的LinearAxis时,我省略了Interval,以便自动计算,如下所示:

new LinearAxis()
{
    Title = "My Data",
    ShowGridLines = true,
    Orientation = AxisOrientation.Y,
    Maximum = 100
}

我想添加辅助Y轴。但是,为了使两个轴上的刻度线对齐,我需要根据我的第一个Y轴的间隔明确设置第二个Y轴的间隔。

问题是,我的第一个Y轴的间隔是自动计算的,并返回Null。 ActualInterval返回0.

如何获得使用自动间隔创建的LinearAx的Interval值,如上面的代码?

1 个答案:

答案 0 :(得分:1)

您可以使用绑定来实现,如下所示:

    <chartingToolkit:Chart Margin="0" Title="Chart Title">
        <chartingToolkit:Chart.Axes>
            <chartingToolkit:LinearAxis x:Name="ax1" 
                                        Orientation="Y" 
                                        ShowGridLines="True" 
                                        Title="First Axis"
                                        Minimum="0"
                                        />
            <chartingToolkit:LinearAxis x:Name="ax2" 
                                        Orientation="Y" 
                                        ShowGridLines="True" 
                                        Title="Second Axis"
                                        Interval="{Binding ElementName=ax1, Path=ActualInterval}" 
                                        Maximum="{Binding ElementName=ax1, Path=ActualMaximum}" 
                                        />
        </chartingToolkit:Chart.Axes>
        <chartingToolkit:ColumnSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding}"/>
    </chartingToolkit:Chart>

enter image description here

编辑:这是以前XAML的代码隐藏等价物:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        chart1 = new Chart();

        LinearAxis ax1 = new LinearAxis
        {
            Title = "First Axis",
            ShowGridLines = true,
            Orientation = AxisOrientation.Y,
            Minimum = 0
        };

        Binding b1 = new Binding();
        b1.Source = ax1;
        b1.Path = new PropertyPath("ActualInterval");

        Binding b2 = new Binding();
        b2.Source = ax1;
        b2.Path = new PropertyPath("ActualMaximum");

        LinearAxis ax2 = new LinearAxis
        {
            Title = "Second Axis",
            ShowGridLines = true,
            Orientation = AxisOrientation.Y
        };

        ax2.SetBinding(LinearAxis.IntervalProperty, b1);
        ax2.SetBinding(LinearAxis.MaximumProperty, b2);

        chart1.Axes.Add(ax1);
        chart1.Axes.Add(ax2);

        PointCollection pc = new PointCollection();
        pc.Add(new Point { X = 1, Y = 10 });
        pc.Add(new Point { X = 2, Y = 20 });
        pc.Add(new Point { X = 3, Y = 30 });
        pc.Add(new Point { X = 4, Y = 40 });

        ColumnSeries series = new ColumnSeries();
        series.ItemsSource = pc;
        series.DependentValuePath = "Y";
        series.IndependentValuePath = "X";

        chart1.Series.Add(series);
        AddChild(chart1);
    }