thumb.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var glob = require('glob');
  2. var Canvas = require('canvas');
  3. var echarts = require('echarts');
  4. var fs = require('fs');
  5. var path = require('path');
  6. require('echarts/map/js/china');
  7. var options = {
  8. bar: require('./option/bar'),
  9. area: require('./option/area'),
  10. scatter: require('./option/scatter'),
  11. pie: require('./option/pie'),
  12. graph: require('./option/graph'),
  13. map: require('./option/map')
  14. };
  15. var WIDTH = 600;
  16. var HEIGHT = 400;
  17. echarts.setCanvasCreator(function () {
  18. return createCanvas();
  19. });
  20. var font = new Canvas.Font('Helvetica', '/System/Library/Fonts/Helvetica.dfont');
  21. font.addFace('/System/Library/Fonts/Helvetica.dfont', 'bolder');
  22. glob('../*.js', function (err, themePathList) {
  23. themePathList.forEach(function (themePath) {
  24. var themeName = path.basename(themePath, '.js');
  25. var canvasList = [];
  26. require(themePath);
  27. echarts.util.each(options, function (option) {
  28. var canvas = createCanvas();
  29. var chart = echarts.init(canvas, themeName);
  30. var optionNeedFix = option;
  31. if (option.options) {
  32. optionNeedFix = option.options[0];
  33. }
  34. canvasList.push(canvas);
  35. optionNeedFix.animation = false;
  36. optionNeedFix.textStyle = {
  37. fontFamily: 'Helvetica',
  38. fontSize: 12
  39. };
  40. chart.setOption(option);
  41. chart.dispose();
  42. });
  43. var columnCount = 2;
  44. var outputCanvas = new Canvas(WIDTH * columnCount, HEIGHT * canvasList.length / columnCount);
  45. var outputCtx = outputCanvas.getContext('2d');
  46. canvasList.forEach(function (canvas, idx) {
  47. outputCtx.drawImage(canvas, idx % columnCount * WIDTH, Math.floor(idx / columnCount) * HEIGHT, WIDTH, HEIGHT);
  48. });
  49. fs.writeFileSync('../thumb/' + themeName + '.png', outputCanvas.toBuffer());
  50. });
  51. });
  52. function createCanvas() {
  53. var canvas = new Canvas(WIDTH, HEIGHT);
  54. var ctx = canvas.getContext('2d');
  55. ctx.addFont(font);
  56. return canvas;
  57. }