首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax派图的高级图表

问Ajax派图的高级图表
EN

Stack Overflow用户
提问于 2013-02-05 00:14:50
回答 1查看 4K关注 0票数 1

我有一个页面,使用高级图表饼图,我试图更新图表与日期选择器下拉。我有一个类似的实现条形图和它的工作伟大。请注意(这是来自一个PHP类,因此出现了连接和其他情况)。

代码语言:javascript
复制
<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/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-05 00:56:22

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

代码语言:javascript
复制
$(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');

});

小提琴

另外,这里是一个演示程序,它不会破坏图表,但是它会改变颜色。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14698095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档