在创建带有条形系列的模型时,Oxyplot除外

时间:2017-08-30 07:47:45

标签: android xamarin oxyplot

我正在使用OxyPlot视图在图表中显示一些数据。它一直工作到昨天。因为,我将数据库部分(sqlite)添加到我的应用程序,每当我打开包含OxyPlot的活动页面时,它只是在移动屏幕上显示此消息:

  

Oxyplot excepttion:对象引用未设置为的实例   宾语。抛出了类型system.NullReferenceException的异常   在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;
    }

任何人都知道使用条形图系列创建模型的问题是什么?

2 个答案:

答案 0 :(得分:0)

更新:

如果我像这样编写创建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;
    }

答案 1 :(得分:0)

此行引起异常:

LabelFormatString = "{2:0.0}",

因此无效。我相信你正在寻找这个:

LabelFormatString = "{#:0.0}",