通过setData为Yii创建的Highcharts Widget动态更新highcharts

时间:2014-04-08 22:16:25

标签: yii highcharts yii-extensions

如果图表是由Yii扩展程序创建的,是否可以使用chart.series[i].setData方法更新highcharts图表?我之前没有Yii扩展使用过highcharts,并且在我通过声明的变量var chart = ...设置图表后能够引用我的变量。我只想为Yii创建的highcharts调用setData方法...这是创建图表的当前代码:

$this->Widget('ext.highcharts.HighchartsWidget', array(
    'id' => 'shop_pie',
    'options' => array(
        'chart' => array(
            'backgroundColor' => '#efefef'
        ),
        'colors' => $colors,
        'title' => array('text' => 'Shop'),
        'plotOptions' => array(
            'pie' => array(
            'showInLegend' => 'true',
            'dataLabels' => array(
                'formatter' => 'js:function(){return this.point.y}',
                'distance' => -10,
                'color' => '#000'
            ),
        ),
        'series' => $results['series'],
    )
));

我的ajax调用更新图表:

"$('#city_filter').on('change',function(e) {
    $.ajax({
        url: ". $quotedUrl . ",
        data: { 'cities': e.val },
        success: function(data) {
            $('#name_span').text(data[0]);
            //chart.series[0].setData(data[1]); // THIS IS WHERE I NEED TO BE ABLE TO REFERENCE THE CHART
        }
    });
});",

非常感谢提前。 安迪

1 个答案:

答案 0 :(得分:2)

"$('#city_filter').on('change',function(e) {
    $.ajax({
        url: ". $quotedUrl . ",
        data: { 'cities': e.val },
        success: function(data) {
            $('#name_span').text(data[0]);
            var chart = $('#shop_pie').highcharts();
            chart.series[0].setData(data[1]); // THIS IS WHERE I NEED TO BE ABLE TO REFERENCE THE CHART
        }
    });
});",

指的是 http://api.highcharts.com/highcharts#Series.setData

和该文章的例子 http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-setdata/