-1

我需要创建一个带有向下钻取饼图的条形图。在所有浏览器中一切正常,但

您需要更多解释吗?

    function activities(params) {

    //params coming from statistics controller
    var title_activities = params['title_activities'],
        source_activities = params['source_activities'],
        link_activities = params['link_activities'],
        credits_databasis_str_activities = params['credits_databasis_str_activities'],
        activities_category_names = params['activities_category_names'],
        activities_category_data = params['activities_category_data'],
        room_filter_name = params['room_filter_name']

    var colors = Highcharts.getOptions().colors,
            categories = activities_category_names,
            name = 'Activities',
            data = activities_category_data

        function setChart(chart, options) {
            chart.series[0].remove(false);
            chart.xAxis[0].setCategories(options.categories, false);
            chart.addSeries({
                type: options.type,
                name: options.name,
                data: options.data,
                color: options.color || colors[0]
            }, false);

            //chart.options.spacingRight = 35; //not working
            //chart.yAxis.title = "Test"; //not working
            chart.redraw();
        }

        var chart = new Highcharts.Chart({
            chart: {
              renderTo: 'activities_chart',
              type: 'bar',
              spacingRight: 15
              //spacingLeft: 15
            },
            title: {
                text: title_activities
            },
            subtitle: {
                text: 'Current room: ' + room_filter_name
            },
            credits: {
                text: "Databasis: " + credits_databasis_str_activities + " households" + "    |    Source: " + source_activities,
                href: link_activities
            },
            xAxis: {
                categories: categories
            },
            yAxis: {
                title: {
                    text: null
                },
                max: 100
            },
            plotOptions: {
                bar: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function() {
                                var drilldown = this.drilldown;
                                var options;
                                if (drilldown) { // drill down

                                    this.series.chart.setTitle({
                                       text: "Activities - " + drilldown.name
                                   });

                                    options = {
                                        'name': drilldown.name,
                                        'categories': drilldown.categories,
                                        'data': drilldown.data,
                                        'type': 'pie'
                                    };
                                } else { // restore

                                  this.series.chart.setTitle({
                                       text: "Activities"
                                   });
                                    options = {
                                        'name': name,
                                        'categories': categories,
                                        'data': data,
                                        'type': 'bar'
                                    };
                                }
                                setChart(this.series.chart, options);
                            }
                        }
                    }
                },
                pie: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function() {
                                var drilldown = this.drilldown;
                                var options;
                                if (drilldown) { // drill down

                                    this.series.chart.setTitle({
                                       text: "Activities - " + drilldown.name
                                   });

                                    options = {
                                        'name': drilldown.name,
                                        'categories': drilldown.categories,
                                        'data': drilldown.data,
                                        'type': 'pie'
                                    };
                                } else { // restore

                                  this.series.chart.setTitle({
                                       text: "Activities"
                                   });
                                    options = {
                                        'name': name,
                                        'categories': categories,
                                        'data': data,
                                        'type': 'bar'
                                    };
                                }
                                setChart(this.series.chart, options);
                            }
                        }
                    },
                    dataLabels: {
                        enabled: true,
                        color: colors[0],
                        connectorColor: colors[0],
                        distance: 15,
                        formatter: function() {
                            var label_String = this.point.name;

                            if (label_String.length > 16) {
                              label_String = label_String.substring(0,16) + "..."
                            };

                            return '<b>'+ label_String +'</b>: '+ this.y;
                        }
                    }
                }
            },
            tooltip: {
                formatter: function() {
                    var point = this.point,
                        s = ""

                    if (point.drilldown) {
                        s += this.x +':<b>'+ this.y +' % of households</b><br/>';
                        s += 'Click to view '+ point.category +' details';
                    } else {
                        s += point.name +':<b>'+ this.y +' times chosen</b><br/>';
                        s += 'Click to return to activities overview';
                    }
                    return s;
                }
            },
            series: [{
                type: 'bar',
                name: name,
                data: data
            }],
            exporting: {
                enabled: false
            }
        });

  }
4

1 回答 1

1

问题是高图表中的错误。我在 3.0.1 上运行导致错误,他们在 2 天前切换到 3.0.2 并且问题已解决。

