How can I put custom color in High Charts PIE data

2019-08-08 08:18发布

Can anyone suggest me about colouring to each slice of High Charts Data.

My Code is given below also a image there. I want to put my custom color to each slice of data (for data are like Firefox, opera, chrome, IE etc.. )

and also want to remove series name ('Browser share') in this example, if I put it false "name: false" then is showing series one, I want to show it like "Firefox: 45%" on mouse hover on Firefox slice

$(function () {
    $('#graph').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2014'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ['Firefox',   45.0],
                ['IE',       26.8],
                {
                    name: 'Chrome',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Safari',    8.5],
                ['Opera',     6.2],
                ['Others',   0.7]
            ]
        }]
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/4.1.5/highcharts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/4.1.5/highcharts-more.src.js"></script>


<div id="graph" style="height: 300px; width:100%;"></div>

enter image description here

2条回答
Juvenile、少年°
2楼-- · 2019-08-08 08:49

To change series name in tooltip you can change pointFormat and remove reference to series.name

Example:

 tooltip: {
            pointFormat: '<b>{point.percentage:.1f}%</b>'
        },

Fiddle: http://jsfiddle.net/avsdgek8/

For colors see answered topics:

highcharts: dynamically define colors in pie chart

How can I change the colors of my highcharts piechart?

Also: color can be set per data point - http://jsfiddle.net/avsdgek8/1/ or per series - http://api.highcharts.com/highcharts#plotOptions.pie.colors

查看更多
走好不送
3楼-- · 2019-08-08 08:59

We can specify default color set which the report should as follows:

 Highcharts.getOptions().plotOptions.pie.colors=['#2f7ed8', '#0d233a',
   '#8bbc21', '#910000', '#1aadce', '#492970', '#f28f43', '#77a1e5',
    '#c42525', '#a6c96a'];

$(function () {

//Specify color set as follows
Highcharts.getOptions().plotOptions.pie.colors=['#2f7ed8', '#0d233a', '#8bbc21', '#910000', '#1aadce', '#492970', '#f28f43', '#77a1e5', '#c42525', '#a6c96a'];

    $('#graph').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2014'
        },
        tooltip: {
            pointFormat: '<b>{point.percentage:.1f}%</b>' // updated tool tip
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ['Firefox',   45.0],
                ['IE',       26.8],
                {
                    name: 'Chrome',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Safari',    8.5],
                ['Opera',     6.2],
                ['Others',   0.7]
            ]
        }]
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/4.1.5/highcharts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/4.1.5/highcharts-more.src.js"></script>


<div id="graph" style="height: 300px; width:100%;"></div>

查看更多
登录 后发表回答