有没有办法从 wpf Livecharts Graph 中的图例中删除一个 lineseries?
Is there a way to remove just one lineseries from the legend in a wpf Livecharts Graph?
所以我使用 wpf 的 Livecharts 并制作了一个包含多个 LineSeries 和 StackedAreas 的 SeriesCollection。到目前为止一切顺利。
我在一个图表中显示所有图形,因此每条线或堆叠区域都在图例中列出。我的问题是,我只需要从图例中删除一个 LineSeries。
在 LiveCharts 的纪录片中,只有一种方法可以删除整个图例或制作自定义图例,但这并不是我所需要的。我想一定有一种简单的方法可以从图例中删除单个图表。
您必须通过覆盖 CartesianChart.ChartLegend
的 ControlTemplate
来覆盖默认图例。
然后通过在您的模型中引入一个新集合来创建您自己的图例,该集合包含您希望为其创建图例的所有 LineSeries
。
此示例使用 ListBox
来保存图例项目并覆盖 ListBox.ItemsTemplate
以实际设计项目。
型号
public class PointShapeLineExample
{
public PointShapeLineExample()
{
InitializeComponent();
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double> { 4, 6, 5, 2 ,4 }
},
new LineSeries
{
Title = "Series 2",
Values = new ChartValues<double> { 6, 7, 3, 4 ,6 },
PointGeometry = null
},
new LineSeries
{
Title = "Series 3",
Values = new ChartValues<double> { 4,2,7,2,7 },
PointGeometry = DefaultGeometries.Square,
PointGeometrySize = 15
}
};
Labels = new[] {"Jan", "Feb", "Mar", "Apr", "May"};
YFormatter = value => value.ToString("C");
//modifying the series collection will animate and update the chart
SeriesCollection.Add(new LineSeries
{
Title = "Series 4",
Values = new ChartValues<double> {5, 3, 2, 4},
LineSmoothness = 0, //0: straight lines, 1: really smooth lines
PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
PointGeometrySize = 50,
PointForeground = Brushes.Gray
});
//modifying any series values will also animate and update the chart
SeriesCollection[3].Values.Add(5d);
// Only create a legend for the first two series
LegendSeries = new List<LineSeries>(SeriesCollection.Take(2));
}
public IEnumerable<LineSeries> LegendSeries { get; set; }
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
public Func<double, string> YFormatter { get; set; }
}
查看
<Window>
<Window.DataContext>
<PointShapeLineExample />
</Window.DataContext>
<CartesianChart Series="{Binding SeriesCollection}"
LegendLocation="Left" >
<CartesianChart.ChartLegend>
<DefaultLegend>
<DefaultLegend.Template>
<ControlTemplate TargetType="DefaultLegend">
<ListBox ItemsSource="{Binding LegendSeries}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
IsHitTestVisible="False">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type LineSeries}">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Stroke}"
Height="12"
Width="12"
Margin="0,0,4,0" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ControlTemplate>
</DefaultLegend.Template>
</DefaultLegend>
</CartesianChart.ChartLegend>
<CartesianChart.AxisY>
<Axis Title="Sales" LabelFormatter="{Binding YFormatter}" />
</CartesianChart.AxisY>
<CartesianChart.AxisX>
<Axis Title="Month" Labels="{Binding Labels}" />
</CartesianChart.AxisX>
</CartesianChart>
</Window>
所以我使用 wpf 的 Livecharts 并制作了一个包含多个 LineSeries 和 StackedAreas 的 SeriesCollection。到目前为止一切顺利。
我在一个图表中显示所有图形,因此每条线或堆叠区域都在图例中列出。我的问题是,我只需要从图例中删除一个 LineSeries。
在 LiveCharts 的纪录片中,只有一种方法可以删除整个图例或制作自定义图例,但这并不是我所需要的。我想一定有一种简单的方法可以从图例中删除单个图表。
您必须通过覆盖 CartesianChart.ChartLegend
的 ControlTemplate
来覆盖默认图例。
然后通过在您的模型中引入一个新集合来创建您自己的图例,该集合包含您希望为其创建图例的所有 LineSeries
。
此示例使用 ListBox
来保存图例项目并覆盖 ListBox.ItemsTemplate
以实际设计项目。
型号 public class PointShapeLineExample { public PointShapeLineExample() { InitializeComponent();
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double> { 4, 6, 5, 2 ,4 }
},
new LineSeries
{
Title = "Series 2",
Values = new ChartValues<double> { 6, 7, 3, 4 ,6 },
PointGeometry = null
},
new LineSeries
{
Title = "Series 3",
Values = new ChartValues<double> { 4,2,7,2,7 },
PointGeometry = DefaultGeometries.Square,
PointGeometrySize = 15
}
};
Labels = new[] {"Jan", "Feb", "Mar", "Apr", "May"};
YFormatter = value => value.ToString("C");
//modifying the series collection will animate and update the chart
SeriesCollection.Add(new LineSeries
{
Title = "Series 4",
Values = new ChartValues<double> {5, 3, 2, 4},
LineSmoothness = 0, //0: straight lines, 1: really smooth lines
PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
PointGeometrySize = 50,
PointForeground = Brushes.Gray
});
//modifying any series values will also animate and update the chart
SeriesCollection[3].Values.Add(5d);
// Only create a legend for the first two series
LegendSeries = new List<LineSeries>(SeriesCollection.Take(2));
}
public IEnumerable<LineSeries> LegendSeries { get; set; }
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
public Func<double, string> YFormatter { get; set; }
}
查看
<Window>
<Window.DataContext>
<PointShapeLineExample />
</Window.DataContext>
<CartesianChart Series="{Binding SeriesCollection}"
LegendLocation="Left" >
<CartesianChart.ChartLegend>
<DefaultLegend>
<DefaultLegend.Template>
<ControlTemplate TargetType="DefaultLegend">
<ListBox ItemsSource="{Binding LegendSeries}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
IsHitTestVisible="False">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type LineSeries}">
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Stroke}"
Height="12"
Width="12"
Margin="0,0,4,0" />
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ControlTemplate>
</DefaultLegend.Template>
</DefaultLegend>
</CartesianChart.ChartLegend>
<CartesianChart.AxisY>
<Axis Title="Sales" LabelFormatter="{Binding YFormatter}" />
</CartesianChart.AxisY>
<CartesianChart.AxisX>
<Axis Title="Month" Labels="{Binding Labels}" />
</CartesianChart.AxisX>
</CartesianChart>
</Window>