【问题标题】:IndexOutOfRangeException when trying to set CategoryIndex in a loop尝试在循环中设置 CategoryIndex 时出现 IndexOutOfRangeException
【发布时间】:2017-04-25 07:09:46
【问题描述】:

我试图在一行中为多个类别绘制许多 IntervalBarItems。在 Oxyplot Categoryaxis 中,每个 IntervalBarSeries 将一个 IntervalBarItem 绘制到该行中。所以通常如果我有三个系列,每个系列有 4 个项目 - 我得到 4 个类别和 3 个 BarItems(Series1、Series2、Series3)。但我想要 3 个类别 - 每个类别对应一个系列,其中包含 4 个项目 - 我可以通过为每个项目设置 CategoryIndex-Property 来获得此结果。在这里我得到 IndexOutOfRangeException - 通过在循环中设置索引属性。

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

【问题讨论】:

  • 尝试设置分类轴的标签:categoryAxis.Labels.Add("Category A");对于每个 CategoryIndex
  • 现在我得到了一个对 OxyPlot.Series.XYAxisSeries.GetClippingRect() 的 NullReference 异常
  • 对 categoryAxis.Labels.Add("..") 有帮助吗?

标签: c# .net wpf xaml oxyplot


【解决方案1】:

这取决于您到底想做什么,但修复循环的一种方法是:

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

【讨论】:

  • 这解决了将项目添加到特定索引的问题。如您所见,公共汽车 - 该系列扩展到每个类别。但我想要一个类别的一个系列。
  • 请看我的编辑
猜你喜欢
  • 1970-01-01
  • 2021-03-16
  • 2021-01-03
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多