2017-04-25 75 views
2

我試圖在一行和幾個類別中繪製許多IntervalBarItems。在Oxyplot中,Categoryaxis將forevalBarSeries一個IntervalBarItem繪製到該行中。所以非常規如果我有三個系列,每個系列有4個項目 - 我有4個類別(3個BarItems)(系列1,系列2,系列3)。但我想要3個類別 - 每個系列中有4個項目 - 我可以通過爲每個項目設置CategoryIndex-Property來獲得此結果。在這裏,我得到IndexOutOfRangeException - 通過設置循環索引屬性。IndexOutOfRangeException嘗試在循環中設置CategoryIndex

MCVE:

XAML文件:

<Window x:Class="BarSeries_Stacked.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:BarSeries_Stacked" 
     xmlns:oxy="http://oxyplot.org/wpf" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <oxy:PlotView Name="plot" /> 
    </Grid> 
</Window> 

CS-文件:

namespace BarSeries_Stacked 
{ 
    /// <summary> 
    /// Interaktionslogik für MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      SetUpPlot(); 
     } 

     private void SetUpPlot() 
     { 

      DateTime start = new DateTime(2017, 1, 1, 15, 0, 0); 
      DateTime start1 = new DateTime(2017, 1, 1, 15, 15, 0); 
      DateTime start2 = new DateTime(2017, 1, 1, 15, 20, 0); 
      DateTime start3 = new DateTime(2017, 1, 1, 15, 45, 0); 

      DateTime end = new DateTime(2017, 1, 1, 15, 10, 0); 
      DateTime end1 = new DateTime(2017, 1, 1, 15, 17, 0); 
      DateTime end2 = new DateTime(2017, 1, 1, 15, 30, 0); 
      DateTime end3 = new DateTime(2017, 1, 1, 15, 59, 0); 

      var model = new PlotModel(); 
      model.IsLegendVisible = false; 

      model.Axes.Add(new OxyPlot.Axes.DateTimeAxis() { Position = AxisPosition.Bottom }); 
      model.Axes.Add(new OxyPlot.Axes.CategoryAxis() { Position = AxisPosition.Left }); 
      plot.Model = model; 


      var series = new OxyPlot.Series.IntervalBarSeries { Title = "Series 1", StrokeThickness = 1 }; 
      model.Series.Add(series); 

      series.Items.Add(new IntervalBarItem { CategoryIndex = 0, Start = start.ToOADate(), End = end.ToOADate() }); 
      series.Items.Add(new IntervalBarItem { CategoryIndex = 0, Start = start1.ToOADate(), End = end1.ToOADate() }); 

      for (int i = 0; i < 10; i++) 
      { 
       var series2 = new OxyPlot.Series.IntervalBarSeries { Title = "Series "+i.ToString(), StrokeThickness = 1 }; 
       series2.Items.Add(new IntervalBarItem { CategoryIndex = i, Start = start2.AddHours(i).ToOADate(), End = end2.AddHours(i).ToOADate() }); 
       series2.Items.Add(new IntervalBarItem { CategoryIndex = i, Start = start3.AddHours(i).ToOADate(), End = end3.AddHours(i).ToOADate() }); 
       model.Series.Add(series2); 
      } 

     } 
    } 
} 

只是用於測試放一些其他的指標(如1)CategoryIndex

+0

嘗試設置爲類別斧標籤:categoryAxis.Labels.Add( 「A類」);對於每個CategoryIndex – Rekshino

+0

現在我得到一個NullReferenceexception OxyPlot.Series.XYAxisSeries.GetClippingRect() –

+0

它有幫助categoryAxis.Labels.Add(「..」)? – Rekshino

回答

2

它取決於你想要做什麼,但修復你的循環的方法之一是:

for (int i = 0; i < 10; i++) 
{ 
    var series2 = new OxyPlot.Series.IntervalBarSeries { Title = "Series " + i.ToString(), StrokeThickness = 1 }; 
    model.Series.Add(series2); 
    for (int j = 0; j < i; j++) 
     series2.Items.Add(new IntervalBarItem { CategoryIndex = j, Start = start2.AddHours(i).ToOADate(), End = end2.AddHours(i).ToOADate() }); 
} 

enter image description here

的核心問題是在這裏,你可以不加,例如,CategoryIndex = 8,如果你只是添加2 IntervalBarItem每個系列。

固定你的循環的另一種方式:

for (int i = 0; i < 10; i++) 
{ 
    var series2 = new OxyPlot.Series.IntervalBarSeries { Title = "Series " + i.ToString(), StrokeThickness = 1 }; 
    series2.Items.Add(new IntervalBarItem { CategoryIndex = 0, Start = start2.AddHours(i).ToOADate(), End = end2.AddHours(i).ToOADate() }); 
    series2.Items.Add(new IntervalBarItem { CategoryIndex = 1, Start = start3.AddHours(i).ToOADate(), End = end3.AddHours(i).ToOADate() }); 
    model.Series.Add(series2); 
} 

enter image description here

還有一,公正,以確保:

for (int i = 0; i < 10; i++) 
{ 
    var series2 = new OxyPlot.Series.IntervalBarSeries { Title = "Series " + i.ToString(), StrokeThickness = 1 }; 
    for (int j = 0; j < random.Next(0, i); j++) 
     series2.Items.Add(new IntervalBarItem { CategoryIndex = j, Start = start2.AddHours(i).ToOADate(), End = end2.AddHours(i).ToOADate() }); 
    model.Series.Add(series2); 
} 

enter image description here

編輯:一個系列爲一個類別:

for (int i = 0; i < 10; i++) 
     { 
      var series2 = new OxyPlot.Series.IntervalBarSeries { Title = "Series " + i.ToString(), StrokeThickness = 1 }; 
      model.Series.Add(series2); 
      for (int j = 0; j < i+1; j++) 
       series2.Items.Add(new IntervalBarItem { CategoryIndex = i, Start = start2.AddHours(j).ToOADate(), End = end2.AddHours(j).ToOADate() }); 
     } 

enter image description here

+0

這解決了將項目添加到特定索引的問題。巴士,你可以看到 - 該系列傳播到每個類別。但我想要ONE系列的一個類別。 –

+0

請參閱我的**編輯**。 – jsanalytics