Sparklines.js
1.54 KB
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
import React from 'react';
import $ from 'jquery';
import PropTypes from 'prop-types';
/* eslint-disable */
import 'imports-loader?jQuery=jquery,this=>window!jquery-sparkline';
/* eslint-enable */
class Sparklines extends React.Component {
static propTypes = {
data: PropTypes.arrayOf(
PropTypes.arrayOf(PropTypes.number),
),
options: PropTypes.oneOfType([
PropTypes.arrayOf(
PropTypes.shape({
type: PropTypes.string.isRequired,
}),
),
PropTypes.shape({
type: PropTypes.string.isRequired,
}),
]),
};
static defaultProps = {
data: [],
options: {},
};
componentDidMount() {
this.initSparkline();
}
initSparkline() {
const $el = $(this.sparklineRef);
const model = $.type(this.data) === 'string' ?
this.props.data.replace(/(^,)|(,$)/g, '')
: this.props.data;
const options = this.props.options;
if ($.type(model) === 'array' && $.type(options) === 'array') {
options.forEach((singleOptions, i) => {
if (i === 0) {
$el.sparkline(model[i], singleOptions);
} else { // set composite for next calls
$el.sparkline(model[i], $.extend({ composite: true }, singleOptions));
}
});
} else {
const data = $.type(model) === 'array' ? model : model.split(',');
$el.sparkline(data, options);
}
}
render() {
return (
<div
className="sparkline" ref={(r) => {
this.sparklineRef = r;
}}
/>
);
}
}
export default Sparklines;