jquery - 使用 Highcharts 通过 JSON 重新加载图表数据

我正在尝试根据页面中其他位置的按钮单击通过 JSON 重新加载 Highcharts 图表的数据。最初我想显示一组默认数据(按类别支出),然后根据用户输入加载新数据(例如,按月支出)。我能想到的从服务器输出 JSON 的最简单方法是将 GET 请求传递到 PHP 页面,例如 $.get('/dough/includes/live-chart.php?mode=month' ,从按钮的 ID 属性中检索此值。

这是我目前检索默认数据(按类别支出)的内容。需要找到如何根据用户输入,按需将完全不同的数据加载到饼图中:

$(document).ready(function() {
            //This is an example of how I would retrieve the value for the button
            $(".clickMe").click(function() {
                var clickID = $(this).attr("id");
            });
            var options = {
                chart: {
                    renderTo: 'graph-container',
                    margin: [10, 175, 40, 40]
                },
                title: {
                    text: 'Spending by Category'
                },
                plotArea: {
                    shadow: null,
                    borderWidth: null,
                    backgroundColor: null
                },
                tooltip: {
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: $'+ this.y;
                    }
                },
                credits: {
                    enabled: false
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            formatter: function() {
                                if (this.y > 5) return '$' + this.y;
                            },
                            color: 'white',
                            style: {
                                font: '13px Trebuchet MS, Verdana, sans-serif'
                            }
                        }
                    }
                },
                legend: {
                    layout: 'vertical',
                    style: {
                        left: 'auto',
                        bottom: 'auto',
                        right: '50px',
                        top: '100px'
                    }
                },
                series: [{
                    type: 'pie',
                    name: 'Spending',
                    data: []
                }]
            };
            $.get('/dough/includes/live-chart.php', function(data) {
            var lines = data.split('\n');
            $.each(lines, function(lineNo, line) {
                var items = line.split(',');
                var data = {};
                $.each(items, function(itemNo, item) {
                    if (itemNo === 0) {
                        data.name = item;
                    } else {
                        data.y = parseFloat(item);
                    }
                });
                options.series[0].data.push(data);
            });
            // Create the chart
            var chart = new Highcharts.Chart(options);
        });
        });

任何帮助将不胜感激

编辑

感谢 Robodude,这是更新后的 Javascript。约翰,你让我在正确的轨道上 - 谢谢!我现在不知道如何从 AJAX 请求中替换图表上的数据。我必须承认 $.get() 后面的代码很可能来自示例代码,我不完全理解它运行时发生了什么 - 也许有更好的方法来格式化数据?

我能够在图表现在加载新数据方面取得一些进展,但它是在已经存在的数据之上添加的。我怀疑罪魁祸首是这一行:

options.series[0].data.push(data);

我试过 options.series[0].setData(data);但什么也没发生。从好的方面来说,AJAX 请求可以根据选择菜单的值完美运行,并且没有 Javascript 错误。这是有问题的代码,没有图表选项:

$.get('/dough/includes/live-chart.php?mode=cat', function(data) {
            var lines = data.split('\n');
            $.each(lines, function(lineNo, line) {
                var items = line.split(',');
                var data = {};
                $.each(items, function(itemNo, item) {
                    if (itemNo === 0) {
                        data.name = item;
                    } else {
                        data.y = parseFloat(item);
                    }
                });
                options.series[0].data.push(data);
            });
            // Create the chart
            var chart = new Highcharts.Chart(options);
        });
        $("#loadChart").click(function() {
            var loadChartData = $("#chartCat").val();
                $.get('/dough/includes/live-chart.php?mode=' + loadChartData, function(data) {
                var lines = data.split('\n');
                $.each(lines, function(lineNo, line) {
                    var items = line.split(',');
                    var data = {};
                    $.each(items, function(itemNo, item) {
                        if (itemNo === 0) {
                            data.name = item;
                        } else {
                            data.y = parseFloat(item);
                        }
                    });
                    options.series[0].data.push(data);
                });
                // Create the chart
                var chart = new Highcharts.Chart(options);
            });
        });
    });

编辑 2 这是图表从中提取的格式 - 非常简单,类别名称和值后面都有\n。

Coffee, 4.08
Dining Out, 5.05
Dining: ODU, 5.97
Dining: Work, 38.41
Entertainment, 4.14
Gas, 79.65
Groceries, 228.23
Household, 11.21
Misc, 12.20
Snacks, 20.27

最佳答案

其他答案对我不起作用。我在他们的文档中找到了答案:

http://api.highcharts.com/highcharts#Series

使用这种方法(see JSFiddle example):

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container'
    },

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]        
    }]
});

// the button action
$('#button').click(function() {
    chart.series[0].setData([129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4, 29.9, 71.5, 106.4] );
});

https://stackoverflow.com/questions/4210879/

相关文章:

c# - 如何从 C# 中的 JSON 字符串中获取一些值?

ruby-on-rails - 使用 Rails 3.2.11 和 RSpec 发布原始 JSON

jquery - 如何使用jquery将项目添加到无序列表