首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Highcharts增加一个类名,以便可以在style.css中唯一地设置片段图切片的样式

Highcharts增加一个类名,以便可以在style.css中唯一地设置片段图切片的样式
EN

Stack Overflow用户
提问于 2013-03-23 03:08:24
回答 1查看 1.3K关注 0票数 0

我正在创建一个HighCharts饼图,并希望对每个切片进行更精细的样式控制--我希望浅色切片具有深色dataLabels,反之亦然。

因此,我希望能够使用我的style.css文件设置切片的样式。我在dataLabels设置中插入了一个类名(slice),外加一个自定义函数,用于遍历我的所有slice并为它们提供唯一的类:

代码语言:javascript
复制
function colorSlices(chart) {
                var count = 1;
                $(".slice").each(function(){
                    $(this).addClass("slice-"+count);
                    count++;
                });
};

var chart;
    $(document).ready(function() {

        // Build the chart
        chart = new Highcharts.Chart({
            credits: { enabled: false },
            chart: {
                renderTo: 'container',
                exporting: { enabled: false },                
                events: {
                    redraw: function(event) {
                        colorSlices();
                    }
                },
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                shadow: true
                    },
            tooltip: {
                pointFormat: '',
                percentageDecimals: 1
            },
            legend: {
            useHTML: true,
            align: 'right',
            verticalAlign: 'middle',
            itemWidth: 260,
            borderColor: '#fff',
            width: 260,
            labelFormatter: function() {
                return '<div class="legend-item">' + this.name +'</div>';
                }
            },
            title: {
                text: ""
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    size:'100%',
                    cursor: 'pointer',
                    showInLegend: true,
                    shadow: true,
                    dataLabels: {
                        enabled: true,
                        distance: -40,
                        useHTML: true,
                        style: {
                                width: '100px'
                                },
                        color: '#fff',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<span class="slice">' + Highcharts.numberFormat(this.percentage,1,".",",") +' %</span>';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                 name: 'Income Investments',
                data: [
                    ['Other Industries',     19.3],
                    ['Media',   16.0],                                   
                    ['Materials',   13.6],
                    ['Software & Services', 10.2],
                    ['Retailing',    7.9],
                    ['Capital Goods',     6.5],
                    ['Healthcare Equipment & Services',     6.0],
                    ['Insurance',     5.7],
                    ['Technology Hardware & Equipment',     5.4],
                    ['Consumer Services',     4.8],
                    ['Telecommunication Services',     4.7]                    
                ]
            }],

        colors: [
                    '#132f55',
                    '#4d6d8a',
                    '#7f95aa',
                    '#b2bfcb',
                    '#d1dae2',
                    '#e5eaef',
                    '#7f7f7f',
                    '#9e9e9e',
                    '#c9c9c9',
                    '#bcbdc0',
                    '#eeefef'
                      ]
});


    })

我最终希望我的饼中的每个切片都有增量类,比如:- slice-1 - slice-2 - slice-3

我在某种程度上做到了这一点,但只有在图表调整大小时。我尝试在load事件中触发我的自定义函数,但没有任何反应。

我的小提琴:http://jsfiddle.net/6PbbR/262/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-26 14:17:00

将colorSlices()设置为load函数对我来说工作得很好。

http://jsfiddle.net/6PbbR/274/

代码语言:javascript
复制
events: {
    redraw: function(event) {
        colorSlices();
    },
    load: colorSlices
}

您还可以在格式化程序中使用this.point.x来分配类。我相信这将完成同样的事情,并减轻对事件的需求。

http://jsfiddle.net/6PbbR/280/

代码语言:javascript
复制
dataLabels: {
    formatter: function() {
        return '<span class="slice slice-'+(this.point.x+1)+'">' + 
        Highcharts.numberFormat(this.percentage,1,".",",") +' %</span>';
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15578092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档