【问题标题】:How to refresh WPF chart after updating the Y Axis Maximum value?更新 Y 轴最大值后如何刷新 WPF 图表?
【发布时间】:2011-03-06 19:07:16
【问题描述】:

我正在使用 WPFToolKit 创建折线图。

图表很好,但我想在第一次加载图表后单击模态窗口的按钮时更改 Y 轴的最大值属性。并且图表应该使用更新后的 Y Axis Max 值进行刷新

下面一行显示了图表是如何在 xaml 中定义的。

<DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart" VerticalAlignment="Stretch"/>

我在 windows.xaml.cs 构造函数中调用下面的代码,它将 Y 轴最大值设置为 200

mcChart.Axes.Add(new LinearAxis()
        {
            Minimum = 0,
            Maximum = YMax > 0 ? YMax : 200,
            Orientation = AxisOrientation.Y,
            ShowGridLines = true,               
        });

        mcChart.UpdateLayout(); 

如何从模式窗口的按钮单击事件和刷新图表中更改 Y 轴最大值以显示新的 YMax。

我不确定我是否必须对 RegisteredListeners 做点什么。 我是 WPF 新手,不胜感激!

请注意,我希望通过 C# 代码而不是 xaml 来实现这一点。

谢谢, 苏杰

【问题讨论】:

  • 模态窗口与主窗口不同,它无法访问图表,对吧?我会使用用户定义的事件来解决这个问题。或者我会在模式窗口关闭后更改最大值。这取决于任务。
  • 好的,忘记模式窗口。基本上我正在寻找如何更改最大值并刷新图表。我可以处理何时何地调用该逻辑。谢谢苏杰
  • @user647204 更新将自动反映,因为 Maximum 属性是一个依赖属性。总有一天你会熟悉它们,但现在只知道你可以在不调用更新函数的情况下为属性设置值。

标签: c# wpf wpftoolkit charts


【解决方案1】:

如果您有权访问图表,则可以找到必要的轴并更改 Maximum 属性而不更新布局。这是一个带有线性 Y 轴的示例:

var yAxis = this.mcChart.ActualAxes.OfType<LinearAxis>().FirstOrDefault(ax => ax.Orientation == AxisOrientation.Y);
if (yAxis != null)
    yAxis.Maximum = 300;

这个例子的完整版本:

MainWindow.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Content="Set max value = 300" HorizontalAlignment="Center" Click="Button_Click"/>
    <charting:Chart Grid.Row="1" x:Name="mcChart">
        <charting:Chart.Series>
            <charting:LineSeries ItemsSource="{Binding LineItems}" IndependentValuePath="Date" DependentValuePath="Value"/>
        </charting:Chart.Series>
    </charting:Chart>
</Grid>

MainWindow.xaml.cs

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

        //Add a linear Y axis
        int YMax = 150;
        mcChart.Axes.Add(new LinearAxis()
        {
            Minimum = 0,
            Maximum = YMax > 0 ? YMax : 200,
            Orientation = AxisOrientation.Y,
            ShowGridLines = true,
        });

        //Create and set a view model
        var items = Enumerable.Range(0, 50).Select(i => new ChartItemModel { Date = new DateTime(2010, 1, 1).AddDays(i), Value = 30 + i }).ToList();
        this.DataContext = new MainViewModel { LineItems = items };
    }

    //Set Maximum=300
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var yAxis = this.mcChart.ActualAxes.OfType<LinearAxis>().FirstOrDefault(ax => ax.Orientation == AxisOrientation.Y);
        if (yAxis != null)
            yAxis.Maximum = 300;
    }
}

public class MainViewModel
{
    public List<ChartItemModel> LineItems { get; set; }
}

public class ChartItemModel
{
    public DateTime Date { get; set; }
    public double Value { get; set; }
}

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 2012-09-25
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多