如何从mysql获取highcharts-convert.js中的系列和类别的数据

时间:2016-10-03 03:16:55

标签: mysql highcharts phantomjs

我使用phantomjs和highcharts-convert.js在服务器上渲染图像。我有名为data.json的JSON脚本。

{
chart: {
    backgroundColor: '#4A4D4E',
    type: 'column'
}, 
xAxis: {
    categories:  ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    labels: {
        formatter: function () {
            return '<span style="fill: white;">' + this.value + '</span>';
        }
    }
},
yAxis: {
    min: 0
},
series: [{
    name: 'Total Interaction',
    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],
    color: 'red'
}]

};

我在cmd中运行此脚本

phantomjs highchart-convert.js -infile data.json -outfile image.png

并且这成功生成了image.png,但我希望从另一个JSON获取类别和系列,这是执行php脚本的结果,如此

 [{"name":"Date","data":["December","January","February","March","April"]},  {"name":"Interaction","data":["99621","148350","107637","113887","103831"]}]

然后我怎么能得到这个?

1 个答案:

答案 0 :(得分:0)

从data.json读取数据并设置一个对象,例如&#34;数据&#34 ;. 从php和set和object中读取数据,例如&#34;系列&#34 ;. 用脚本中的系列替换数据系列。 保存新数据。 使用新数据调用命令行语句。

//  I'm harcoding but you get the data from data.json
var data = {
chart: {
    backgroundColor: '#4A4D4E',
    type: 'column'
}, 
xAxis: {
    categories:  ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    labels: {
        formatter: function () {
            return '<span style="fill: white;">' + this.value + '</span>';
        }
    }
},
yAxis: {
    min: 0
},
series: [{
    name: 'Total Interaction',
    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],
    color: 'red'
}]};

// It's harcoded but you must get the data from the script
var series = [{"name":"Date","data":["December","January","February","March","April"]},  {"name":"Interaction","data":["99621","148350","107637","113887","103831"]}];
//  Replace the series
data.series = series;

// save data as data2.json
// call in command line: phantomjs highchart-convert.js -infile data2.json -outfile image.png