服务器端图表.svg与Highcharts和PhantomJS一起导出,从.csv文件加载数据时出错

服务器端图表.svg与Highcharts和PhantomJS一起导出,从.csv文件加载数据时出错,csv,highcharts,export,server-side,phantomjs,Csv,Highcharts,Export,Server Side,Phantomjs,我一直在尝试根据Pact出版的《学习Highcharts》一书中的一章,即“在服务器端运行Highcharts”,开发一个图表(使用Highcharts库)的服务器端图片导出(.svg)解决方案。本书中的示例使用HighchartExport.js脚本和seriesData.js文件中存储的数据以及PhantomJS(一种无头webkit)。我发现以.csv、xml或json等格式从数据库导出数据是“正常的”,所以我根据这些格式制作了一个图表。在正常的浏览器-服务器环境中执行时,该图表工作正常,

我一直在尝试根据Pact出版的《学习Highcharts》一书中的一章,即“在服务器端运行Highcharts”,开发一个图表(使用Highcharts库)的服务器端图片导出(.svg)解决方案。本书中的示例使用HighchartExport.js脚本和seriesData.js文件中存储的数据以及PhantomJS(一种无头webkit)。我发现以.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();

$.get函数在这里不合适。JQuery$.get使用get请求从服务器加载数据。相反,您需要一个普通的文件读取

这是一个你如何做到这一点的例子。


请注意,您没有在脚本中使用'opt'变量。
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
// read the csv file
var data = fs.read('test.csv');

var svg = page.evaluate(function(opt, data) {
    ...
    var lines = data.split('\n');
    // Iterate over the lines and add categories or series
    $.each(lines, function(lineNo, line) {
       ...
    });
},evalArg, data);