私は、Pact 出版の本「Learning Highcharts」の章に基づいて、グラフ (Highcharts ライブラリを使用) ソリューションのサーバー側画像エクスポート (.svg) を開発しようとしています – 章:「Running Highcharts on the Server Side」. この本の例では、HighchartExport.js スクリプトと seriesData.js ファイルに保存されたデータ、およびヘッドレス Webkit である PhantomJS を使用しています。DB から .csv、xml、json などの形式でデータをエクスポートするのは「普通」だと思うので、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