chartist-svg 0.2.3 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/index.js +116 -36
- package/package.json +3 -3
package/LICENSE
CHANGED
package/index.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const svgdomCss = require('svgdom-css');
|
|
3
|
-
const Chartist
|
|
3
|
+
const Chartist = require('chartist');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
// CONSTANTS
|
|
9
|
+
// =========
|
|
10
|
+
|
|
11
|
+
/** Path to Chartist CSS file. */
|
|
12
|
+
const CSSPATH = require.resolve('chartist/dist/index.css');
|
|
13
|
+
/** Custom CSS for Chartist. */
|
|
10
14
|
const CSSCUSTOM =
|
|
11
15
|
`.ct-label.ct-vertical {
|
|
12
16
|
font-family: Courier;
|
|
@@ -21,42 +25,118 @@ const CSSCUSTOM =
|
|
|
21
25
|
fill: crimson;
|
|
22
26
|
text-anchor: start;
|
|
23
27
|
}`;
|
|
24
|
-
|
|
28
|
+
/** Map of Chartist functions. */
|
|
25
29
|
const FUNCTION = new Map([
|
|
26
|
-
['bar',
|
|
27
|
-
['line', Chartist.
|
|
28
|
-
['pie',
|
|
30
|
+
['bar', Chartist.BarChart],
|
|
31
|
+
['line', Chartist.LineChart],
|
|
32
|
+
['pie', Chartist.PieChart],
|
|
29
33
|
]);
|
|
30
34
|
|
|
31
35
|
|
|
32
36
|
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
// GLOBAL VARIABLES
|
|
39
|
+
// ================
|
|
40
|
+
|
|
41
|
+
var CSSDATA = ''; // Delay loading CSS file.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
// METHODS
|
|
47
|
+
// =======
|
|
48
|
+
|
|
49
|
+
// UTILITY
|
|
50
|
+
// -------
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create an element with attributes and content.
|
|
54
|
+
* @param {string} name tag name
|
|
55
|
+
* @param {string} content text content
|
|
56
|
+
* @param {object<string, string>} attrs attributes
|
|
57
|
+
* @returns {Element} element
|
|
58
|
+
*/
|
|
59
|
+
function createElement(name, content='', attrs={}) {
|
|
60
|
+
var a = document.createElement(name);
|
|
61
|
+
for (var k in attrs)
|
|
62
|
+
a.setAttribute(k, attrs[k]);
|
|
63
|
+
a.textContent = content;
|
|
64
|
+
return a;
|
|
40
65
|
}
|
|
41
66
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create a title element for Chartist.
|
|
70
|
+
* @param {string} text title text
|
|
71
|
+
* @param {number} x x-coordinate
|
|
72
|
+
* @param {number} y y-coordinate
|
|
73
|
+
* @param {Object} o attributes
|
|
74
|
+
* @returns {Element} title element
|
|
75
|
+
*/
|
|
76
|
+
function createTitle(text, x=0, y=0, o={}) {
|
|
77
|
+
o.x += x;
|
|
78
|
+
o.y += y;
|
|
79
|
+
return createElement('text', text, o);
|
|
45
80
|
}
|
|
46
81
|
|
|
47
82
|
|
|
48
83
|
|
|
49
84
|
|
|
85
|
+
// DEFAULT OPTIONS
|
|
86
|
+
// ---------------
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Set default options.
|
|
90
|
+
* @param {Object} o options
|
|
91
|
+
* @returns {Object} options
|
|
92
|
+
*/
|
|
50
93
|
function defaults(o) {
|
|
51
|
-
|
|
52
|
-
var
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
94
|
+
// Set options.
|
|
95
|
+
var options = Object.assign({
|
|
96
|
+
width: 1200,
|
|
97
|
+
height: 600,
|
|
98
|
+
chartPadding: {
|
|
99
|
+
left: 20,
|
|
100
|
+
right: 100,
|
|
101
|
+
},
|
|
102
|
+
}, o.options, o.chart);
|
|
103
|
+
// Set responsive options.
|
|
104
|
+
var resOptions = o.resOptions || o.resChart || [];
|
|
105
|
+
// Set onDraw handler, css.
|
|
106
|
+
var onDraw = o.onDraw || null;
|
|
107
|
+
var css = o.css || '';
|
|
108
|
+
// Set title and subtitle.
|
|
109
|
+
var h = Math.min(options.width, options.height);
|
|
110
|
+
var title = Object.assign({
|
|
111
|
+
x: 0,
|
|
112
|
+
y: 0,
|
|
113
|
+
height: 0.08*h,
|
|
114
|
+
'font-size': `${0.03*h}px`,
|
|
115
|
+
'font-family': 'Verdana',
|
|
116
|
+
'font-weight': 'bold',
|
|
117
|
+
'text-anchor': 'middle',
|
|
118
|
+
fill: 'crimson',
|
|
119
|
+
role: 'caption',
|
|
120
|
+
}, o.title);
|
|
121
|
+
var subtitle = Object.assign({
|
|
122
|
+
x: 0,
|
|
123
|
+
y: 0,
|
|
124
|
+
height: 0.04*h,
|
|
125
|
+
'font-size': `${0.02*h}px`,
|
|
126
|
+
'font-family': 'Verdana',
|
|
127
|
+
'font-weight': 'bold',
|
|
128
|
+
'text-anchor': 'middle',
|
|
129
|
+
fill: 'indianred',
|
|
130
|
+
}, o.subtitle);
|
|
56
131
|
return Object.assign({options, resOptions, onDraw, css, title, subtitle}, o);
|
|
57
132
|
}
|
|
58
133
|
|
|
59
134
|
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
// MAIN
|
|
138
|
+
// ----
|
|
139
|
+
|
|
60
140
|
/**
|
|
61
141
|
* Generate SVG chart using Chartist.
|
|
62
142
|
* @param {string} type chart type (line, bar, pie)
|
|
@@ -75,35 +155,35 @@ function chartistSvg(type, data, o={}) {
|
|
|
75
155
|
if (!CSSDATA) CSSDATA = fs.readFileSync(CSSPATH, 'utf8');
|
|
76
156
|
|
|
77
157
|
// Setup window with svg.
|
|
78
|
-
var o
|
|
79
|
-
window
|
|
158
|
+
var o = defaults(o);
|
|
159
|
+
window = svgdomCss(o.css + '\n' + CSSCUSTOM + '\n' + CSSDATA);
|
|
80
160
|
document = window.document;
|
|
81
|
-
var w
|
|
82
|
-
var th
|
|
83
|
-
var div
|
|
161
|
+
var w = o.options.width, h = o.options.height;
|
|
162
|
+
var th = o.title.height, sth = o.subtitle.height;
|
|
163
|
+
var div = document.createElement('div');
|
|
84
164
|
document.querySelector('svg').appendChild(div);
|
|
85
165
|
|
|
86
166
|
// Create chart.
|
|
87
|
-
var fn
|
|
167
|
+
var fn = FUNCTION.get(type);
|
|
88
168
|
var chart = new fn(div, data, o.options, o.resOptions);
|
|
89
|
-
return new Promise(
|
|
169
|
+
return new Promise(resolve => {
|
|
90
170
|
if (o.onDraw) chart.on('draw', o.onDraw);
|
|
91
171
|
chart.on('created', () => {
|
|
92
172
|
var svg = div.querySelector('svg');
|
|
93
|
-
var
|
|
94
|
-
var
|
|
173
|
+
var title = createTitle(data.title, 0.5*w, 0.6*th, o.title);
|
|
174
|
+
var subtitle = createTitle(data.subtitle, 0.5*w, th + 0.6*sth, o.subtitle);
|
|
95
175
|
for(var e of div.querySelectorAll('svg > g'))
|
|
96
|
-
e.setAttribute('transform', `translate(0, ${th+sth})`);
|
|
176
|
+
e.setAttribute('transform', `translate(0, ${th + sth})`);
|
|
97
177
|
for(var e of div.querySelectorAll('svg .ct-label.ct-horizontal'))
|
|
98
178
|
e.setAttribute('transform', `rotate(20, ${e.getAttribute('x')}, ${e.getAttribute('y')}) translate(-10, 0)`);
|
|
99
|
-
svg.setAttribute('height', h+th+sth+0.2*h);
|
|
179
|
+
svg.setAttribute('height', h + th + sth + 0.2*h);
|
|
100
180
|
svg.setAttribute('style', '');
|
|
101
|
-
svg.appendChild(
|
|
102
|
-
svg.appendChild(
|
|
181
|
+
svg.appendChild(title);
|
|
182
|
+
svg.appendChild(subtitle);
|
|
103
183
|
window.setComputedStyle(div);
|
|
104
|
-
var
|
|
184
|
+
var text = div.innerHTML;
|
|
105
185
|
div.parentNode.removeChild(div);
|
|
106
|
-
|
|
186
|
+
resolve(text);
|
|
107
187
|
});
|
|
108
188
|
});
|
|
109
189
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chartist-svg",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Generate SVG chart using chartist on node.js.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/nodef/chartist-svg#readme",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"chartist": "
|
|
26
|
-
"svgdom-css": "0.2"
|
|
25
|
+
"chartist": "^1.3",
|
|
26
|
+
"svgdom-css": "^0.2"
|
|
27
27
|
}
|
|
28
28
|
}
|