1

我有一个使用 highcharts 饼图的页面,我正在尝试使用日期选择器下拉更新图表。我对条形图有类似的实现,并且效果很好。请注意(这是来自 PHP 类,因此是连接和其他)。

<script type='text/javascript'>

                    function drawPie(data)
                    {
                        var chart;
                        alert('called');
                        var options = {
                            chart: {
                                renderTo: 'PieChart',
                                plotBackgroundColor: null,
                                plotBorderWidth: null,
                                plotShadow: false,
                                height: 350
                            },
                            title: {
                                text: 'Product Popularity'
                            },
                            tooltip: {
                                pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                                percentageDecimals: 1
                            },
                            plotOptions: {
                                pie: {
                                    allowPointSelect: true,
                                    cursor: 'pointer',
                                    dataLabels: {enabled: false},
                                    showInLegend: true
                                }
                            },
                            series: [{
                                type: 'pie',
                                name: 'Product Popularity',
                                data: data

                            }]
                        }
                        chart = new Highcharts.Chart(options);
                        $('#ProductPieMod div.mod_content').css('height', $('#PieChart').css('height'));
                    }
                $(document).ready(function(){
                    drawPie(" . $this->get_data($this->date) . ");

                    $('#ProductPieMod_date').on('change', function(){
                        var val = parseInt($(this).val());
                        switch(val)
                        {
                            case 0:
                                var date = Date.today();
                                break;
                            case 1:
                                var date = Date.parse('last week');
                                break;
                            case 2:
                                var date = Date.today().moveToFirstDayOfMonth();
                                break;
                            case 3:
                                var date = Date.parse('January').moveToFirstDayOfMonth();
                                break;
                            default:
                                var date = Date.today();
                        }
                        var info;
                        $.ajax({
                            type: 'POST',
                            url: '". matry::base_to('utilities/dhs/manager_dash') . "',
                            async: false,
                            dataType: 'json',
                            data: {date: date.toString('M/dd/yyyy'), module: 'ProductPieMod'},
                            success: function(data)
                            {
                                drawPie(data);  
                            }
                        });

                    });
                });
                </script>

我的 ajax 返回以下字符串:

[['FASTCLIX LANCING DEVICE', 62.5],['FREESTYLE LANCING DEVICE', 25],['ONETOUCH DELICA LANCING DEVICE', 12.5]]

另外,这个图表是最初构建的,使用完全相同的方法,就好了。就在我使用下拉菜单(运行 onChange 事件)时,它会中断。

更新 我也为此创建了一个小提琴:http: //jsfiddle.net/SHReZ/1/

4

1 回答 1

2

首先,您需要将chartvar 放置到document.ready处理程序范围,接下来,您需要在绘制之前销毁图表。

$(document).ready(function () {
    var chart;

    function drawPie(data) {
        console.log('called');
        var options = {
            chart: {
                renderTo: 'PieChart',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                height: 350
            },
            title: {
                text: 'Product Popularity'
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: false
                    },
                    showInLegend: true
                }
            },
            series: [{
                type: 'pie',
                name: 'Product Popularity',
                data: data
            }]
        };
        if (chart !== undefined) chart.destroy();
        chart = new Highcharts.Chart(options);
        $('#ProductPieMod div.mod_content').css('height', $('#PieChart').css('height'));
    }


    drawPie([
        ['ONETOUCH DELICA LANCING DEVICE', 27.78],
        ['FREESTYLE LANCING DEVICE', 20.83],
        ['Nova Max Ketone Test Strips Health and', 11.11],
        ['ONETOUCH ULTRASOFT LANCING DEVICE 2PK', 11.11]
    ]);
    //get data from https://gist.github.com/zba/4712055 , delay 4
    $.post('/gh/gist/response.html/4712055', {
        delay: 4
    }, function (data) {
        drawPie(data);
    }, 'json');

});

小提琴

这里也是演示,它不会破坏图表,但它会改变颜色

于 2013-02-05T00:56:22.517 回答