【问题标题】:Livecharts change default legend实时图表更改默认图例
【发布时间】:2018-03-09 23:02:46
【问题描述】:

这是我的问题。

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

chart with legend

是否可以根据例如颜色(描边)或 DefaultGeometries 显示图例?

提前感谢您的帮助,

迭戈

【问题讨论】:

    标签: wpf livecharts


    【解决方案1】:

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

    您需要创建一个新集合并按照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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2021-09-17
      • 2011-12-11
      • 1970-01-01
      相关资源
      最近更新 更多