我一直在尝试基于 Pact 出版书籍“Learning Highcharts”中的一章——章节:“在服务器端运行 Highcharts”,开发图表(使用 Highcharts 库)解决方案的服务器端图片导出 (.svg)。书中的示例使用了 HighchartExport.js 脚本和存储在 seriesData.js 文件中的数据以及 PhantomJS,一个无头 webkit。我发现以 .csv 或 xml 或 json 等格式从 DB 导出数据是“正常的”,因此我根据http://www.highcharts.com/docs/working-with-data/preprocessing-data-from制作了一个图表-a-file-csv-xml-json。该图表在普通浏览器 - 服务器环境中执行时工作正常,但是当我为 PhantomJS 重写它时,出现了问题。我使用的 PhantomJS 脚本代码如下:
/*
This is HighchartsExportSO.js
phantomJS Script for Highcharts chart
(data in file test.csv)
*/
var system = require('system');
var page = require('webpage').create();
var fs = require('fs');
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
}
/*Callback to enable messages from inside page evaluate function*/
page.onConsoleMessage = function (msg) {
console.log('Page says: ' + msg);
};
page.onResourceRequested = function(requestData, networkRequest) {
console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};
page.onResourceReceived = function(response) {
console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.injectJs("jquery-1.10.2.min.js");
page.injectJs("highcharts.js");
page.injectJs("exporting.js");
// Load width and height from parameters
var width = parseInt(system.args[2], 10) || 1366;
var height = parseInt(system.args[3], 10) || 768;
// Build up result and chart size args for evaluate function
var evalArg = {
width: width,
height: height
};
// The page.evaluate method takes on a function and
// executes it in the context of page object.
var svg = page.evaluate(function(opt) {
$('body').append('<div id="container"/>');
/*Setting chart options*/
var options = {
chart: {
renderTo: 'container',
},
title: {
text: 'Chart Name
},
xAxis: {
type: 'datetime',
labels: {
step: 10,
rotation: -45,
style: {
fontSize: '9px',
fontFamily: 'Verdana, sans-serif'
}
},
categories: []
},
yAxis: {
title: {
text: '% Percentage'
}
},
series: []
};
//Loading data from csv file
$.get('test.csv', function(data) {
// Split the lines
var lines = data.split('\n');
// Iterate over the lines and add categories or series
$.each(lines, function(lineNo, line) {
var items = line.split(',');
// header line containes categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
if (itemNo > 0) options.xAxis.categories.push(item);
});
}
// the rest of the lines contain data with their name in the first position
else {
var series = {
data: []
};
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
}
});
options.series.push(series);
}
});
});
var chart = new Highcharts.Chart(options);
return chart.getSVG();
}, evalArg);
if (fs.isFile("chart.svg")) {
fs.remove("chart.svg");
}
fs.write("chart.svg", svg);
phantom.exit();
因此,当我在 cmd 中执行“phantomjs.exe HighchartsExportSO.js”时,我没有收到任何错误,并且正在生成实际的 .svg 文件,但其中没有数据系列,这让我相信有问题$get 函数。我的问题是为什么这在 PhantomJS 中不起作用,我必须做什么才能让它起作用。(顺便说一句,我在 Win7 环境中工作,PhantomJS 版本是 1.9.2)示例数据文件(test.csv)将包含以下数据:
Dates,01-JUL-13,02-JUL-13,03-JUL-13,04-JUL-13,05-JUL-13,06-JUL-13,07-JUL-13
Series1,74.57,76.91,84.35,85.3,86.25,89.31,89.73
Series2,100,99.99,99.99,99.99,99.99,99.99,99.99
Series3,97.09,99.8,99.85,99.9,99.91,99.92,99.94