【问题标题】:LiveCharts ColumnSeries fill colorLiveCharts ColumnSeries 填充颜色
【发布时间】:2022-01-10 03:48:17
【问题描述】:

我正在关注 LiveChart 文档中的 this 示例。代码如下所示:

<lvc:CartesianChart Margin="20" Series="{Binding SeriesCollection}" LegendLocation="Left">
        <lvc:CartesianChart.AxisX>
            <lvc:Axis Title="Salesman" Labels="{Binding Labels}"></lvc:Axis>
        </lvc:CartesianChart.AxisX>
        <lvc:CartesianChart.AxisY>
            <lvc:Axis Title="Sold Apps" LabelFormatter="{Binding Formatter}"></lvc:Axis>
        </lvc:CartesianChart.AxisY>
    </lvc:CartesianChart>

它看起来很完美,对我来说也很完美,但是有一个问题。在此示例中,如何更改 ColumnSeriesfill 颜色?正如我之前所说,我宁愿不实现另一个图表,因为它非常适合我,但我不知道如何更改 Fill 颜色。

我没有在 SO 中找到类似的问题。

编辑

工作解决方案:

<lvc:CartesianChart Margin="20" Series="{Binding SeriesCollection}" LegendLocation="Left">
        <lvc:CartesianChart.SeriesColors>
            <lvc:ColorsCollection>
                <Color>Green</Color>
            </lvc:ColorsCollection>
        </lvc:CartesianChart.SeriesColors>
        <lvc:CartesianChart.AxisX>
            <lvc:Axis Title="Salesman" Labels="{Binding Labels}"></lvc:Axis>
        </lvc:CartesianChart.AxisX>
        <lvc:CartesianChart.AxisY>
            <lvc:Axis Title="Sold Apps" LabelFormatter="{Binding Formatter}"></lvc:Axis>
        </lvc:CartesianChart.AxisY>
    </lvc:CartesianChart>

【问题讨论】:

    标签: wpf livecharts


    【解决方案1】:

    您可以在 XAML 中定义一组 Color 对象,它们将按声明顺序隐式映射到 ColumnSeries 对象:

    <lvc:CartesianChart x:Name="CartesianChart"
                        Series="{Binding SeriesCollection}">
      <lvc:CartesianChart.SeriesColors>
        <lvc:ColorsCollection>
          <Color>Red</Color>
          <Color>Orange</Color>
          <Color>LightSlateGray</Color>
        </lvc:ColorsCollection>
      </lvc:CartesianChart.SeriesColors>
    </lvc:CartesianChart>
    

    或者在 XAML 或 C# 中显式定义 ColumnSeriesFill

    public SeriesCollection SeriesCollection { get; set; } = new SeriesCollection
    {
      new ColumnSeries
      {
        Title = "Series A",
        Values = new ChartValues<double> { 10, 20, 30, 20, 10},
        Fill = Brushes.CornflowerBlue
      },
      new ColumnSeries
      {
        Title = "Series B",
        Values = new ChartValues<double> { 100, 200, 300, 200, 100 },
        Fill = Brushes.DarkOrange
      }
    };
    

    【讨论】:

    • 我相信您的解决方案有效,但您能解释一下如何在我的示例中添加特定颜色吗?如何编辑此代码以在我的解决方案中添加此 ColorsCollection?因为当我像你提到的那样添加这行代码时(lvc:ColorsCollection)我的图表没有显示
    • 您能否使用ColorsCollection 更新您的代码,以便我们看看有什么问题?
    • @michasaucer 你知道如何更改单列颜色吗?这个例子改变了整个列系列。我需要在运行时更改单列颜色。谢谢
    • 对不起,我不知道。我在一个项目中使用了 LiveCharts。
    • @fatihyildizhan 考虑使用专用的ColumnSeries 来实现此目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2014-10-27
    • 2018-12-12
    • 2021-06-17
    • 2022-11-06
    相关资源
    最近更新 更多