thumb.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var glob = require('glob');
  20. var Canvas = require('canvas');
  21. var echarts = require('echarts');
  22. var fs = require('fs');
  23. var path = require('path');
  24. require('echarts/map/js/china');
  25. var options = {
  26. bar: require('./option/bar'),
  27. area: require('./option/area'),
  28. scatter: require('./option/scatter'),
  29. pie: require('./option/pie'),
  30. graph: require('./option/graph'),
  31. map: require('./option/map')
  32. };
  33. var WIDTH = 600;
  34. var HEIGHT = 400;
  35. echarts.setCanvasCreator(function () {
  36. return createCanvas();
  37. });
  38. var font = new Canvas.Font('Helvetica', '/System/Library/Fonts/Helvetica.dfont');
  39. font.addFace('/System/Library/Fonts/Helvetica.dfont', 'bolder');
  40. glob('../*.js', function (err, themePathList) {
  41. themePathList.forEach(function (themePath) {
  42. var themeName = path.basename(themePath, '.js');
  43. var canvasList = [];
  44. require(themePath);
  45. echarts.util.each(options, function (option) {
  46. var canvas = createCanvas();
  47. var chart = echarts.init(canvas, themeName);
  48. var optionNeedFix = option;
  49. if (option.options) {
  50. optionNeedFix = option.options[0];
  51. }
  52. canvasList.push(canvas);
  53. optionNeedFix.animation = false;
  54. optionNeedFix.textStyle = {
  55. fontFamily: 'Helvetica',
  56. fontSize: 12
  57. };
  58. chart.setOption(option);
  59. chart.dispose();
  60. });
  61. var columnCount = 2;
  62. var outputCanvas = new Canvas(WIDTH * columnCount, HEIGHT * canvasList.length / columnCount);
  63. var outputCtx = outputCanvas.getContext('2d');
  64. canvasList.forEach(function (canvas, idx) {
  65. outputCtx.drawImage(canvas, idx % columnCount * WIDTH, Math.floor(idx / columnCount) * HEIGHT, WIDTH, HEIGHT);
  66. });
  67. fs.writeFileSync('../thumb/' + themeName + '.png', outputCanvas.toBuffer());
  68. });
  69. });
  70. function createCanvas() {
  71. var canvas = new Canvas(WIDTH, HEIGHT);
  72. var ctx = canvas.getContext('2d');
  73. ctx.addFont(font);
  74. return canvas;
  75. }