蝴蝶图使用Highcharts

时间:2017-01-11 07:07:49

标签: javascript highcharts

我尝试使用Highcharts创建蝴蝶图表。我想把它描绘成 Butterfly Chart

守则如下

// Data gathered from http://populationpyramid.net/germany/2015/
$(function () {
// Age categories
var categories = ['0-4', '5-9', '10-14', '15-19',
        '20-24', '25-29', '30-34', '35-39', '40-44',
        '45-49', '50-54', '55-59', '60-64', '65-69',
        '70-74', '75-79', '80-84', '85-89', '90-94',
        '95-99', '100 + '];
$(document).ready(function () {
    Highcharts.chart('container', {
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Population pyramid for Germany, 2015'
        },
        subtitle: {
            text: 'Source: <a href="http://populationpyramid.net/germany/2015/">Population Pyramids of the World from 1950 to 2100</a>'
        },
        xAxis: [{
            categories: categories,
            reversed: false,
            labels: {
                step: 1
            }
        }, /*{ // mirror axis on right side
            opposite: true,
            reversed: false,
            categories: categories,
            linkedTo: 0,
            labels: {
                step: 1
            }
        }*/],
        yAxis: {
            title: {
                text: null
            },
            labels: {
                formatter: function () {
                    return Math.abs(this.value) + '%';
                }
            }
        },

        plotOptions: {
            series: {
                stacking: 'normal'
            }
        },

        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + ', age ' + this.point.category + '</b><br/>' +
                    'Population: ' + Highcharts.numberFormat(Math.abs(this.point.y), 0);
            }
        },

        series: [{
            name: 'Male',
            data: [-2.2, -2.2, -2.3, -2.5, -2.7, -3.1, -3.2,
                -3.0, -3.2, -4.3, -4.4, -3.6, -3.1, -2.4,
                -2.5, -2.3, -1.2, -0.6, -0.2, -0.0, -0.0]
        }, {
            name: 'Female',
            data: [2.1, 2.0, 2.2, 2.4, 2.6, 3.0, 3.1, 2.9,
                3.1, 4.1, 4.3, 3.6, 3.4, 2.6, 2.9, 2.9,
                1.8, 1.2, 0.6, 0.1, 0.0]
        }]
    });
});

});

这是我的小提琴link,如何在系列之间获取我的xAxis标签。请指导我实现这一目标。

2 个答案:

答案 0 :(得分:2)

inverted columnrange图表与cross-specific-values插件结合使用是一种方法。列范围图表允许您指定列的位置并为标签创建空间。该插件将轴移动到图表的中心。

    Highcharts.chart('container', {
    title: {
      text: 'Butterfly Chart Example'
    },

    subtitle: {
        text: '<a href="http://stackoverflow.com">stackoverflow.com</a>'
    },

    chart: {
        type: 'columnrange',
        inverted: true,
        marginTop: 100
    },

    legend: {
      verticalAlign: 'top',
      y: 60,
      x: -25,
      itemDistance: 50
    },

    xAxis: {
        categories: ['G7', 'A8', 'V9', 'V4', 'V3', 'V1', 'V5'],
        crossing: 118,
        lineWidth: 0,
        tickLength: 0,
    },

    yAxis: {
      gridLineWidth: 0,
      tickInterval: 50,
      min: 0,
      max: 250,
      lineWidth: 1,
      title: {
        text: null
      }
    },

    plotOptions: {
      series: {
        grouping: false
      }
    },

    series: [{
        name: 'South',
        color: 'blue',
        data: [
          [55, 100],
          [60, 100],
          [65, 100],
          [55, 100],
          [75, 100],
          [52, 100],
          [60, 100]
        ]
    }, {
      name: 'North',
      color: 'orange',
      data: [
        [120, 170],
        [120, 150],
        [120, 175],
        [120, 130],
        [120, 125],
        [120, 148],
        [120, 145]
      ]
    }]

});

示例:http://jsfiddle.net/7d4mrhuv/

答案 1 :(得分:2)

我会以不同于摩根的方式来解决这个问题,尽管这个例子肯定是有用的。

我会将其作为标准条形图,使用多个yAxis对象(每个系列一个),这也允许使用左侧系列的reversed属性。< / p>

代码示例:

    yAxis: [{
        title: { text: null },
        width: 200,
        reversed: true
    },{
        offset: 0,
        title: { text: null },
        left: 300,
        width: 200
    }],
    series: [{
      yAxis: 0,
      data: [...]
    }, {
      yAxis: 1,
      data: [...]
    }]

<强>小提琴:

示例输出:

screenshot