代码之家  ›  专栏  ›  技术社区  ›  Ribas

Livecharts更改默认图例

  •  0
  • Ribas  · 技术社区  · 8 年前

    这是我的问题。

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

    chart with legend

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

    提前感谢您的帮助,

    迭戈

    1 回复  |  直到 7 年前
        1
  •  4
  •   doogie    8 年前

    我知道这有点晚,但我想做一些类似的事情,所以这里是我能够想出的。

    您需要创建一个新集合,并遵循以下示例: 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);
    }