尝试在循环中设置 CategoryIndex 时出现 IndexOutOfRangeException
IndexOutOfRangeException when trying to set CategoryIndex in a loop
我试图在一行中为多个类别绘制许多 IntervalBarItems。在 Oxyplot Categoryaxis 中,为每个 IntervalBarSeries 绘制一个 IntervalBarItem 到线中。所以通常如果我有三个系列,每个系列有 4 个项目 - 我得到 4 个类别和 3 个 BarItems(系列 1、系列 2、系列 3)。但我想要 3 个类别 - 每个类别对应一个系列,该系列包含该行中的 4 个项目 - 我可以通过为每个项目设置 CategoryIndex-属性 来获得此结果。在这里我得到了 IndexOutOfRangeException - 通过在 Loop.
中设置 Index-属性
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
这取决于你到底想做什么,但修复循环的一种方法是:
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() });
}
这里的核心问题是,如果您每个系列只添加 2 IntervalBarItem
,则无法添加,例如 CategoryIndex = 8
。
修复循环的另一种方法:
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);
}
还有一个,只是为了确保:
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);
}
编辑: 一个类别一个系列:
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() });
}
我试图在一行中为多个类别绘制许多 IntervalBarItems。在 Oxyplot Categoryaxis 中,为每个 IntervalBarSeries 绘制一个 IntervalBarItem 到线中。所以通常如果我有三个系列,每个系列有 4 个项目 - 我得到 4 个类别和 3 个 BarItems(系列 1、系列 2、系列 3)。但我想要 3 个类别 - 每个类别对应一个系列,该系列包含该行中的 4 个项目 - 我可以通过为每个项目设置 CategoryIndex-属性 来获得此结果。在这里我得到了 IndexOutOfRangeException - 通过在 Loop.
中设置 Index-属性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
这取决于你到底想做什么,但修复循环的一种方法是:
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() });
}
这里的核心问题是,如果您每个系列只添加 2 IntervalBarItem
,则无法添加,例如 CategoryIndex = 8
。
修复循环的另一种方法:
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);
}
还有一个,只是为了确保:
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);
}
编辑: 一个类别一个系列:
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() });
}