首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Highcharts和json格式

Highcharts和json格式
EN

Stack Overflow用户
提问于 2012-03-29 16:58:32
回答 1查看 1.1K关注 0票数 0

我在一个名为querytojson.php的文件中有用于查询的代码

代码语言:javascript
复制
if(!$query = @pg_query("SELECT AVG(\"UploadSpeed\") AS \"UploadSpeed\",
                               AVG(\"DownloadSpeed\") AS \"DownloadSpeed\",
                               AVG(\"Latency\") AS \"Latency\",
                               AVG(\"Jitter\") AS \"Jitter\",
                               AVG(\"PacketLoss\") AS \"PacketLoss\" FROM \"ZipPerformance\" "))
die("<br>Errore nella query: " . pg_last_error($query));

while($row = pg_fetch_assoc($query)){
  // aggiungo all'array
  $risultati[] = $row;  
}
// stampo a video i risultati formattati secondo la sintassi di JSON 
echo json_encode($risultati);

json数据的格式如下:

代码语言:javascript
复制
[{"UploadSpeed":"0.342153197182936","DownloadSpeed":"4.35602301750153","Latency":"110.290067528565","Jitter":"0.0333323723888251","PacketLoss":"0.164373075044556"}]

现在,我想用Highcharts库创建一个图形,文件名为index1.html

代码语言:javascript
复制
$(document).ready(function() {
        var options = {

            chart: {

                renderTo: 'container',

                defaultSeriesType: 'column'

            },

            title: {

                text: 'HOBBIT'

            },
            tooltip: {

            },
            labels: {
                html: 'index.html'
            },

            xAxis: {
                categories: []
            },
            yAxis: {

                title: {

                    text: 'Velocità di connessione'

                }

            },

            series: []

        };
})

我希望将json数据直接传递给index.html

EN

回答 1

Stack Overflow用户

发布于 2012-03-29 21:50:18

我做过类似的事情。我这样做的方式是,我在一个sperate js文件中创建了图表,并将图表作为一个函数调用,将JSON对象作为一个变量传入。这是我的一个脚本在我的网站上使用的一个例子。在您的例子中,标签和值是相同的变量,因此循环迭代是不同的,但思想仍然是相同的。

代码语言:javascript
复制
var chart;
function pieChart(id, values, labels, animate){
    if (animate === undefined ){
        animate = true;
    }
    var arrays = new Array();
    for (i=0;i<values.length;i++)   {
        arrays[i] = [labels[i], values[i]];
    }

chart = new Highcharts.Chart({
    chart: {
        renderTo: id,
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    },
    credits: {
        enabled: false
    },
    title: {
        text: 'Event Occurrence',
        style: {
            color: '#000000'
        }
    },
    tooltip: {
        formatter: function() {
            return '<b>'+ this.point.name +'</b>: '+ this.y +' %';
        }
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                color: '#000000',
                connectorColor: '#000000',
                formatter: function() {
                    return '<b>'+ this.point.name +'</b>: '+ this.y +' %';
                }
            }
        },
        series: {
            animation: animate
            }
    },
    series: [{
        type: 'pie',
        name: 'Probability',
        data: arrays
    }]
});
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9922130

复制
相关文章

相似问题

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