Oxyplot异常在创建带条系列的模型时



我正在使用oxyplot视图在图中显示一些数据。它一直正常运行到昨天。由于,我将数据库部分(SQLite)添加到我的应用程序中,每当我打开"活动"页面包含OxyPlot时,它只会在移动屏幕上显示此消息:

oxyplot expextion:对象引用未设置为一个实例 目的。类型系统的例外。 在OxyPlot.ReflectionPath..ctor(System.String Path)[0x00006]

我试图找到System.NullReferenceException时放置断点,但没有停止该程序时的断点。

在我的活动中的用户界面中添加PlotView控件:

design d = new design();
var plotView = new PlotView(this);
plotView.Model = d.CreatePlotModelTest;
this.AddContentView(plotView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));

更新:

我注意到,如果在创建绘图模型而不是条形系列时使用线性序列,则错误将消失。但是,如果我尝试一个条系列(在下面的代码中,您可以在评论时看到),该应用将再次向我显示错误。

public PlotModel CreatePlotModelTest()
    {
        var plotModel = new PlotModel { Title = "OxyPlot Demo" };
        plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
        plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 });

        var series1 = new LineSeries
        {
            MarkerType = MarkerType.Circle,
            MarkerSize = 4,
            MarkerStroke = OxyColors.White
        };
        series1.Points.Add(new DataPoint(0.0, 6.0));
        series1.Points.Add(new DataPoint(1.4, 2.1));
        series1.Points.Add(new DataPoint(2.0, 4.2));
        series1.Points.Add(new DataPoint(3.3, 2.3));
        series1.Points.Add(new DataPoint(4.7, 7.4));
        series1.Points.Add(new DataPoint(6.0, 6.2));
        series1.Points.Add(new DataPoint(8.9, 8.9));
        plotModel.Series.Add(series1);
        /*var barSeries = new BarSeries
        {
            ItemsSource = new List<BarItem>(new[]
    {
            new BarItem{ Value = (2) },
            new BarItem{ Value = (3) },
            new BarItem{ Value = (4) },
            new BarItem{ Value = (5) },
            new BarItem{ Value = (6) }
    }),
            LabelPlacement = LabelPlacement.Inside,
            LabelFormatString = "{2:0.0}"
        };
        plotModel.Series.Add(barSeries);
        plotModel.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom });
            plotModel.Axes.Add(new CategoryAxis
            {
                Position = AxisPosition.Left,
                Key = "CakeAxis",
                ItemsSource = new[]
                    {
            "Apple cake",
            "Baumkuchen",
            "Bundt Cake",
            "Chocolate cake",
            "Carrot cake"
    }
            });*/
        return plotModel;
    }

任何人都有一个想法,可以使用bar系列创建我的模型的问题?

更新:

如果我编写这样的创建plotmodel,它可以很好地工作:

public PlotModel CreatePlotModelTest()
    {
        var model = new PlotModel
        {
            Title = "BarSeries",
            LegendPlacement = LegendPlacement.Outside,
            LegendPosition = LegendPosition.BottomCenter,
            LegendOrientation = LegendOrientation.Horizontal,
            LegendBorderThickness = 0
        };

        var rand = new Random();
        double[] cakePopularity = new double[5];
        for (int i = 0; i < 5; ++i)
        {
            cakePopularity[i] = rand.NextDouble();
        }
        var sum = cakePopularity.Sum();
        var s2 = new BarSeries { Title = "Series 1", StrokeColor = OxyColors.Black, StrokeThickness = 1 };
        s2.Items.Add(new BarItem { Value = (cakePopularity[0] / sum * 100) });
        s2.Items.Add(new BarItem { Value = (cakePopularity[1] / sum * 100) });
        s2.Items.Add(new BarItem { Value = (cakePopularity[2] / sum * 100) });
        s2.Items.Add(new BarItem { Value = (cakePopularity[3] / sum * 100) });
        s2.Items.Add(new BarItem { Value = (cakePopularity[4] / sum * 100) });
        var categoryAxis = new CategoryAxis { Position = AxisPosition.Left };
        categoryAxis.Labels.Add("Apple cake");
        categoryAxis.Labels.Add("Baumkuchen");
        categoryAxis.Labels.Add("Bundt Cake");
        categoryAxis.Labels.Add("Chocolate cake");
        categoryAxis.Labels.Add("Carrot cake");
        var valueAxis = new LinearAxis { Position = AxisPosition.Bottom, MinimumPadding = 0, MaximumPadding = 0.06, AbsoluteMinimum = 0 };
        model.Series.Add(s2);
        //model.Series.Add(s2);
        model.Axes.Add(categoryAxis);
        model.Axes.Add(valueAxis);
        return model;
    }

例外是由这一行引起的:

LabelFormatString = "{2:0.0}",

如无效。我相信您正在寻找:

LabelFormatString = "{#:0.0}",

最新更新