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