使用 XAML 绑定设置具有自定义标签的 LiveCharts 系列的颜色

Setting the colour of a LiveCharts Series that has custom labels using XAML Binding

设置系列填充和描边的典型方法在 Livecharts website 上有完美的解释。但是,为了为点设置自定义标签,您需要在视图模型中创建系列(如下所示)。这会阻止您在 XAML 中调用 Fill 或 Stroke,因为您没有像下面的示例那样创建每个系列。

<lvc:CartesianChart Name="Chart" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="15">
   <lvc:CartesianChart.Series>
      <lvc:LineSeries Values="{Binding Values}" LineSmoothness="1" StrokeThickness="10" 
                      DataLabels="True" FontSize="20" Foreground="#6B303030"
                      Stroke="White" Fill="Transparent" PointGeometrySize="0"/>
   </lvc:CartesianChart.Series>

我当前创建系列及其关联标签的代码。

ViewModel

ABValuesSC = new SeriesCollection
    {
        new LineSeries
        { Values = ABValues,
            DataLabels = true,
            FontSize = 14,
            //MinPointShapeDiameter = 15,
            StrokeDashArray = new System.Windows.Media.DoubleCollection {2},
            Fill = System.Windows.Media.Brushes.Transparent,
            LabelPoint = point =>
            {if(point.Key==0)
                {
                    return "A";
                }
            else
                {
                    return "B";
                }
            }
        },
        new ScatterSeries
        { Values = TriggerValues,
            DataLabels = true,
            FontSize = 14,
            MinPointShapeDiameter = 15,
            LabelPoint = point =>
            {if(point.Key==0)
                {
                    return "1";
                }
            else
                {
                    return "2";
                }
            }
        },
        new LineSeries
        { Values = NAVmatValues,
            LineSmoothness=0,
        }
    };

XAML

<lvc:CartesianChart  Series="{Binding ABValuesSC}"/> 

给你这个输出。

是否有一种方法可以访问图表的系列填充以将其从默认值更改为可绑定的?例如是否可以将颜色绑定到列表 或者是否有更好的方法为我的图表制作标签,以便我可以使用与此 post 顶部示例类似的方法?

由于您已经在使用 MVVM,因此请使用命令来操作颜色。在命令委托中,您所要做的就是访问系列集合并选择您要更改的系列。请注意,您必须将其转换为正确的系列类型。

((LineSeries)ABValuesSC [0]).Fill = Brushes.Aqua; //change fill of first series

这样您就可以操作任何 属性 您想要的系列,而不仅仅是填充。

与其以编程方式创建 SeriesCollection 并将其绑定到视图,还可以直接在 XAML 中定义(大部分)这些事物,并且只绑定您需要更改的事物你的视图模型。

移动到XAML

据我了解您的代码,您只想更改 ViewModel 中的值和填充,因此我们将您的 "configuration" 放在 XAML 中,看起来像这样:

<lvc:CartesianChart>
   <lvc:CartesianChart.Series>
      <lvc:LineSeries Values="{Binding ABValues}" DataLabels="True" FontSize="14" StrokeDashArray="1,1" Fill="{Binding ABColor}" LabelPoint="{Binding ABLabelPoint}"/>
      <lvc:ScatterSeries Values="{Binding TriggerValues}" DataLabels="True" FontSize="14" MinPointShapeDiameter="15" LabelPoint="{Binding TriggerLabelPoint}"/>
      <lvc:LineSeries Values="{Binding NAVmatValues}" LineSmoothness="0"/>
   </lvc:CartesianChart.Series>         
</lvc:CartesianChart>

LabelPoint 绑定

无法在 XAML 中设置 LabelPoint(或者至少我不知道如何设置),并且必须在您的 ViewModel 中作为 属性 提供(参见下面的代码)

class YourClass
{
    //Property to Bind
    public Func<ChartPoint,string> ABLabelPoint { get; set; }

    //Constructor
    public YourClass()
    {
        //Define LabelPoint, where 0 = A, 1 = B etc.
        //Or use your Code, doesent really matter
        ABLabelPoint = point => ((char)(point.X + 65)).ToString();
    }
}

(不要忘记将 Scatterseries LabelPoint 作为 TribberLabelPoint 属性)

值绑定

值现在已绑定,因此您必须像这样 属性 公开它们

public ChartValues<ValueType> ABValues { get; set; }

注意:将 ValueType 替换为使用的类型,例如。整数或字节。

填充颜色绑定

与值一样,填充颜色绑定到必须实现的 属性。确保在颜色更改时通知视图(参见 INotifyPropertyChanged

如果您的 class 已经实现了这个接口,它可能看起来像这样

//private Field
private SolidColorBrush _abColor = new SolidColorBrush(Colors.Green);

//Public Property which the XAML binds to
public SolidColorBrush ABColor
{
    get { return _abColor; }
    set
    {
        _abColor = value;
        OnPropertyChanged();
    }
}