如何在High Charts PIE数据中添加自定义颜色|切片并想要更改切片文本

时间:2015-05-07 11:49:35

标签: javascript jquery graph highcharts

任何人都可以建议我对每张High Charts Data进行着色。

我的代码下面也是一张图片。我想将自定义颜色放到每个数据片上(对于数据,如Firefox,Opera,chrome,IE等)。

并且还希望在此示例中删除系列名称('浏览器共享'),如果我将其设为false“name:false”然后显示系列一,我想在鼠标上显示它像“Firefox:45%”将鼠标悬停在Firefox切片上

$(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 个答案:

答案 0 :(得分:2)

我们可以指定报告应该如下的默认颜色集:

 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>

答案 1 :(得分:1)

要在工具提示中更改系列名称,您可以更改pointFormat并删除对series.name

的引用

示例:

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

小提琴:http://jsfiddle.net/avsdgek8/

有关颜色,请参阅已回答的主题:

highcharts: dynamically define colors in pie chart

How can I change the colors of my highcharts piechart?

另外:可以为每个数据点设置颜色 - http://jsfiddle.net/avsdgek8/1/ 或每个系列 - http://api.highcharts.com/highcharts#plotOptions.pie.colors