-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
191 lines (161 loc) · 4.89 KB
/
index.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const fs = require('fs');
const svgdomCss = require('svgdom-css');
const Chartist = require('chartist');
// CONSTANTS
// =========
/** Path to Chartist CSS file. */
const CSSPATH = require.resolve('chartist/dist/index.css');
/** Custom CSS for Chartist. */
const CSSCUSTOM =
`.ct-label.ct-vertical {
font-family: Courier;
font-weight: bold;
font-size: 14px;
text-anchor: end;
}
.ct-label.ct-horizontal {
font-family: Courier;
font-weight: bold;
font-size: 14px;
fill: crimson;
text-anchor: start;
}`;
/** Map of Chartist functions. */
const FUNCTION = new Map([
['bar', Chartist.BarChart],
['line', Chartist.LineChart],
['pie', Chartist.PieChart],
]);
// GLOBAL VARIABLES
// ================
var CSSDATA = ''; // Delay loading CSS file.
// METHODS
// =======
// UTILITY
// -------
/**
* Create an element with attributes and content.
* @param {string} name tag name
* @param {string} content text content
* @param {object<string, string>} attrs attributes
* @returns {Element} element
*/
function createElement(name, content='', attrs={}) {
var a = document.createElement(name);
for (var k in attrs)
a.setAttribute(k, attrs[k]);
a.textContent = content;
return a;
}
/**
* Create a title element for Chartist.
* @param {string} text title text
* @param {number} x x-coordinate
* @param {number} y y-coordinate
* @param {Object} o attributes
* @returns {Element} title element
*/
function createTitle(text, x=0, y=0, o={}) {
o.x += x;
o.y += y;
return createElement('text', text, o);
}
// DEFAULT OPTIONS
// ---------------
/**
* Set default options.
* @param {Object} o options
* @returns {Object} options
*/
function defaults(o) {
// Set options.
var options = Object.assign({
width: 1200,
height: 600,
chartPadding: {
left: 20,
right: 100,
},
}, o.options, o.chart);
// Set responsive options.
var resOptions = o.resOptions || o.resChart || [];
// Set onDraw handler, css.
var onDraw = o.onDraw || null;
var css = o.css || '';
// Set title and subtitle.
var h = Math.min(options.width, options.height);
var title = Object.assign({
x: 0,
y: 0,
height: 0.08*h,
'font-size': `${0.03*h}px`,
'font-family': 'Verdana',
'font-weight': 'bold',
'text-anchor': 'middle',
fill: 'crimson',
role: 'caption',
}, o.title);
var subtitle = Object.assign({
x: 0,
y: 0,
height: 0.04*h,
'font-size': `${0.02*h}px`,
'font-family': 'Verdana',
'font-weight': 'bold',
'text-anchor': 'middle',
fill: 'indianred',
}, o.subtitle);
return Object.assign({}, o, {options, resOptions, onDraw, css, title, subtitle});
}
// MAIN
// ----
/**
* Generate SVG chart using Chartist.
* @param {string} type chart type (line, bar, pie)
* @param {object} data chartist data (inc. title, subtitle)
* @param {object} [o] options
* @param {object} [o.chart] chartist options (width, height, chartPadding, ...)
* @param {object} [o.options] chartist options (width, height, chartPadding, ...)
* @param {object} [o.resOptions] chartist responsive options
* @param {object} [o.onDraw] chartist on 'draw' handler
* @param {object} [o.css] custom css
* @param {object} [o.title] title attributes (x, y, height, font-size, ...)
* @param {object} [o.subtitle] subtitle attributes (x, y, height, font-size, ...)
* @returns {Promise<string>} svg code (independent)
*/
function chartistSvg(type, data, o={}) {
// Load chartist CSS.
if (!CSSDATA) CSSDATA = fs.readFileSync(CSSPATH, 'utf8');
// Setup window with svg.
var o = defaults(o);
window = svgdomCss(o.css + '\n' + CSSCUSTOM + '\n' + CSSDATA);
document = window.document;
var w = o.options.width, h = o.options.height;
var th = o.title.height, sth = o.subtitle.height;
var div = document.createElement('div');
document.querySelector('svg').appendChild(div);
// Create chart.
var fn = FUNCTION.get(type);
var chart = new fn(div, data, o.options, o.resOptions);
return new Promise(resolve => {
if (o.onDraw) chart.on('draw', o.onDraw);
chart.on('created', () => {
var svg = div.querySelector('svg');
var title = createTitle(data.title, 0.5*w, 0.6*th, o.title);
var subtitle = createTitle(data.subtitle, 0.5*w, th + 0.6*sth, o.subtitle);
for(var e of div.querySelectorAll('svg > g'))
e.setAttribute('transform', `translate(0, ${th + sth})`);
for(var e of div.querySelectorAll('svg .ct-label.ct-horizontal'))
e.setAttribute('transform', `rotate(20, ${e.getAttribute('x')}, ${e.getAttribute('y')}) translate(-10, 0)`);
svg.setAttribute('height', h + th + sth + 0.2*h);
svg.setAttribute('style', '');
svg.appendChild(title);
svg.appendChild(subtitle);
window.setComputedStyle(div);
var text = div.innerHTML;
div.parentNode.removeChild(div);
resolve(text);
});
});
}
module.exports = chartistSvg;