【问题标题】:Highcharts Donut Chart text in center change on hover悬停时中心更改的Highcharts圆环图文本
【发布时间】:2013-03-27 22:05:53
【问题描述】:

我正在寻找一种方法,让圆环图中心的文本在悬停时发生变化。没有悬停状态时,中心会显示总金额(这里我代表放假天数,所以可能是15个放假天数)。悬停时,中心的文本需要与悬停的段匹配(可用、请求和休假日),但我不知道如何根据悬停更改中心的文本。有没有人有任何想法?任何帮助是极大的赞赏!!

http://jsfiddle.net/NVX3S/146/

<script src="http://code.highcharts.com/highcharts.js"></script>

<div id="container1" style="min-width: 300px; height: 300px; margin: 0 auto"></div>


$(function () {
    var colors = ['#8d62a0', '#ceb3d8', '#d5dddd'];
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container1',
            type: 'pie',
            height: 250,
            width: 250,
            borderRadius: 0
        },
        credits: {
            enabled: false
        },
        title: false,
        tooltip: false,
        plotOptions: {
            pie: {
                borderWidth: 6,
                startAngle: 90,
                innerSize: '55%',
                size: '100%',
                shadow: true,
                // {
                //     color: '#000000',
                //     offsetX: 0,
                //     offsetY: 2,
                //     opacity: 0.7,
                //     width: 3
                // },
                dataLabels: false,
                stickyTracking: false,
                states: {
                    hover: {
                        enabled: true
                    }
                }
            }
        },

        series: [{
            data: [
                {y:65, color: colors[0]},
                {y:15, color: colors[1]},
                {y:20, color: colors[2]}
            ]
            // data: [
            //     ['Firefox',   44.2],
            //     ['IE7',       26.6],
            //     ['IE6',       20],
            //     ['Chrome',    3.1],
            //     ['Other',    5.4]
            // ]
        }]
    },
    function(chart) { // on complete

        var xpos = '50%';
        var ypos = '53%';
        var circleradius = 102;


    // Render the text 
    chart.renderer.text('126.5', 103, 127).css({
            width: circleradius*2,
            color: '#4572A7',
            fontSize: '16px',
            textAlign: 'center'
      }).attr({
            // why doesn't zIndex get the text in front of the chart?
            zIndex: 999
        }).add();
    });
});

【问题讨论】:

    标签: highcharts


    【解决方案1】:

    使用points.events.mouseOver/mouseOut,例如:http://jsfiddle.net/NVX3S/147/

    代码:

                point: {
                    events: {
                        mouseOver: function(){
                            this.series.chart.innerText.attr({text: this.y});
                        }, 
                        mouseOut: function(){
                            this.series.chart.innerText.attr({text: 112});
                        }
                    }
                }
    

    innerText 只是自定义属性,创建方式如下:

    chart.innerText = chart.renderer.text('112', 112, 125).css({ ... }).add();
    

    【讨论】:

    • 非常感谢!这就像一个魅力。非常感谢您的帮助!
    • 在 jsfiddle 中你提到了 var circleradius = 102;你是怎么得到那个圆半径的?有什么方法可以动态获取吗?
    • 最好向问题的作者提出这个问题。这不是我的代码部分。我会这样计算内部位置:jsfiddle.net/NVX3S/904(只需使用标签的边界框和饼图的中心位置)。
    • @PawełFus 有没有想过让悬停效果在 innerSize 中也有效?
    • 在饼图中(空白区域,由innerSize 创建)我们没有SVG 形状,所以我们不能在HTML 中触发mouseover 事件。可能您需要将mouseover 绑定到容器并计算(?)如果有切片将悬停在哪个切片上..
    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多