简体   繁体   English

海图甜甜圈和条形图

[英]Highchart Donut and Bar Chart

I'm trying to replicate chart similar to this (See Image Below). 我正在尝试复制与此类似的图表(请参见下图)。 I've attached two separate plunkers for each chart. 我为每个图表附加了两个单独的松节器。

What I am trying to achieve: Example Image 我想要达到的目标: 示例图像

Donut Chart: ===> See Comment below for Plunkr 甜甜圈图: ===>请参阅下面的Plunkr评论

 chart: {
                type: 'pie'
            },
            credits: {
                enabled: false
            },
            colors: ['#F59640', '#A589D9', '#F16D64', '#35BEC1', '#EDEDED'],
            title: {
                text: '82',
                style: {
                    color: '#333333',
                    fontWeight: 'bold',
                    fill: '#333333',
                    width: '211px',
                    fontSize: '32px'
                },
                y: -30,
                verticalAlign: 'middle'
            },
            subtitle: {
                text: 'out of 100',
                style: {
                    color: '#333333',
                    fill: '#333333',
                    width: '211px',
                    fontSize: '28px'
                },
                y: 30,
                verticalAlign: 'middle'
            },
            plotOptions: {
                pie: {
                    innerSize: '60%',
                    outerRadius: '70%',
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: false
                    }
                }
            },
            exporting: {
                enabled: false
            },
            series: [{
                data: [
                    ['Collaboration', 19.61],
                    ['Reading Articles', 17.47],
                    ['Insight', 19.95],
                    ['Personalization', 25]
                ]
            }]
  1. I'm trying to setup these values dynamically from an API. 我正在尝试从API动态设置这些值。 Total value is 100%, with 4-5 categories/series. 总价值为100%,包含4-5个类别/系列。
  2. Its almost complete, just need to show blank value if items do not add up to 100%. 它几乎完成了,如果项加起来不等于100%,只需显示空白值。

Bar Chart: ===> Bar Chart Plunkr 条形图: ===> 条形图Plunkr

            chart: {
            type: 'bar'
        },
        title: {
            text: ''
        },
        legend: {
            enabled: false
        },
        xAxis: {
            categories: ['Collaboration', 'Reading Articles', 'Insight', 'Personalization'],
            visible: false,
        },
        yAxis: {
            min: 0,
            max: 25,
            title: {
                text: null
            },
            stackLabels: {
                enabled: true,
            },
        },
        exporting: {
            enabled: false
        },
        plotOptions: {
            series: {
                stacking: 'percent'
            },
            bar: {
                grouping: false,
                dataLabels: {
                    enabled: false
                }
            }
        },
        credits: {
            enabled: false
        },
        series: [{
            name: 'Remaining',
            data: [5.39, 7.53, 5.05, 0],
            borderWidth: 0,
            color: 'rgba(0,0,0,0)'
        }, {
            name: 'Remaining',
            data: [5.39, 7.53, 5.05, 0],
            borderWidth: 0,
            stack: 1,
            animation: false,
            color: '#CCC'
        }, {
            name: 'Completed',
            data: [{ y: 19.61, color: '#F59640' }, { y: 17.47, color: '#A589D9' }, { y: 19.95, color: '#F16D64' },
            { y: 25, color: '#35BEC1' }]
        }]
  1. Unable to separate each bar like the image. 无法将每个条形图分开。 Each bars are not stacking each other. 每个酒吧不互相堆叠。

  2. Also trying to match BOTH the labels above each bar as well as showing the Y labels under each bar with only the First and Last Label Showing. 此外,还要尝试使每个条形上方的标签都匹配,并在每个条形下方显示Y标签,并且仅显示“第一个”和“最后一个”标签。

Unable to post more than 2 link at this time. 目前无法发布2个以上的链接。

Thank you in advance for your help. 预先感谢您的帮助。

As sebastian mentioned, for the donut chart you can calculate the the remainder and add that as a data point 如塞巴斯蒂安所说,对于甜甜圈图,您可以计算余数并将其添加为数据点

