2017-08-30 60 views
0

我正在使用OxyPlot視圖來顯示圖表中的一些數據。它一直在正常工作,直到昨天。因爲,我添加了數據庫的部分(源碼),我的應用程序,每當我打開包含OxyPlot活動頁面,它只是顯示在手機屏幕上的這條消息:Oxyplot除了用小節序列創建模型

Oxyplot excepttion:對象引用未設置爲實例一個 對象。 System.NullReferenceException類型的異常在Oxyplot.ReflectionPath..ctor(System.String路徑)[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)); 

更新:

我發現,如果我用一個linear系列在創建情節模式,而不是棒系列,錯誤會消失。但是,如果我嘗試使用酒吧系列(在下面的代碼中,您可以看到它已被評論),那麼應用程序會再次向我顯示錯誤。

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

任何人都有一個想法什麼可以是創建我的模型與酒吧系列的問題?

+1

在哪一行你會得到錯誤? – Piyush

+0

這是我的問題,我把一個斷點,但實際上VS並沒有告訴我在哪一行我得到的錯誤。 –

+0

什麼是'energyBarsList'和'PlotView'?如果由你創建,則顯示'PlotView'類。 – Piyush

回答

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

異常是由這一行造成的:

LabelFormatString = "{2:0.0}", 

,因爲這在無效。我相信你正在尋找這個:

LabelFormatString = "{#:0.0}",