我正在尝试在一行和几个类别中绘制许多IntervalBarItem。在Oxyplot中,Categoryaxis将ForevalBarSeries中的一个IntervalBarItem绘制到该行中。因此,如果我有三个系列,每个有4个项目 - 我得到4个类别,3个BarItems(Series1,Series2,Series3)。但是我想要3个类别 - 每个类别对应一个系列中的4个项目 - 我可以通过为每个项目设置CategoryIndex-Property来获得此结果。在这里,我得到IndexOutOfRangeException - 通过在循环中设置Index-Property。
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 :(得分: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() });
}
这里的核心问题是,如果每个系列只添加2 CategoryIndex = 8
,则无法添加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);
}
还有一个,只是为了确保:
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() });
}