【问题标题】:How to create plots at runtime (OxyPlot)如何在运行时创建绘图 (OxyPlot)
【发布时间】:2019-08-04 17:51:40
【问题描述】:

我想要一个按钮,当单击该按钮时,它会创建一个 PlotView 的新实例并将其添加到 StackPanel。我该怎么做?

我试图想出一些东西,但它不起作用。到目前为止,这是我的代码:

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

            var plotView = new PlotView();
            plotView.Height = 300;
            plotView.Width = 600;
            plotView.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            var oxyPlotModel = new OxyPlotModel();
            var plotModel = new PlotModel();
            plotView.DataContext = oxyPlotModel;
            oxyPlotModel.PlotModel = plotModel;
            SetUpAxes(ref plotModel);
            plotModel.Axes[1].IsZoomEnabled = true;

            stackPanel1.Children.Add(plotView);
        }

        private void SetUpAxes(ref PlotModel plotModel)
        {
            plotModel.Axes.Clear();
            foreach (var axis in plotModel.Axes)
                axis.Reset();
            var yAxis = new OxyPlot.Axes.LinearAxis();
            var xAxis = new OxyPlot.Axes.DateTimeAxis();
            yAxis.IsZoomEnabled = false;
            yAxis.AbsoluteMinimum = -50;
            yAxis.AbsoluteMaximum = 450;
            yAxis.MajorGridlineStyle = LineStyle.Solid;
            xAxis.MajorGridlineStyle = LineStyle.Solid;
            xAxis.AbsoluteMinimum = OxyPlot.Axes.DateTimeAxis.ToDouble(new DateTime(Convert.ToDateTime("21/01/14 " + "00:00:00").Ticks));
            xAxis.AbsoluteMaximum = OxyPlot.Axes.DateTimeAxis.ToDouble(new DateTime(Convert.ToDateTime("21/01/14 " + "00:00:00").AddDays(1).Ticks));
            yAxis.IsPanEnabled = false;
            yAxis.IsZoomEnabled = false;
            yAxis.Font = "Open Sans";
            xAxis.Font = "Open Sans";
            plotModel.Axes.Add(yAxis);
            plotModel.Axes.Add(xAxis);
        }
    }

这是我的视图模型:

public class OxyPlotModel : INotifyPropertyChanged
    {

        private OxyPlot.PlotModel plotModel;
        public OxyPlot.PlotModel PlotModel
        {
            get
            {
                return plotModel;
            }
            set
            {
                plotModel = value;
                OnPropertyChanged("PlotModel");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

另一种选择是让 PlotViews 在 xaml 本身中不可见,然后在按下按钮后一个一个地显示它们,但我觉得这种方法更好。

【问题讨论】:

    标签: c# wpf xaml charts oxyplot


    【解决方案1】:

    这对于 ListBox 来说听起来不错。本质上,只需将新图表添加到 iobservable 图表集合中即可。

    xaml

    <button command="{binding AddChart}" />
    <ListBox ItemsSource="{Binding ChartDataSets}" IsSynchronizedWithCurrentItem="true">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <views:OxyPlotChart MinWidth="600" MinHeight="300" />
                </DataTemplate>
            </ListBox.ItemTemplate>
    </ListBox>
    

    OxyPLotChart.xaml

    <oxy:PlotView Model="{Binding}" />
    

    在 OxyPlotModel 中(*假设您的代码是正确的)

    public IObservableCollection<PlotView> ChartDataSets;
    
    public DelegateCommand AddChart { get; private set;}
    
    public OxyPlotModel()
    {
        ChartDataSets = new ObservableCollection<PlotView>();
        AddChart = new DelegateCommand(AddOxyChart);
    }
    
    private void AddOxyChart()
    {
         var plotView = new PlotView();
         plotView.Height = 300;
         plotView.Width = 600;
         plotView.VerticalAlignment = System.Windows.VerticalAlignment.Top;
         var oxyPlotModel = new OxyPlotModel();
         var plotModel = new PlotModel();
         plotView.DataContext = oxyPlotModel;
         oxyPlotModel.PlotModel = plotModel;
         SetUpAxes(ref plotModel);
         plotModel.Axes[1].IsZoomEnabled = true;
    
         ChartDataSets.Add(plotView);
    }
    
    private void SetUpAxes(ref PlotModel plotModel)
    {
        plotModel.Axes.Clear();
        foreach (var axis in plotModel.Axes)
            axis.Reset();
        var yAxis = new OxyPlot.Axes.LinearAxis();
        var xAxis = new OxyPlot.Axes.DateTimeAxis();
        yAxis.IsZoomEnabled = false;
        yAxis.AbsoluteMinimum = -50;
        yAxis.AbsoluteMaximum = 450;
        yAxis.MajorGridlineStyle = LineStyle.Solid;
        xAxis.MajorGridlineStyle = LineStyle.Solid;
        xAxis.AbsoluteMinimum = OxyPlot.Axes.DateTimeAxis.ToDouble(new DateTime (Convert.ToDateTime("21/01/14 " + "00:00:00").Ticks));
         xAxis.AbsoluteMaximum = OxyPlot.Axes.DateTimeAxis.ToDouble(new DateTime(Convert.ToDateTime("21/01/14 " + "00:00:00").AddDays(1).Ticks));
        yAxis.IsPanEnabled = false;
        yAxis.IsZoomEnabled = false;
        yAxis.Font = "Open Sans";
        xAxis.Font = "Open Sans";
        plotModel.Axes.Add(yAxis);
        plotModel.Axes.Add(xAxis);
    }
    

    【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多