series: [{
    data: [
        ['Collaboration', 19.61],
        ['Usability', 17.47],
        ['Insight', 19.95],
        ['Personalization', 25],
        {
            name: 'Missing',
            y: 17.97, // calculate missing value and use as a data point
            color: '#ccc'
        }
    ]
}]

donut chart plnkr: https://plnkr.co/edit/LiwDUGg0B6XeXRhXwFGE?p=preview 甜甜圈图plnkr: https ://plnkr.co/edit/LiwDUGg0B6XeXRhXwFGE ? p = preview

For the bar chart, you don't want the plotOptions.series.stacking = percent . 对于条形图,您不希望plotOptions.series.stacking = percent This is converting your series data to a percent when you have already done that in the data you passed in. 当您在传递的数据中完成序列数据后,这会将其转换为百分比。

Now you can set your Remaining series to the max value 现在,您可以将“剩余系列”设置为最大值

{
    name: 'Remaining',
    data: [25, 25, 25, 25],
    borderWidth: 0,
    stack: 1,
    animation: false,
    color: '#CCC'
}

As for separating the bars & the labels, I would go ahead and make 4 separate charts and use an ngFor to load them into the DOM. 至于分隔条和标签,我将继续制作4个单独的图表,并使用ngFor将其加载到DOM中。

Bar chart only: 仅条形图:

You can achieve that kind of look by creating a separate series for every point and linking it to it's own (properly positioned) x and y axes. 您可以通过为每个点创建一个单独的系列并将其链接到其自己的(正确定位的)x和y轴来实现这种外观。

Live working example: http://jsfiddle.net/kkulig/41q6k8tc/ 实际工作示例: http : //jsfiddle.net/kkulig/41q6k8tc/

var series = [{
      data: [{
          y: 19.61,
          color: '#F59640'
        }],
        name: 'Complete',
        xAxis: 0,
        yAxis: 0
    }, {
      data: [{
        y: 17.47,
        color: '#A589D9'
      }],
      name: 'Complete',
      xAxis: 1,
      yAxis: 1
    }, {
      data: [{
        y: 19.95,
        color: '#F16D64'
      }],
      name: 'Complete',
      xAxis: 2,
      yAxis: 2
    }, {
      data: [{
        y: 25,
        color: '#35BEC1'
      }],
      name: 'Complete',
      xAxis: 3,
      yAxis: 3
    }];

For positioning axes I used top property (it's not documented in the API, but it works): 对于定位轴,我使用了top属性(API中没有记录该属性,但是它可以工作):

var xAxes = [{
  categories: ['Collaboration'],
}, {
  categories: ['Reading Articles'],
  top: '25%'
}, {
  categories: ['Insight'],
  top: '50%'
}, {
  categories: ['Personalization'],
  top: '75%'
}].map(function(axis) {
  // common options
  axis.height = '25%';
  axis.offset = 0;
  axis.lineWidth = 0;
  axis.tickWidth = 0;

  return axis;
});

For every series I created corresponding 'Uncompleted' series using this code: 对于每个系列,我使用以下代码创建了相应的“未完成”系列:

for(var i = series.length - 1; i >= 0; i--) {
    series.push({
    name: 'Uncompleted',
    data: [25 - series[i].data[0].y],
    xAxis: series[i].xAxis,
    yAxis: series[i].yAxis,
    color: '#ccc',
    dataLabels: {
        enabled: false
    }
  });
}

These series are stacked. 这些系列堆叠在一起。

Via tickPositions you can configure an axis to have only two labels. 通过tickPositions您可以将轴配置为仅具有两个标签。

Tooltip doesn't seem to handle top offset properly. 工具提示似乎无法正确处理顶部偏移。 Line y = anchor[1] needs to be changed to + (point.series.xAxis.top || 0) - (this.chart.plotTop || 0); y = anchor[1]需要更改为+ (point.series.xAxis.top || 0) - (this.chart.plotTop || 0); in Highcharts.Tooltip.prototype.refresh core function. Highcharts.Tooltip.prototype.refresh核心函数中。

API references: API参考:

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

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