duanpin2034 2014-09-27 22:54
浏览 54

Highcharts规范,在$ .getJSON调用后设置检索到的数据

I am having difficulties to get the data received from PHP script and set it into the Gauge graph. I am getting the json result correctly, just do not know how to set it to the right Highchart gauge variables (gaugeOptions).

I'd like to put in #container-C_2 the returned values from PHP, so configure the min, max and set the value on the gauge graph. strong text I think In the $.getJSON() body I need to call $('#container-C_2').highcharts( with some arguments, but don't know how..

Can someone help me here?

Many thanks.

PHP

    $result = array();
    $result['name'][0] = 'A';

    //--some values    
    $result['min'][] = 10;
    $result['max'][] = 50;
    $result['val'][]  = 20;

    $json = array();
    array_push($json,$result);

print json_encode($json);

Client side:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>

        <style type="text/css"> 

        </style>    

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">



        $(document).ready(function() {

    var gaugeOptions = {

        chart: {
            type: 'solidgauge'
        },

        title: null,

        pane: {
            center: ['50%', '100%'],
            size: '100%',
            startAngle: -90,
            endAngle: 90,
            background: {
                backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
                innerRadius: '60%',
                outerRadius: '100%',
                shape: 'arc'
            }
        },

        tooltip: {
            enabled: false
        },

        // the value axis
        yAxis: {
            stops: [
                 [0.25, '#DF5353'], // red
                 [0.5, '#DDDF0D'], // yellow
                 [0.75, '#55BF3B'], // green 
            ],
            lineWidth: 0,
            minorTickInterval: null,
            tickPixelInterval: 400,
            tickWidth: 0,
            title: {
                y: -70
            },
            labels: {
                y: 16
            }
        },

        plotOptions: {
            solidgauge: {
                dataLabels: {
                    y: 5,
                    borderWidth: 0,
                    useHTML: true
                }
            }
        }
    };  //-- gaugeOptions


    // The C_1 gauge
    $('#container-C_1').highcharts(Highcharts.merge(gaugeOptions, {
        yAxis: {
            min: -50,
            max: 50,
            title: {
                text: 'graph 1'
            }
        },

        credits: {
            enabled: false
        },

        series: [{
            name: 'C_1',
            data: [-50],
            dataLabels: {
                format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                    ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y}</span><br/>' +
                       '<span style="font-size:12px;color:silver">  graph... </span></div>'
            },
            tooltip: {
                valueSuffix: ' graph... '
            }
        }]

    })); 


     // The C_2 gauge
            $('#container-C_2').highcharts(Highcharts.merge(gaugeOptions, {
                yAxis: {
                    min: -50,
                    max: 50,
                    title: {
                        text: 'C_2'
                    }
                },
                series: [

                {
                    name: 'C_2',
                    data: [45],
                    dataLabels: {
                        format: '<div style="text-align:center"><span style="font-size:25px;color:' +
                            ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '">{y:.1f}</span><br/>' +
                               '<span style="font-size:12px;color:silver"> graph... </span></div>'
                    },
                    tooltip: {
                        valueSuffix: ' graph... '
                    } 
                }

                ]

            })); //--container-C_2


    $.getJSON("query.php", function(json) {

         //this WORKS!
         alert("name, grow_rate: " + json[0]['name'] + ","+ json[0]['val'] );


        //This seems not to have any effect
         gaugeOptions.yAxis.min = json[0]['min'];
         gaugeOptions.yAxis.max = json[0]['max'];
         gaugeOptions.yAxis.title.text = 'JUST SOME TEXT...';

        gaugeOptions.series[0] = {};
        //gaugeOptions.series[0].name = json[0]['name'][0];
        gaugeOptions.series[0].name = json[0]['name'];
        gaugeOptions.series[0].data = json[0]['val'];


//--            stucked here, how can I passed retreived values to the graph?
    // chart = new Highcharts.merge(gaugeOptions);



     });


});  //-- ready()

        </script>


        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
        <script src="http://code.highcharts.com/highcharts-more.js"></script>


<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>

<script src="http://code.highcharts.com/modules/solid-gauge.src.js"></script>

<div style="width: 600px; height: 400px; margin: 0 auto">
    <div id="container-C_1" style="width: 300px; height: 200px; float: left"></div>
    <div id="container-C_2" style="width: 300px; height: 200px; float: left"></div>
</div>



    </head>

    <body>

    </body>
</html>
  • 写回答

1条回答 默认 最新

  • donglu1973 2016-06-08 13:00
    关注
    var point = $('#container-C_2').highcharts().series[0].points[0];
    point.update(json[0]['val']);
    

    Instead of this

    //This seems not to have any effect
    gaugeOptions.yAxis.min = json[0]['min'];
    gaugeOptions.yAxis.max = json[0]['max'];
    gaugeOptions.yAxis.title.text = 'JUST SOME TEXT...';
    
    gaugeOptions.series[0] = {};
    //gaugeOptions.series[0].name = json[0]['name'][0];
    gaugeOptions.series[0].name = json[0]['name'];
    gaugeOptions.series[0].data = json[0]['val'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私