从 ColumnSeries 中删除项目
Remove item from ColumnSeries
如何使用 Oxyplot 从给定的 ColumnSeries 中删除项目?
鉴于代码是库本身提供的示例(稍作修改),以及某种删除事件(我自己可以弄清楚),我将如何删除一个项目(专栏)从图中?
如果我只是从 bar.Items 列表中删除该项目,标签将不会消失。从 tmp.Axes[0].ActualLabels(即 CategoryAxis)中删除它不会 "refresh" 视图,并且标签保留在那里。这种情况有什么解决办法吗?我已经设法用折线图和饼图做到了,但我在第一列上苦苦挣扎。
构建图形的代码隐藏:
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
中都被绑定(当然,如果您需要更多代表 Value2
和 Value3
Items
的值 class 您可以将新的 ColumnSeries 添加到您的 PlotModel 的系列中)
然后我在Window中添加了一个按钮,并在代码隐藏中添加:
Items.RemoveAt(0);
Model1.InvalidatePlot(true);
这会更新 Plot,删除(每次)第一个 ColumnSeries
,包括 CategoryAxis
中的 Label。
Window 代码隐藏:
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; }
}
}
<<--------------------------------编辑-------- ---------------------->>
如果每个类别只需要一个栏,则每个项目只需要一个值。然后您可以(如前例所示)删除甚至添加项目 from/to 集合(使用 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; }
}
}
如何使用 Oxyplot 从给定的 ColumnSeries 中删除项目?
鉴于代码是库本身提供的示例(稍作修改),以及某种删除事件(我自己可以弄清楚),我将如何删除一个项目(专栏)从图中?
如果我只是从 bar.Items 列表中删除该项目,标签将不会消失。从 tmp.Axes[0].ActualLabels(即 CategoryAxis)中删除它不会 "refresh" 视图,并且标签保留在那里。这种情况有什么解决办法吗?我已经设法用折线图和饼图做到了,但我在第一列上苦苦挣扎。
构建图形的代码隐藏:
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
中都被绑定(当然,如果您需要更多代表 Value2
和 Value3
Items
的值 class 您可以将新的 ColumnSeries 添加到您的 PlotModel 的系列中)
然后我在Window中添加了一个按钮,并在代码隐藏中添加:
Items.RemoveAt(0);
Model1.InvalidatePlot(true);
这会更新 Plot,删除(每次)第一个 ColumnSeries
,包括 CategoryAxis
中的 Label。
Window 代码隐藏:
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; }
}
}
<<--------------------------------编辑-------- ---------------------->>
如果每个类别只需要一个栏,则每个项目只需要一个值。然后您可以(如前例所示)删除甚至添加项目 from/to 集合(使用 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; }
}
}