chartist-svg 0.2.1 → 0.2.4
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/README.md +7 -6
- package/index.js +25 -8
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Generate SVG chart using [
|
|
1
|
+
Generate SVG chart using [Chartist].
|
|
2
2
|
> You can paste the SVG code to a file directly.
|
|
3
3
|
|
|
4
4
|
<br>
|
|
5
5
|
|
|
6
6
|
```javascript
|
|
7
|
-
const
|
|
8
|
-
//
|
|
7
|
+
const chartistSvg = require('chartist-svg');
|
|
8
|
+
// chartistSvg(<type>, <data>, [options])
|
|
9
9
|
// -> Promise: "svg code"
|
|
10
10
|
|
|
11
11
|
type: 'line' // 'line'||'bar'||'pie'
|
|
@@ -54,7 +54,7 @@ var data = {
|
|
|
54
54
|
var options = {
|
|
55
55
|
css: '.ct-chart-line .ct-series .ct-point { stroke: green; }'
|
|
56
56
|
};
|
|
57
|
-
|
|
57
|
+
chartistSvg('line', data, options).then(svg => console.log(svg));
|
|
58
58
|
/* (generated svg can be used independently) */
|
|
59
59
|
/* (it is not dependent upon any external css) */
|
|
60
60
|
// <svg xmlns="http://www.w3.org/2000/svg" ...
|
|
@@ -67,6 +67,7 @@ chartist('line', data, options).then(svg => console.log(svg));
|
|
|
67
67
|
|
|
68
68
|
## References
|
|
69
69
|
|
|
70
|
-
- [chartist :: Gion Kunz][
|
|
70
|
+
- [chartist :: Gion Kunz][Chartist]
|
|
71
|
+
- [svg-chartist :: itbilu](https://www.npmjs.com/package/svg-chartist)
|
|
71
72
|
|
|
72
|
-
[
|
|
73
|
+
[Chartist]: https://www.npmjs.com/package/chartist
|
package/index.js
CHANGED
|
@@ -48,14 +48,29 @@ function title(txt, x=0, y=0, o={}) {
|
|
|
48
48
|
|
|
49
49
|
|
|
50
50
|
function defaults(o) {
|
|
51
|
-
var
|
|
51
|
+
var options = Object.assign({width: 1200, height: 600, chartPadding: {left: 20, right: 100}}, o.options, o.chart);
|
|
52
|
+
var resOptions = o.resOptions||o.resChart||[], onDraw = o.onDraw||null;
|
|
53
|
+
var css = o.css||'', h = Math.min(options.width, options.height);
|
|
52
54
|
var title = Object.assign({x: 0, y: 0, height: 0.08*h, 'font-size': `${0.03*h}px`, 'font-family': 'Verdana', 'font-weight': 'bold', fill: 'crimson', 'text-anchor': 'middle', role: 'caption'}, o.title);
|
|
53
55
|
var subtitle = Object.assign({x: 0, y: 0, height: 0.04*h, 'font-size': `${0.02*h}px`, 'font-family': 'Verdana', 'font-weight': 'bold', fill: 'indianred', 'text-anchor': 'middle'}, o.subtitle);
|
|
54
|
-
return Object.assign({
|
|
56
|
+
return Object.assign({options, resOptions, onDraw, css, title, subtitle}, o);
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Generate SVG chart using Chartist.
|
|
62
|
+
* @param {string} type chart type (line, bar, pie)
|
|
63
|
+
* @param {object} data chartist data (inc. title, subtitle)
|
|
64
|
+
* @param {object} [o] options
|
|
65
|
+
* @param {object} [o.options] chartist options (width, height, chartPadding, ...)
|
|
66
|
+
* @param {object} [o.resOptions] chartist responsive options
|
|
67
|
+
* @param {object} [o.onDraw] chartist on 'draw' handler
|
|
68
|
+
* @param {object} [o.css] custom css
|
|
69
|
+
* @param {object} [o.title] title attributes (x, y, height, font-size, ...)
|
|
70
|
+
* @param {object} [o.subtitle] subtitle attributes (x, y, height, font-size, ...)
|
|
71
|
+
* @returns {Promise<string>} svg code (independent)
|
|
72
|
+
*/
|
|
73
|
+
function chartistSvg(type, data, o={}) {
|
|
59
74
|
// Load chartist CSS.
|
|
60
75
|
if (!CSSDATA) CSSDATA = fs.readFileSync(CSSPATH, 'utf8');
|
|
61
76
|
|
|
@@ -63,18 +78,20 @@ function chartistSvg(typ, dat, o={}) {
|
|
|
63
78
|
var o = defaults(o);
|
|
64
79
|
window = svgdomCss(o.css+'\n'+CSSCUSTOM+'\n'+CSSDATA);
|
|
65
80
|
document = window.document;
|
|
66
|
-
var w
|
|
81
|
+
var w = o.options.width, h = o.options.height;
|
|
67
82
|
var th = o.title.height, sth = o.subtitle.height;
|
|
68
83
|
var div = document.createElement('div');
|
|
69
84
|
document.querySelector('svg').appendChild(div);
|
|
70
85
|
|
|
71
86
|
// Create chart.
|
|
72
|
-
var
|
|
87
|
+
var fn = FUNCTION.get(type);
|
|
88
|
+
var chart = new fn(div, data, o.options, o.resOptions);
|
|
73
89
|
return new Promise((fres) => {
|
|
74
|
-
|
|
90
|
+
if (o.onDraw) chart.on('draw', o.onDraw);
|
|
91
|
+
chart.on('created', () => {
|
|
75
92
|
var svg = div.querySelector('svg');
|
|
76
|
-
var ttl = title(
|
|
77
|
-
var stl = title(
|
|
93
|
+
var ttl = title(data.title, 0.5*w, 0.6*th, o.title);
|
|
94
|
+
var stl = title(data.subtitle, 0.5*w, th+0.6*sth, o.subtitle);
|
|
78
95
|
for(var e of div.querySelectorAll('svg > g'))
|
|
79
96
|
e.setAttribute('transform', `translate(0, ${th+sth})`);
|
|
80
97
|
for(var e of div.querySelectorAll('svg .ct-label.ct-horizontal'))
|