简体   繁体   English

Highcharts甜甜圈图定制

[英]Highcharts Donut Chart customization

I'm working with highcharts and aiming to make a chart similar to this: 我正在与highcharts一起工作,旨在制作类似于以下内容的图表: Highcharts甜甜圈图

Now I've been playing around with the whole ordeal for a while now and I've come been able to reach the following point: http://jsfiddle.net/ccjSy/24/ 现在,我已经对整个折磨进行了一段时间的讨论,并且能够达到以下几点: http : //jsfiddle.net/ccjSy/24/

$(function () {
    $(document).ready(function () {
        var chart = null;               
        var categories = ['Body Fat', 'Lean Mass'],
            name = 'Body Fat vs. Lean Mass',
            data = [{
                y: 69,
                color: '#F0CE0C',
                drilldown: {
                    name: 'Body Fat', 
                    color: '#F0CE0C'
                }
            }, {
                y: 207,
                color: '#23A303',
                drilldown: {
                    name: 'Lean Mass' ,
                    color: '#23A303'
                }
            }];


        // Build the data array
        var browserData = [];
        for (var i = 0; i < data.length; i++) {

            // add browser data
            browserData.push({
                name: categories[i],
                y: data[i].y,
                color: data[i].color
            });

        }

        // Create the chart
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'donutchart',
                type: 'pie',
                spacingTop: 0
            },
            title: {
                text: 'Your Body Fat vs Lean Mass',
                style: {"color": '#7d7d7d'}
            },
            series: [{
                name: 'Weight',
                data: browserData,
                dataLabels: {
                    style: {
                        fontFamily: 'ArialMT',
                        fontSize: '15px',
                        fontWeight: 'bold',
                        color: '#7d7d7d'
                    }
                },
                innerSize: '70%'
            }],
            tooltip: {
                valueSuffix: ' lbs'
            },
            plotOptions: {
                series: {
                    cursor: 'pointer'
                }
            }
        });
    });
});

I am thinking of leaving the space empty in the donut chart and inserting a custom circle with the Weight labeling. 我正在考虑在甜甜圈图中将空间留空,并在“重量”标签上插入自定义圆圈。 However, I'm not certain how to tackle the following problems: 但是,我不确定如何解决以下问题:
1) Leave more defined white spaces in between my two columns 1)在我的两列之间保留更多定义的空格
2) How to properly align my two data labels? 2)如何正确对齐两个数据标签? (As you can see in the model, they are in a straight line with neutral lines attached) (正如您在模型中看到的那样,它们是一条直线,并附加了中性线)
3) How to display the data underneath the two labels with both, the pounds and the percentage, as shown in the image. 3)如图所示,如何在两个标签下面同时显示磅和百分数的数据。

Answering each of your three questions: 回答您的三个问题:

  1. " More white space between columns? " -- add a plotOptions.pie.borderWidth element. 列之间是否有更多空格? ”-添加plotOptions.pie.borderWidth元素。 The default value is 1. I used 5 to give it more white space. 默认值为1。我使用5为其提供了更多的空白。
  2. " Properly align the two data labels? " -- You can compute a custom startAngle based on the two data values which will give you the correct angle to start with. “是否正确对齐两个数据标签? ”-您可以根据两个数据值计算自定义的startAngle ,从而为您提供正确的开始角度。 Since you only have two data values in the series, that's easy. 由于系列中只有两个数据值,因此很容易。

    Calculate startAngle for pie chart 计算饼图的startAngle

     var startAngle = 0 - (90 + (180 * (data[0].y / (data[0].y + data[1].y)))); 

    Notice that if you change the y value for the first data object, the "Body Fat" portion of the pie chart will still maintain it's correct position, even as the portion of the chart grows larger or smaller. 请注意,如果您更改第一个数据对象的y值,则即使图表的该部分变大或变小,饼图的“ Body Fat”部分仍将保持其正确位置。

  3. " Display data underneath the two labels with custom formatting? " -- use plotOptions.pie.format to apply custom formatting to the pie value labels. 使用自定义格式在两个标签下面显示数据吗? ”-使用plotOptions.pie.format将自定义格式应用于饼图值标签。

     format: '<div style="position: relative; top: -20px; text-align: center">{point.name}<br><b><span style="font-size: 29px">{point.percentage:.0f}%</span></b> ({point.y} lbs)</div>' 

Here's a working JSFiddle to see the results. 这是一个工作的JSFiddle来查看结果。 You'll still need to add a dark gray circle behind the pie chart and add the "Total Weight" text but that shouldn't be too difficult. 您仍然需要在饼图后面添加一个深灰色的圆圈,并添加“总重量”文本,但这并不难。

Since I'm sure you'll be wondering about this, I should mention that there's a lot of white space under the pie chart title because it's leaving enough room for other labels that may appear above or below the pie. 因为我敢肯定您会对此感到疑惑,所以我要指出的是,饼图标题下有很多空白,因为它为饼图上方或下方的其他标签留出了足够的空间。 Since you're only using two values that will always be on the left/right sides of the pie chart, that doesn't change the fact that HighCharts is preparing to have other labels that may need to be displayed in the surrounding area. 由于您仅使用两个始终位于饼图的左侧/右侧的值,因此不会改变HighCharts准备在周围区域显示其他标签的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM