2016-07-14 5 views
0

nodejsとjsdomを使用してサーバーサイドで生成されるツリーマップ(グラフの種類)からsvgコードを抽出しようとしています。jsdom、highchartsでサーバーサイドを作成する

私は正常に「折れ線グラフ」私は私のサーバー上でそれを実行エクスポートし、次のコードを発見した:https://gist.github.com/TorsteinHonsi/e8a1e6971608523eb8dd

私はツリーマップにグラフを変更すると、私は以下のエラーを取得!

私は単につまり私は、この(highchartsで公式ツリーマップの例からが見つかりました:http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/treemap-coloraxis/)にこの

Highcharts.chart('container', { 
    chart: { 
     forExport: true, 
     width: 600, 
     height: 400 
    }, 
    xAxis: { 
     categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
     'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
    }, 
    series: [{ 
     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] 
    }] 
}); 

を変更、ツリーマップに折れ線グラフからコードを変更私は実行すると

Highcharts.chart('container', { 
    chart: { 
     forExport: true, 
     width: 600, 
     height: 400 
    }, 
    series: [{ 
     type: 'treemap', 
     layoutAlgorithm: 'squarified', 
     data: [{ 
      name: 'A', 
      value: 6, 
      colorValue: 1 
     }, { 
      name: 'B', 
      value: 6, 
      colorValue: 2 
     }, { 
      name: 'C', 
      value: 4, 
      colorValue: 3 
     }, { 
      name: 'D', 
      value: 3, 
      colorValue: 4 
     }, { 
      name: 'E', 
      value: 2, 
      colorValue: 5 
     }, { 
      name: 'F', 
      value: 2, 
      colorValue: 6 
     }, { 
      name: 'G', 
      value: 1, 
      colorValue: 7 
     }] 
    }] 
}); 

を"node myscript.js"と表示されます。エラーが発生しました:

/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:8 
(function(D,aa){typeof module==="object"&&module.exports?module.exports=D.document?aa(D):aa:D.Highcharts=aa(D)})(typeof window!=="undefined"?window:this,function(D){function aa(a,b){var c="Highcharts error #"+a+": www.highcharts.com/errors/"+a;if(b)throw Error(c);D.console&&console.log(c)}function pb(a,b,c){this.options=b;this.elem=a;this.prop=c}function E(){var a,b=arguments,c,d={},e=function(a,b){var c,d;typeof a!=="object"&&(a={});for(d in b)b.hasOwnProperty(d)&&(c=b[d],a[d]=c&&typeof c==="object"&& 
                                                                 ^

Error: Highcharts error #17: www.highcharts.com/errors/17 
    at Error (native) 
    at aa (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:8:256) 
    at Object.gb.initSeries (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:214:242) 
    at /opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:236:107 
    at Array.forEach (native) 
    at p (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:25:392) 
    at Object.gb.firstRender (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:236:78) 
    at Object.gb.init (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:214:135) 
    at Object.gb.getArgs (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:213:57) 
    at Object.x.Chart (/opt/dlins/node-v6.3.0-linux-x64/bin/node_modules/highcharts/highcharts.js:212:378) 

/** 
 
* Sample of serverside generation of Highcharts using an extension to jsdom in node.js. 
 
* 
 
* Usage: 
 
* npm install jsdom 
 
* npm install highcharts 
 
* node highcharts-jsdom 
 
*/ 
 

 
/* eslint-env node */ 
 
/* eslint no-console: 0 */ 
 
var jsdom = require('jsdom'), 
 
    fs = require('fs'); 
 

 
// Get the document and window 
 
var doc = jsdom.jsdom('<!doctype html><html><body><div id="container"></div></body></html>'), 
 
    win = doc.defaultView; 
 

 
// Do some modifications to the jsdom document in order to get the SVG bounding 
 
// boxes right. 
 
doc.createElementNS = function (ns, tagName) { 
 
    var elem = doc.createElement(tagName); 
 

 
    // Set private namespace to satisfy jsdom's getter 
 
    elem._namespaceURI = ns; // eslint-disable-line no-underscore-dangle 
 
    /** 
 
    * Pass Highcharts' test for SVG capabilities 
 
    * @returns {undefined} 
 
    */ 
 
    elem.createSVGRect = function() {}; 
 
    /** 
 
    * jsdom doesn't compute layout (see https://github.com/tmpvar/jsdom/issues/135). 
 
    * This getBBox implementation provides just enough information to get Highcharts 
 
    * to render text boxes correctly, and is not intended to work like a general 
 
    * getBBox implementation. The height of the boxes are computed from the sum of 
 
    * tspans and their font sizes. The width is based on an average width for each glyph. 
 
    * It could easily be improved to take font-weight into account. 
 
    * For a more exact result we could to create a map over glyph widths for several 
 
    * fonts and sizes, but it may not be necessary for the purpose. 
 
    * @returns {Object} The bounding box 
 
    */ 
 
    elem.getBBox = function() { 
 
     var lineWidth = 0, 
 
      width = 0, 
 
      height = 0; 
 

 
     [].forEach.call(elem.children.length ? elem.children : [elem], function (child) { 
 
      var fontSize = child.style.fontSize || elem.style.fontSize, 
 
       lineHeight, 
 
       textLength; 
 

 
      // The font size and lineHeight is based on empirical values, copied from 
 
      // the SVGRenderer.fontMetrics function in Highcharts. 
 
      if (/px/.test(fontSize)) { 
 
       fontSize = parseInt(fontSize, 10); 
 
      } else { 
 
       fontSize = /em/.test(fontSize) ? parseFloat(fontSize) * 12 : 12; 
 
      } 
 
      lineHeight = fontSize < 24 ? fontSize + 3 : Math.round(fontSize * 1.2); 
 
      textLength = child.textContent.length * fontSize * 0.55; 
 

 
      // Tspans on the same line 
 
      if (child.getAttribute('dx') !== '0') { 
 
       height += lineHeight; 
 
      } 
 

 
      // New line 
 
      if (child.getAttribute('dy') !== null) { 
 
       lineWidth = 0; 
 
      } 
 

 
      lineWidth += textLength; 
 
      width = Math.max(width, lineWidth); 
 

 
     }); 
 

 
     return { 
 
      x: 0, 
 
      y: 0, 
 
      width: width, 
 
      height: height 
 
     }; 
 
    }; 
 
    return elem; 
 
}; 
 

 
// Require Highcharts with the window shim 
 
var Highcharts = require('highcharts')(win); 
 

 
// Disable all animation 
 
Highcharts.setOptions({ 
 
    plotOptions: { 
 
     series: { 
 
      animation: false, 
 
      dataLabels: { 
 
       defer: false 
 
      } 
 
     } 
 
    } 
 
}); 
 

 
// Generate the chart into the container 
 
/* 
 
// Working simple line chart 
 
Highcharts.chart('container', { 
 
    chart: { 
 
\t \t forExport: true, 
 
\t \t width: 600, 
 
\t \t height: 400 
 
\t }, 
 
\t xAxis: { 
 
\t categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
 
\t \t 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
 
\t }, 
 
\t series: [{ 
 
\t 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] 
 
\t }] 
 
}); 
 
*/ 
 

 
//not working treemap 
 
// http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/treemap-coloraxis/ 
 
Highcharts.chart('container', { 
 
\t chart: { 
 
\t \t forExport: true, 
 
\t \t width: 600, 
 
\t \t height: 400 
 
\t }, 
 
\t series: [{ 
 
\t \t type: 'treemap', 
 
\t \t layoutAlgorithm: 'squarified', 
 
\t \t data: [{ 
 
\t \t \t name: 'A', 
 
\t \t \t value: 6, 
 
\t \t \t colorValue: 1 
 
\t \t }, { 
 
\t \t \t name: 'B', 
 
\t \t \t value: 6, 
 
\t \t \t colorValue: 2 
 
\t \t }, { 
 
\t \t \t name: 'C', 
 
\t \t \t value: 4, 
 
\t \t \t colorValue: 3 
 
\t \t }, { 
 
\t \t \t name: 'D', 
 
\t \t \t value: 3, 
 
\t \t \t colorValue: 4 
 
\t \t }, { 
 
\t \t \t name: 'E', 
 
\t \t \t value: 2, 
 
\t \t \t colorValue: 5 
 
\t \t }, { 
 
\t \t \t name: 'F', 
 
\t \t \t value: 2, 
 
\t \t \t colorValue: 6 
 
\t \t }, { 
 
\t \t \t name: 'G', 
 
\t \t \t value: 1, 
 
\t \t \t colorValue: 7 
 
\t \t }] 
 
\t }] 
 
}); 
 

 
var svg = win.document.getElementById('container').innerHTML; 
 
fs.writeFile('chart.svg', svg, function() { 
 
    console.log('Wrote ' + svg.length + ' bytes to ' + __dirname + '/chart.svg.'); // eslint-disable-line no-path-concat 
 
});

+0

プロジェクトにtreemap.jsモジュールを追加していないように見えます。公式のHighchartsの例で見つけることができます:

+0

しかし、私はモジュール全体を "var Highcharts = require( 'highcharts' )(win); "、node_modules/highchartsには、ツリーマップが存在します。どうすればそれを含めることができますか? – DavidDunham

+0

私はこの問題がこの記事のモジュールの読み込みと似ていると思う:http://www.highcharts.com/blog/192-use-highcharts-to-create-charts-in-react –

答えて

0

特別にロードするために必要なヒートマップとツリーマップモジュール:エラーを生成LETEスクリプトはこれです。 node_modulesのライブラリは自動的にそれを行いません。 VaRのHighchartsの後に追加されるために必要なコードの

そこで二行:

require('highcharts/modules/heatmap')(Highcharts); 
require('highcharts/modules/treemap')(Highcharts); 
関連する問題