实时图表更改默认图例

Livecharts change default legend

这是我的问题。

我正在使用 LiveCharts 来显示一些数据。 一切正常,直到显示代表所有显示数据的图例。

chart with legend

是否可以显示基于例如颜色(描边)或默认几何图形的图例?

在此先感谢您的帮助,

迭戈

我知道这有点晚了,但我想做类似的事情,所以这就是我能想到的。

您需要创建一个新集合并按照 Live Charts Energy Predictions 的示例进行操作。

首先需要为图表设置 LegendLocation="None"

<wpf:CartesianChart Hoverable="False" DataTooltip="{x:Null}" DisableAnimations="True" LegendLocation="None" Series="{Binding AllSeries, ElementName=FastGraphRoot}">

新图例代码(.xaml中重要的部分):

<ListBox Name="ListBox" MinHeight="250" ItemsSource="{Binding AllSeriesGroupByName, ElementName=FastGraphRoot}" Panel.ZIndex="1" BorderThickness="0" Background="Transparent">
        <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type wpf:LineSeries}">
                <TextBlock Text="{Binding Title}" 
                           Foreground="{Binding Stroke}"
                           FontSize="24"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <ContentPresenter />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

绑定列表将来自原始系列但已分组,为此我使用了 属性:

private SeriesCollection _allSeriesGroupByName = new SeriesCollection();
public SeriesCollection AllSeriesGroupByName { get { return _allSeriesGroupByName; } set { _allSeriesGroupByName = value; OnPropertyChanged(); } }

你可以简单地用这个代码填充它(或者你认为更快的任何其他东西):

var col = collection.GroupBy(g => ((LineSeries)g).Stroke).Select(p => p.First());
AllSeriesGroupByName = new SeriesCollection();
foreach (var c in col)
{
    AllSeriesGroupByName.Add(c);
}