简体   繁体   English

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

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

The typical way of setting the Series Fill and Stroke are explained perfectly on the Livecharts website . Livecharts网站上完美解释了设置“系列填充”和“冲程”的典型方法。 However, in order to set custom labels for points you need to create the Series in the view model (Shown below). 但是,为了设置点的自定义标签,您需要在视图模型中创建系列(如下所示)。 This prevents you from being able to call Fill or Stroke in the XAML as you don't have each series being created like the example below. 由于您没有像下面的示例那样创建每个系列,因此这使您无法在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>

My current code which creates the series and its associated labels. 我当前的代码,用于创建系列及其相关标签。

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 XAML

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

Giving you this output. 给你这个输出。

在此处输入图片说明

Is there a method for accessing a series fill for the chart to change it from the default and have it be bindable? 是否有一种方法可以访问图表的系列填充,以将其从默认值更改并具有可绑定性? for example would it be possible to have the colours be bound to a list or is there a better way of making the labels for my chart such that i can use a similar method to the example at the top of this post? 例如,是否可以将颜色绑定到列表,或者是否有更好的方法为图表制作标签,以便可以使用与本文顶部示例相似的方法?

Instead of creating a SeriesCollection programmatically and bind it to the View, its possible to define (most) of these Things directly in the XAML and only bind the Things you need to change in your ViewModel. 除了可以通过编程方式创建SeriesCollection并将其绑定到View之外,还可以直接在XAML中定义(大多数)这些Things,而仅绑定需要在ViewModel中进行更改的Things。

Move to XAML 移至XAML

As far as I understood your code, you only want to change the Values and the Fill in your ViewModel, so we put your "configuration" in the XAML which looks something like this: 据我了解您的代码,您只想更改ViewModel中的Values和Fill,因此我们将您的“配置”放入如下所示的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 Binding LabelPoint绑定

The LabelPoint cant be set (or at least i dont know how) in the XAML and must be provided as a Property in your ViewModel (see Code below) 无法在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();
    }
}

(Dont forget to do this for the Scatterseries LabelPoint as TribberLabelPoint Property) (不要忘记将Scatterseries LabelPoint作为TribberLabelPoint属性来执行此操作)

Values Binding 值绑定

The Values are now bound, therefore you must expose them as a Property like this 现在已绑定了值,因此您必须像这样将其公开为属性

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

Note: Replace ValueType with the used Type, eg. 注意:将ValueType替换为使用的Type,例如。 int or byte. 整数或字节。

Fill Color Binding 填充颜​​色绑定

Like the Values, the Fill color is bound to a Property which must be implemented. 像值一样,填充颜色绑定到必须实现的属性。 Make sure you notify the View when the Color Changes (see INotifyPropertyChanged ) 确保在颜色更改时通知视图(请参阅INotifyPropertyChanged

If your class already has this interface implemented it could look like this 如果您的班级已经实现了此接口,则可能看起来像这样

//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();
    }
}

As you already use MVVM, use a command to manipulate color. 由于您已经在使用MVVM,请使用命令来操纵颜色。 In the Command delegate all you have to do is to access the series collection and pick out the series you want to change. 在Command委托中,您所需要做的就是访问系列集合并选择要更改的系列。 Note that you have to cast it to the right series type. 请注意,您必须将其转换为正确的系列类型。

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

This way you can manipulate any property of the series you want, not just fill. 这样,您可以操纵想要的系列的任何属性,而不仅仅是填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM