【问题标题】:Oxyplot excepttion while creating a model with bar series使用条形系列创建模型时出现 Oxyplot 异常
【发布时间】:2017-08-30 07:47:44
【问题描述】:

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

Oxyplot 异常:对象引用未设置为 目的。引发了 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;
    }

有人知道用条形系列创建我的模型有什么问题吗?

【问题讨论】:

  • 您在哪一行出现错误?
  • 这是我的问题,我放了一个断点,但实际上VS没有显示我在哪一行得到错误。
  • energyBarsListPlotView 是什么?显示 PlotView 类,如果它是你创建的。
  • 异常位置如图:在构造函数中:Oxyplot.ReflectionPath..ctor
  • 也许调试器没有停止,因为您将断点放在异常之后。也正如@ShushiHangover 所说,你有位置。我赌new PlotView(..)new PlotModel(..)这样的构造函数。

标签: android xamarin oxyplot


【解决方案1】:

更新:

如果我这样写 create 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;
    }

【讨论】:

    【解决方案2】:

    异常是由这一行引起的:

    LabelFormatString = "{2:0.0}",
    

    因为这无效。我相信您正在寻找这个:

    LabelFormatString = "{#:0.0}",
    

    【讨论】:

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