使用Oxyplot,如何从给定的ColumnSeries中删除项目?
鉴于代码是库本身提供的示例(稍加修改),以及某种类型的Delete事件(我可以自己弄清楚)如何从中删除项目(列)图表?
如果我只是从bar.Items列表中删除该项目,那么Label就不会失败。从tmp.Axes [0]中删除它.ActualLabels(这是CategoryAxis)赢了"刷新"视图,标签仍然存在。这种情况有什么解决方案吗?我已经设法用Line和Pie Graphs来做,但是我在第一列中苦苦挣扎。
构建图表的代码隐藏:
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; }
}
}
答案 0 :(得分:1)
我不确定我知道你想要做什么,但我希望这会对你有所帮助:
我拿了样本,然后围绕你的代码和样本玩了一下。我认为您的代码存在的问题是您的CategoryAxis
中有绑定,但数据不会添加Bind,而是直接添加到您的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
中(当然,如果您需要更多列代表Value2
和{{1}您可以将新的ColumnSeries添加到PlotModel系列中的Value3
类的值
然后我在窗口添加了一个Button,并在Code-Behind中添加了:
Items
这会更新Plot删除(每次)第一个 Items.RemoveAt(0);
Model1.InvalidatePlot(true);
,包括ColumnSeries
中的标签。
Window Code-Behind:
CategoryAxis
&lt;&lt; --------------------------------- EDIT --------- ------------------------&GT;&GT;
如果每个类别只需要一个条形,则每个项目只需要一个值。然后你可以(如上例所示)从集合中删除甚至添加项目(使用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; }
}
}
更新图表。代码隐藏:
InvalidatePlot