为 highcharts 中的每一列设置不同的颜色

IT技术 javascript highcharts
2021-02-25 07:52:00

我需要为 Highcharts 图表中的每一列动态设置不同的颜色。我的 Highcharts 图表是:

options = {
         chart: {
             renderTo: 'chart',
             type: 'column',
             width: 450
         },
         title: {
             text: 'A glance overview at your contest’s status'
         },
         xAxis: {
             categories: ['Approved Photos', 'Pending Approval Photos', 
                          'Votes', 'Entrants'],
             labels: {
                 //rotation: -45,
                 style: {
                     font: 'normal 9px Verdana, sans-serif, arial'
                 }
             }
         },
         yAxis: {
             allowDecimals: false,
             min: 0,
             title: {
                 text: 'Amount'
             }
         },
         legend: {
             enabled: false
         },
         series: []
     };
     series = {
         name: "Amount",
         data: [],
         dataLabels: {
             enabled: true,
             color: '#000000',
             align: 'right',
             x: -10,
             y: 20,
             formatter: function () {
                 return this.y;
             },
             style: {
                 font: 'normal 13px Verdana, sans-serif'
             }
     }
 };

数据是这样设置的:

for (var i in Data) {
  if (parseInt(Data[i]) != 0) {
    series.data.push(parseInt(Data[i]));
  } else {
    series.data.push(null);
  }
}
options.series.push(series);
chart = new Highcharts.Chart(options);

如何为此循环中的每个数据点动态设置不同的颜色?

4个回答

这是最新版本的 Highcharts(当前为 3.0)的另一个解决方案。

colorByPoint选项设置为 true 并定义所需的颜色序列

options = {
    chart: {...},
    plotOptions: {
        column: {
            colorByPoint: true
        }
    },
    colors: [
        '#ff0000',
        '#00ff00',
        '#0000ff'
    ]
}

下面是一个例子基于Highcharts柱与旋转标签演示

有什么办法可以为多个系列做这样的事情吗?不是全局的?我有一个折线图和列,但我希望我的线条颜色与我为列系列设置的颜色不同。在柱状图中,某些列也需要不同的颜色。
2021-04-19 07:52:00
正是我需要的,而且比公认的答案更容易实施。谢谢!
2021-05-14 07:52:00
请注意,此解决方案适用于条形图和柱形图。只需将 plotOptions 数组中的 'column' 更改为 'bar'
2021-05-16 07:52:00

当您将值添加到 series.data 时,您还可以使用点选项设置颜色,例如

series.data.push({ y: parseInt(Data[i]), color: '#FF0000' });

有关点选项的更多详细信息,请参阅https://api.highcharts.com/class-reference/Highcharts.Point#color

谢谢你的回答!
2021-05-04 07:52:00
你好!又是我...如何通过数据设置列宽。例如:如果数据是 23 我想设置列宽 100px,如果数据是 123 它将是 110px。对于列中间的推杆数据,我需要此选项。
2021-05-05 07:52:00
抱歉,我不确定你会如何在 highcharts 中做到这一点。
2021-05-08 07:52:00
您可以 plotOptions->series->linewidth 来设置宽度。但是,这将固定图表中所有条的宽度。也许您可以使用最大值来设置它。highcharts.com/ref/#plotOptions-series--lineWidth
2021-05-08 07:52:00
该文档链接已损坏
2021-05-09 07:52:00

尝试以下任一方法:

方法一:

Highcharts.setOptions({ colors: ['#3B97B2', '#67BC42', '#FF56DE', '#E6D605', '#BC36FE'] });

方法二:

var colors = ['#3B97B2', '#67BC42', '#FF56DE', '#E6D605', '#BC36FE', '#000'];

 $('#bar_chart').highcharts({
        chart: {
            type: 'column'              
        },
        title: {
            text: ''
        },
        subtitle: {
            text: ''
        },
        xAxis: {
            type: 'category'
        },
        yAxis: {
            title: {
                text: ''
            }
        },
        legend: {
            enabled: false
        },
        plotOptions: {
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: false                       
                }
            }
        },         

        series: [{
            name: '',
            colorByPoint: true,
            data: [{
                name: 'Blue',
                y: 5.78,
                color: colors[0]

            }, {
                name: 'Green',
                y: 5.19,
                color: colors[1]

            }, {
                name: 'Pink',
                y: 32.11,
                color: colors[2]

            }, {
                name: 'Yellow',
                y: 10.04,
                color: colors[3]

            }, {
                name: 'Purple',
                y: 19.33,
                color: colors[4]

            }]
        }]
    });
有没有办法将颜色设置为不同颜色的数组?例如,我从我的数据中返回我想要的颜色,所以我想使用它而不是手动设置它,并且某些列具有某些颜色。
2021-04-25 07:52:00

我有来自 API 响应和 2 系列的颜色。具有动态色彩的第二个系列。

以下是在系列映射时设置动态颜色的示例。

const response = [{
    'id': 1,
    'name': 'Mango',
    'color': '#83d8b6',
    'stock': 12.0,
    'demand': 28,
  },
  {
    id ': 2,
    'name': 'Banana',
    'color': '#d7e900',
    'stock': 12.0,
    'demand': 28,
  }
];
let chartData: {
  categories: [],
  series: []
};
chartData.categories = response.map(x => x.name);
chartData.series.push({
  name: 'Series 1',
  type: 'column',
  data: response.map(x => x.demand)
});
chartData.series.push({
  name: 'Series 2',
  type: 'column',
  data: response.map(x => ({
    y: x.stock,
    color: x.color // set color here dynamically
  }))
});
console.log('chartData: ', chartData);

您还可以阅读有关Highcharts 系列点和标记的更多信息