因此,我为wpf使用了Livecharts,并创建了一个SeriesCollection,它包含多个LineSeries和StackedAreas。到目前一切尚好。
我在一张图表中显示所有的图表,因此在图例中列出了每一行或每一个堆叠的区域。我的问题是,我只需要从图例中删除一个LineSeries。
在LiveCharts的纪录片中,只有一种方法可以移除整个传说,或者制作一个定制的传说,但这并不是我所需要的。我认为一定有一个简单的方法,只需从图例中删除单个图。
发布于 2020-04-07 15:01:03
您必须通过重写ControlTemplate of CartesianChart.ChartLegend来覆盖默认图例。
然后通过在模型中引入一个新集合来创建您自己的图例,该集合保存了您希望为其创建的所有LineSeries。
此示例使用ListBox保存图例项,并重写ListBox.ItemsTemplate以实际设计项目。
模型公共类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>https://stackoverflow.com/questions/61075234
复制相似问题