繁体   English   中英

如何在运行时创建图(OxyPlot)

[英]How to create plots at runtime (OxyPlot)

我想要一个按钮,当单击该按钮时,将创建一个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本身中不可见,然后在按下按钮后一个接一个地显示它们,但我觉得这种方法更好。

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

a

<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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM