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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7737409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:23:21  来源:igfitidea点击:

set different colors for each column in highcharts

javascripthighcharts

提问by user750487

I need to set different colors for each column in Highcharts graph dynamically. My Highcharts graph is:

我需要为 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'
             }
     }
 };

The data is set this way:

数据是这样设置的:

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);

How can I dynamically set different colors for each data point in this loop?

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

回答by Jér?me

Here is another solution with the latest version of Highcharts (currently 3.0).

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

Set the colorByPointoption to true and define the color sequencethat you want.

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

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

Here is an examplebased upon Highcharts Column with rotated labels demo

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

回答by escouser

When you add the value to the series.data you can also set the color using the point options e.g

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

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

For more details about the point options see https://api.highcharts.com/class-reference/Highcharts.Point#color

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

回答by Mahalakshmi Chinnadurai

Try either of these approaches:

尝试以下任一方法:

Approach 1:

方法一:

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

Approach 2:

方法二:

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]

            }]
        }]
    });

回答by Ravi Anand

i had colors from API response and 2 series. second series with dynamic colors.

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

following is sample to set dynamic colors while series mapping.

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

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);

you could also read more about Highcharts series Point and Marker

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