在Highcharts.js条形图

时间:2015-07-02 21:11:18

标签: javascript jquery html charts highcharts

我试图计算如何从我的图表中控制条形的顶部/底部间距,高度和位置,我需要将所有条形对齐从左上角开始,然后将高度设置为每个30像素,然后在每个条形图上方/下方有2个边距,点击任意条形图后,如果所有条形图的高度都大于容器的原始高度,则所有条形图的位置都相同...然后调整图表大小容纳更新的图表?我知道听起来很复杂,有没有人取得这样的成就?

我创建了drilldown highchart.js

这是布局应如何显示的示例: Level one

level two

这是我的js:

$(function () {
            $('#container').highcharts({
                chart: 
                {
                    type: 'bar',
                    backgroundColor: 'red',
                    spacingTop: 20,
                    spacingBottom: 20,
                    height: 400
                },
                title: {
                    text: null
                },
                subtitle: {},
                xAxis: {
                    type: 'category',
                    labels: {
                        style: {
                            fontSize: '14px',
                            textAlign: 'right',
                            paddingTop: '10px',
                            paddingRight: '10px',
                            paddingBottom: '10px',
                            //background: '#fff',
                        },
                        useHTML : true
                    }
                },
                yAxis: {
                    labels: {
                        align: 'center'
                    },
                    title: {
                        text: null
                    }

                },
                legend: {
                    enabled: false
                },
                plotOptions: {
                    series: {
                        color: '#d0d1d1',
                        borderWidth: 2,
                        borderColor: "#fff",
                        pointPadding: 0,
                        groupPadding: 0,            },
                },

                tooltip: 
                {
                    enabled: false
                },
                credits: 
                {
                    enabled: false
                },
                series: [{
                    data: [{
                        name: "Asia",
                        y: 56.33,
                        drilldown: "Microsoft Internet Explorer"
                    }, {
                        name: "North America",
                        y: 24.03,
                        drilldown: "Chrome"
                    }, {
                        name: "South America",
                        y: 10.38,
                        drilldown: "Firefox"
                    }, {
                        name: "Europe",
                        y: 4.77,
                        drilldown: "Safari"
                    }, {
                        name: "Australia",
                        y: 0.91,
                        drilldown: "Opera"
                    }, {
                        name: "Africa",
                        y: 32.33,
                        drilldown: "Opera"
                    }]
                }],
                drilldown: {
                    series: [{
                        id: "Microsoft Internet Explorer",
                        data: [
                            [
                                "v11.0",
                                24.13,
                            ],
                            [
                                "v8.0",
                                17.2
                            ],
                            [
                                "v9.0",
                                8.11
                            ],
                            [
                                "v10.0",
                                5.33
                            ],
                            [
                                "v6.0",
                                1.06
                            ],
                            [
                                "v7.0",
                                0.5
                            ]
                        ]
                    }, {
                        name: "Chrome",
                        id: "Chrome",
                        data: [
                            [
                                "v40.0",
                                5
                            ],
                            [
                                "v41.0",
                                4.32
                            ],
                            [
                                "v42.0",
                                3.68
                            ],
                            [
                                "v39.0",
                                2.96
                            ],
                            [
                                "v36.0",
                                2.53
                            ],
                            [
                                "v43.0",
                                1.45
                            ],
                            [
                                "v31.0",
                                1.24
                            ],
                            [
                                "v35.0",
                                0.85
                            ],
                            [
                                "v38.0",
                                0.6
                            ],
                            [
                                "v32.0",
                                0.55
                            ],
                            [
                                "v37.0",
                                0.38
                            ],
                            [
                                "v33.0",
                                0.19
                            ],
                            [
                                "v34.0",
                                0.14
                            ],
                            [
                                "v30.0",
                                0.14
                            ]
                        ]
                    }, {
                        name: "Firefox",
                        id: "Firefox",
                        data: [
                            [
                                "v35",
                                2.76
                            ],
                            [
                                "v36",
                                2.32
                            ],
                            [
                                "v37",
                                2.31
                            ],
                            [
                                "v34",
                                1.27
                            ],
                            [
                                "v38",
                                1.02
                            ],
                            [
                                "v31",
                                0.33
                            ],
                            [
                                "v33",
                                0.22
                            ],
                            [
                                "v32",
                                0.15
                            ]
                        ]
                    }, {
                        name: "Safari",
                        id: "Safari",
                        data: [
                            [
                                "v8.0",
                                2.56
                            ],
                            [
                                "v7.1",
                                0.77
                            ],
                            [
                                "v5.1",
                                0.42
                            ],
                            [
                                "v5.0",
                                0.3
                            ],
                            [
                                "v6.1",
                                0.29
                            ],
                            [
                                "v7.0",
                                0.26
                            ],
                            [
                                "v6.2",
                                0.17
                            ]
                        ]
                    }, {
                        name: "Opera",
                        id: "Opera",
                        data: [
                            [
                                "v12.x",
                                0.34
                            ],
                            [
                                "v28",
                                0.24
                            ],
                            [
                                "v27",
                                0.17
                            ],
                            [
                                "v29",
                                0.16
                            ]
                        ]
                    }]
                }
            });
            });

1 个答案:

答案 0 :(得分:0)

使用drilldowndrillup事件为轴设置新的高度,该高度将使用您想要的空间:http://jsfiddle.net/oe236652/4/

        events: {
            drilldown: function (e) {
                this.xAxis[0].update({
                    height: 34 * e.seriesOptions.data.length
                }, false);
            },
            drillup: function (e) {
                this.xAxis[0].update({
                    height: 34 * e.seriesOptions.data.length
                }, false);
            }
        }

在创建图表和xAxis.height

时另外设置series.pointWidth
    xAxis: {
        height: 34 * 6, // (30 + 2*2) * number_of_points
        ...
    },

    plotOptions: {
        series: {
            pointWidth: 30,
            ...
        },
    },

注意:我建议设置图表的高度,足以渲染所有条形图(点击"北美"),或者在drillup / drilldown设置新的图表的大小,使用setSize(w, h)