具有多个组的Highcharts分组柱形图?

时间:2013-04-30 20:48:50

标签: charts highcharts multiple-columns series

我希望在Highcharts中创建一个分组的柱形图,除了在给定的一天中有多个组。该图表看起来像http://www.highcharts.com/demo/column-stacked-and-grouped(来自此论坛问题http://highslide.com/forum/viewtopic.php?f=9&t=19575),除了每个堆叠条替换为一组分组列(非堆叠)。因此,我们每天会看到多组列,其理念是每个组对应一个用户。有谁知道怎么做?

编辑:这是我找到的http://jsfiddle.net/pMA2H/1/

的jsfiddle
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title>ElementStacks - jsFiddle demo</title>

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script>

  <link rel="stylesheet" type="text/css" href="/css/normalize.css"/>
  <link rel="stylesheet" type="text/css" href="/css/result-light.css"/>



      <script type='text/javascript' src="http://highcharts.com/js/testing.js"></script>


  <style type='text/css'>

  </style>



<script type='text/javascript'>//<![CDATA[ 
$(function(){
/*
Data is:
                            Gross Amount    Cost Amount
Services       Australia    20              10              
               Germany      30              15
Manufacturing  Australia    35              17
               Germany      25              12

----

Would like to be able define my categories hierarchically - example:
xAxis: [{
  categories: [{
    name: 'Services'
    children: ['Australia', 'Germany']
  },{
    name: 'Manufacturing'
    children: ['Australia', 'Germany']
  }]
}]

and get a result similar to what is fudged up by using the renderer on the right.
*/

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'column'
    }, 
    xAxis: [{
        categories: ['Australia', 'Germany', 'Australia', 'Germany'],
        labels: {
            y: 40
        }
    }],
    legend: {
        margin: 40
    },

    series: [{
        name: 'Gross',
        data: [['Services', 20],['Services',30],['Manufacturing', 35],['Manufacturing', 25]]
    },{
        name: 'Cost',
        data: [['Services', 10],['Services',15],['Manufacturing', 17],['Manufacturing', 13]]
    }]
}, function(chart){
    $('.highcharts-axis:first > text').each(function() {
       this.setAttribute('y', parseInt(this.getAttribute('y')) - 20)
    });

    var text1 = chart.renderer.text("Services", 150, 340).add();
    var text2 = chart.renderer.text("Manufacturing", 350, 340).add();
});
});//]]>  

</script>


</head>
<body>
  <div id="container" style="height: 400px; width: 500px"></div>


</body>


</html>

来自此处的帖子http://highcharts.uservoice.com/forums/55896-general/suggestions/2230615-grouped-x-axis#comments。 x轴增量的代码有点单调乏味,因为您必须手动添加每个增量并包含其间距以确保数据点对齐。我之前已经完成了图表,您可以在其中为日期指定pointStart和pointInterval。如果有人知道更优雅的解决方案,那就太棒了。

4 个答案:

答案 0 :(得分:2)

您可以将插件用于分组类别,您可以在此处找到它:https://github.com/blacklabel/grouped_categories

答案 1 :(得分:2)

我遇到了同样的问题,并解决了它使用简单的HighCharts。它需要一些神奇的数据混合,但结果是公平的。

enter image description here

见这里:https://stackoverflow.com/a/31029535/461499

答案 2 :(得分:1)

除非您还没有解释其他内容,否则您要求的是分组柱形图的默认行为:

http://jsfiddle.net/jlbriggs/TfvGp/

.

实现这一目标所需要的只是多个数据系列和一种“列”

答案 3 :(得分:1)

要解决这个问题,你需要堆叠一个无法堆叠的变量。喜欢: 系列:{

{ 
    name: 'boys', 
    stack: 1,
    data: [2, 6, 5,0] 
} 

{ 
    name: 'girls', 
    stack: 2,
    data: [0, 6, 1,3] 
} 

} 没有堆叠1,2,3,4 e.t.c