有没有一种方法可以从wpf Livecharts Graph中的图例中只删除一个行序列



所以我将Livecharts用于wpf,并制作了一个包含多个LineSeries和StackedAreas的SeriesCollection。到目前为止还不错。

我在一个图表中显示所有图形,因此每条线或堆叠区域都列在图例中。我的问题是,我只需要从传奇中删除一个系列。

在LiveCharts的纪录片中,只有一种方法可以删除整个传说或制作一个自定义的传说,但这并不完全是我所需要的。我想一定有一种简单的方法可以从图例中删除单个图形。

您必须通过覆盖CartesianChart.ChartLegendControlTemplate来覆盖默认图例
然后通过在模型中引入一个新的集合来创建您自己的图例,该集合包含您希望为其创建图例的所有LineSeries

本例使用ListBox来保存图例项,并覆盖ListBox.ItemsTemplate来实际设计这些项。

模型公共类PointShapeLineExample{公共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>

相关内容

  • 没有找到相关文章

最新更新