从ColumnSeries中删除项
本文关键字:删除 ColumnSeries | 更新日期: 2023-09-27 18:14:44
使用Oxyplot,我将如何从给定的ColumnSeries中删除一个项目?
给定的代码是在库本身提供的例子(有一个小的修改),以及某种类型的删除事件(我可以弄清楚自己)我将如何从图中删除一个项目(列)?
如果我只是从栏中删除项目。项目列表,标签将不会消失。将其从tmp.Axes[0]中移除。ActualLabels(它是CategoryAxis)不会"刷新"视图,而Label仍然在那里。这种情况有什么解决办法吗?我已经设法用线形图和饼状图做到了,但是我在用列图时遇到了困难。
构建图的代码隐藏:
namespace ColumnSeriesDemo
{
using System.Collections.ObjectModel;
using System.Windows;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
using WpfExamples;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
[Example("Shows column series.")]
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Create some data
this.Items = new Collection<Item>
{
new Item {Label = "Apples", Value1 = 37, Value2 = 12, Value3 = 19},
new Item {Label = "Pears", Value1 = 7, Value2 = 21, Value3 = 9},
new Item {Label = "Bananas", Value1 = 23, Value2 = 2, Value3 = 29}
};
// Create the plot model
var tmp = new PlotModel { Title = "Column series", LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightTop, LegendOrientation = LegendOrientation.Vertical };
// Add the axes, note that MinimumPadding and AbsoluteMinimum should be set on the value axis.
tmp.Axes.Add(new CategoryAxis { ItemsSource = this.Items, LabelField = "Label" });
tmp.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, AbsoluteMinimum = 0 });
// Add the series, note that the BarSeries are using the same ItemsSource as the CategoryAxis.
ColumnSeries bar = new ColumnSeries();
tmp.Series.Add(bar);
bar.Items.Add(new ColumnItem { Color = OxyPlot.OxyColors.Yellow, Value = this.Items[0].Value3, CategoryIndex = 0 });
bar.Items.Add(new ColumnItem { Color = OxyPlot.OxyColors.Green, Value = this.Items[0].Value2, CategoryIndex = 2 });
bar.Items.Add(new ColumnItem { Color = OxyPlot.OxyColors.Red, Value = this.Items[0].Value1, CategoryIndex = 3 });
this.Model1 = tmp;
this.DataContext = this;
}
public Collection<Item> Items { get; set; }
public PlotModel Model1 { get; set; }
}
public class Item
{
public string Label { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
}
}
我真的不确定我知道你在做什么,但我希望这对你有帮助:
我拿了样本,在你的代码和样本周围玩了一点。我认为你的代码的问题是,你有一个绑定在你的CategoryAxis
,但数据不是添加绑定,而是直接在你的ColumnSeries
。使用您的代码,我保留了第一部分,而不是ColumnSeries bar = new ColumnSeries()
,我做了:
ColumnSeries bar = new ColumnSeries
{
FillColor = OxyPlot.OxyColors.Yellow,
ValueField = "Value1",
Title = "Value1",
ItemsSource = Items
};
tmp.Series.Add(bar);
这样,Items
中的数据在您的CategoryAxis
和ColumnSeries
中都被绑定(当然,如果您需要更多的列来表示Items
类的Value2
和Value3
值,您可以将新的ColumnSeries添加到您的PlotModel系列中)
然后我给窗口添加了一个按钮,并在代码后面添加了:
Items.RemoveAt(0);
Model1.InvalidatePlot(true);
这更新了Plot(每次)移除第一个ColumnSeries
,包括CategoryAxis
中的标签。
窗口后台代码:
using System.Collections.ObjectModel;
using System.Windows;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
namespace OxyPlot_TEST
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
// Create some data
this.Items = new Collection<Item>
{
new Item {Label = "Apples", Value1 = 37, Value2 = 12, Value3 = 19},
new Item {Label = "Pears", Value1 = 7, Value2 = 21, Value3 = 9},
new Item {Label = "Bananas", Value1 = 23, Value2 = 2, Value3 = 29}
};
// Create the plot model
var tmp = new PlotModel { Title = "Column series", LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightTop, LegendOrientation = LegendOrientation.Vertical };
// Add the axes, note that MinimumPadding and AbsoluteMinimum should be set on the value axis.
tmp.Axes.Add(new CategoryAxis { ItemsSource = this.Items, LabelField = "Label" });
tmp.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, AbsoluteMinimum = 0 });
ColumnSeries bar = new ColumnSeries
{
FillColor = OxyPlot.OxyColors.Yellow,
ValueField = "Value1",
Title = "Value1",
ItemsSource = Items
};
ColumnSeries bar1 = new ColumnSeries
{
FillColor = OxyPlot.OxyColors.Green,
ValueField = "Value1",
Title = "Value1",
ItemsSource = Items
};
ColumnSeries bar2 = new ColumnSeries
{
FillColor = OxyPlot.OxyColors.Red,
ValueField = "Value1",
Title = "Value1",
ItemsSource = Items
};
tmp.Series.Add(bar);
tmp.Series.Add(bar1);
tmp.Series.Add(bar2);
this.Model1 = tmp;
this.DataContext = this;
}
public Collection<Item> Items { get; set; }
public PlotModel Model1 { get; set; }
private void button1_Click(object sender, RoutedEventArgs e)
{
Items.RemoveAt(0);
Model1.InvalidatePlot(true);
}
}
public class Item
{
public string Label { get; set; }
public double Value1 { get; set; }
public double Value2 { get; set; }
public double Value3 { get; set; }
}
}
& lt; & lt ;--------------------------------- 编辑 --------------------------------->>
如果每个类别只需要一个bar,则每个Item只需要一个Value。然后你可以(像前面的例子一样)从集合中删除或甚至添加项目(使用InvalidatePlot
更新情节)。后台代码:
using System.Collections.ObjectModel;
using System.Windows;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;
namespace OxyPlot_TEST
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
// Create some data
this.Items = new Collection<Item>
{
new Item {Label = "Apples", Value1 = 37},
new Item {Label = "Pears", Value1 = 7},
new Item {Label = "Bananas", Value1 = 23}
};
// Create the plot model
var tmp = new PlotModel { Title = "Column series", LegendPlacement = LegendPlacement.Outside, LegendPosition = LegendPosition.RightTop, LegendOrientation = LegendOrientation.Vertical };
// Add the axes, note that MinimumPadding and AbsoluteMinimum should be set on the value axis.
tmp.Axes.Add(new CategoryAxis { ItemsSource = this.Items, LabelField = "Label" });
tmp.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, AbsoluteMinimum = 0 });
ColumnSeries bar = new ColumnSeries
{
FillColor = OxyPlot.OxyColors.Black,
ValueField = "Value1",
Title = "Value1",
ItemsSource = Items
};
tmp.Series.Add(bar);
this.Model1 = tmp;
this.DataContext = this;
}
public Collection<Item> Items { get; set; }
public PlotModel Model1 { get; set; }
private void button1_Click(object sender, RoutedEventArgs e)
{
Items.RemoveAt(0);
Model1.InvalidatePlot(true);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Items.Add(new Item()
{
Label = "Strawberrys", Value1 = 55
});
Model1.InvalidatePlot(true);
}
}
public class Item
{
public string Label { get; set; }
public double Value1 { get; set; }
}
}