对于任何对从条形图到饼图的向下钻取图感兴趣的人...在这里您可以找到示例

这是代码:

HTML:

<script src="http://code.highcharts.com/3.0.2/highcharts.js"></script>
<div id="activities_chart" style="width: 100%; height: 340px;"></div>

JS:

$(function () {

var title_activities = "Activities",
    source_activities = "My source",
    link_activities = "http://www.highcharts.com",
    credits_databasis_str_activities = "215",
    activities_category_names = ["eat", "read", "surf in internet"],
    activities_category_data = [{y:66.15, drilldown:{name:"eat - How often?", data:[["1 time per day", 21], ["3 times per day", 45], ["2 times per day", 27], ["2 times a week", 3], ["≥ 3 times a week", 5], ["less", 4], ["every other week", 3], ["1 times a week", 7], ["not specified", 6], ["once a month", 6]]}}, {y:67.71, drilldown:{name:"read - How much time?", data:[["2h per day", 11], ["1h per day", 41], ["less often", 6], ["2h per week", 8], ["1/2h per day", 38], ["1h per week", 10], ["3h per week", 4], ["≥ 3h per day", 5], ["not specified", 7]]}}, {y:59.38, drilldown:{name:"surf in internet - How much time?", data:[["2h per day", 31], ["1h per week", 1], ["2h per week", 2], ["1h per day", 37], ["1/2h per day", 20], ["≥ 3h per day", 13], ["less often", 3], ["not specified", 5], ["3h per week", 2]]}}],
    room_filter_name = "Living Room"

var colors = Highcharts.getOptions().colors,
        categories = activities_category_names,
        name = 'Activities',
        data = activities_category_data

function setChart(chart, options) {

        var max = chart.series.length;
        for (var i = 0; i < max; i++) {
          chart.series[0].remove(false);
        }

        chart.xAxis[0].setCategories(options.categories, false);

        chart.addSeries({
            type: options.type,
            name: options.name,
            data: options.data,
            color: colors[0]
        }, false);

        chart.redraw();
    }

    var chart = new Highcharts.Chart({
        chart: {
          renderTo: 'activities_chart',
          type: 'bar',
          spacingRight: 15
        },
        title: {
            text: title_activities
        },
        subtitle: {
            text: 'Current room: ' + room_filter_name
        },
        credits: {
            text: "Databasis: " + credits_databasis_str_activities + " households" + "    |    Source: " + source_activities,
            href: link_activities
        },
        xAxis: {
            categories: categories
        },
        yAxis: {
            title: {
                text: null
            },
            max: 100
        },
        plotOptions: {
            bar: {
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() {
                            var drilldown = this.drilldown;

                            this.series.chart.setTitle({
                                   text: "Activities - " + drilldown.name
                            });

                            var options = {
                                'name': drilldown.name,
                                'categories': drilldown.categories,
                                'data': drilldown.data,
                                'type': 'pie'
                            };

                            setChart(this.series.chart, options);
                        }
                    }
                }
            },
            pie: {
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() {
                          this.series.chart.setTitle({
                               text: "Activities"
                           });
                          var options = {
                                'name': name,
                                'categories': categories,
                                'data': data,
                                'type': 'bar'
                            };

                            setChart(this.series.chart, options);
                        }
                    }
                },
                dataLabels: {
                    enabled: true,
                    color: colors[0],
                    connectorColor: colors[0],
                    distance: 15,
                    formatter: function() {
                        var label_String = this.point.name;

                        if (label_String.length > 16) {
                          label_String = label_String.substring(0,16) + "..."
                        };

                        return '<b>'+ label_String +'</b>: '+ this.y;
                    }
                }
            }
        },
        tooltip: {
            formatter: function() {
                var point = this.point,
                    s = ""

                if (point.drilldown) {
                    s += this.x +':<b>'+ this.y +' % of households</b><br/>';
                    s += 'Click to view '+ point.category +' details';
                } else {
                    s += point.name +':<b>'+ this.y +' times chosen</b><br/>';
                    s += 'Click to return to activities overview';
                }
                return s;
            }
        },
        series: [{
            type: 'bar',
            name: name,
            data: data
        }],
        exporting: {
            enabled: false
        }
    })
}); //end
于 2013-06-09T20:51:16.637 回答