LiveCharts ColumnSeries在运行时更新颜色 [英] LiveCharts ColumnSeries update Colours on Runtime

查看:1042
本文介绍了LiveCharts ColumnSeries在运行时更新颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF上使用LiveCharts 0.9.7和1.2.9齿轮版本来显示ColumnSeries上的填充数据.

I am using LiveCharts 0.9.7 and 1.2.9 Geared Version on WPF to show population data on ColumnSeries.

这是我的情况:开始时,我用蓝色填充列.我想在运行时更改单个列的值的颜色.

Here is my scenario: At the beginning, I am filling Columns with Blue. I want to change the colour of a single Column's Value on runtime.

我尝试使用SeriesCollection[0].Values[0]达到单个值,但是没有填充"或颜色"选项,它只是Double.

I tried to reach a single value with SeriesCollection[0].Values[0] but, there no Fill or Colour option, it is just Double.

我也尝试转换SeriesCollection[0] to ColumnSeries,但是我无法达到结果.是否可以在运行时更新单个值的颜色?

I also tried to cast SeriesCollection[0] to ColumnSeries but, I couldn't achieve the result. Is it possible to update a single value's colour on runtime?

public SeriesCollection SeriesCollection { get; set; } = new SeriesCollection
{
  new ColumnSeries
  {
    Title = "Population of Bodrum",
    Values = new ChartValues<double> { 1500, 2500, 3700, 2000, 1000},
    Fill = Brushes.Blue
  }
};

推荐答案

您可以通过将CartesianMapper分配给ColumnSeries.Configuration来指定配置.

You can specify a configuration by assigning a CartesianMapper to ColumnSeries.Configuration.

以下示例将给定图表的第三列的颜色从蓝色更改为红色:

The following example changes the color of the third column of your given chart from blue to red:

public class ChartDataModel
{
  public ChartDataModel()
  {
    this.BlueSeries = new ColumnSeries()
    {
      Title = "Population of Bodrum",
      Values = new ChartValues<double> { 1500, 2500, 3700, 2000, 1000 },
      Fill = Brushes.Blue
    };
    this.SeriesCollection = new SeriesCollection() { this.BlueSeries };
  }

  private void ChangeThirdChartPointColorToRed()
  {
    CartesianMapper<double> mapper = Mappers.Xy<double>()
      .X((value, index) => index)
      .Y(value => value)
      .Fill((value, index) => index == 2 ? Brushes.Red : Brushes.Blue);

    // Dynamically set the third chart point color to red
    this.BlueSeries.Configuration = mapper;
  }

  // The actual chart data source
  public SeriesCollection SeriesCollection { get; set; }

  private ColumnSeries BlueSeries { get; set; }
}

您还可以通过指定相应的谓词,使用CartesianMapper根据点的 y 值更改颜色.要绘制所有超过2,000红色的值,可以使用以下映射器:

You can also use the CartesianMapper to change the color of a point according to its y value by specifying a corresponding predicate. To draw all values that exceed a value of 2,000 red you could use the following mapper:

 CartesianMapper<double> mapper = Mappers.Xy<double>()
   .X((value, index) => index)
   .Y(value => value)
   .Fill((value, index) => value > 2000 ? Brushes.Red : Brushes.Blue);

这篇关于LiveCharts ColumnSeries在运行时更新颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