UNPKG

9.57 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var slicedToArray = require('./slicedToArray-ef426d0f.js');
8require('./unsupportedIterableToArray-8a00e599.js');
9var React = require('react');
10var React__default = _interopDefault(React);
11require('./_commonjsHelpers-72d386ba.js');
12require('./index-b0606964.js');
13require('./defineProperty-0921a47c.js');
14var toConsumableArray = require('./toConsumableArray-7f36359f.js');
15var _styled = require('styled-components');
16var _styled__default = _interopDefault(_styled);
17require('./getPrototypeOf-e2d1e599.js');
18require('./color.js');
19require('./components.js');
20require('./contains-component.js');
21var css = require('./css.js');
22require('./dayjs.min-aa59a48e.js');
23require('./date.js');
24require('./miscellaneous.js');
25require('./environment.js');
26require('./font.js');
27require('./math-ecfd5d91.js');
28require('./characters.js');
29require('./format.js');
30require('./keycodes.js');
31require('./url.js');
32require('./web3.js');
33require('./constants.js');
34require('./breakpoints.js');
35var springs = require('./springs.js');
36require('./text-styles.js');
37var _extends$1 = require('./extends-40571110.js');
38var objectWithoutProperties = require('./objectWithoutProperties-35db8ab0.js');
39require('./objectWithoutPropertiesLoose-1af20ad0.js');
40require('react-dom');
41var web = require('./web-d0294535.js');
42var proptypes = require('./proptypes-956d3000.js');
43
44var LABELS_HEIGHT = 30;
45var WIDTH_DEFAULT = 300;
46
47function useMeasuredWidth() {
48 var ref = React.useRef();
49
50 var _useState = React.useState(WIDTH_DEFAULT),
51 _useState2 = slicedToArray._slicedToArray(_useState, 2),
52 measuredWidth = _useState2[0],
53 setMeasuredWidth = _useState2[1];
54
55 var onResize = React.useCallback(function () {
56 if (ref.current) {
57 setMeasuredWidth(ref.current.clientWidth);
58 }
59 }, []);
60 var onRef = React.useCallback(function (element) {
61 ref.current = element;
62 onResize();
63 }, [onResize]);
64 React.useEffect(function () {
65 window.addEventListener('resize', onResize);
66 return function () {
67 return window.removeEventListener('resize', onResize);
68 };
69 }, [onResize]);
70 return [measuredWidth, onRef];
71}
72
73var _StyledSvg = _styled__default("svg").withConfig({
74 displayName: "LineChart___StyledSvg",
75 componentId: "sc-8kiakb-0"
76})(["display:block"]);
77
78var _StyledText = _styled__default("text").withConfig({
79 displayName: "LineChart___StyledText",
80 componentId: "sc-8kiakb-1"
81})(["alignment-baseline:middle;font-size:12px;font-weight:300;", ";"], css.unselectable);
82
83function LineChart(_ref) {
84 var animDelay = _ref.animDelay,
85 borderColor = _ref.borderColor,
86 color = _ref.color,
87 dotRadius = _ref.dotRadius,
88 height = _ref.height,
89 label = _ref.label,
90 labelColor = _ref.labelColor,
91 linesProps = _ref.lines,
92 reset = _ref.reset,
93 springConfig = _ref.springConfig,
94 total = _ref.total,
95 widthProps = _ref.width,
96 props = objectWithoutProperties._objectWithoutProperties(_ref, ["animDelay", "borderColor", "color", "dotRadius", "height", "label", "labelColor", "lines", "reset", "springConfig", "total", "width"]);
97
98 var _useMeasuredWidth = useMeasuredWidth(),
99 _useMeasuredWidth2 = slicedToArray._slicedToArray(_useMeasuredWidth, 2),
100 width = _useMeasuredWidth2[0],
101 onSvgRef = _useMeasuredWidth2[1];
102
103 var lines = React.useMemo(function () {
104 return linesProps.map(function (lineOrValues) {
105 return Array.isArray(lineOrValues) ? {
106 values: lineOrValues
107 } : lineOrValues;
108 });
109 }, [linesProps]); // the count of provided values
110
111 var valuesCount = React.useMemo(function () {
112 // All the values have the same length, so we can use the first one.
113 return lines[0] ? lines[0].values.length : 0;
114 }, [lines]); // the total amount of values
115
116 var totalCount = React.useMemo(function () {
117 // If no total is provided, the total is the number of provided values.
118 return total > 0 && total > valuesCount ? total : valuesCount;
119 }, [valuesCount, total]);
120 var getX = React.useCallback(function (index) {
121 return width / Math.max(1, totalCount - 1) * index;
122 }, [width, totalCount]);
123 var getY = React.useCallback(function (percentage, progress, height) {
124 var padding = dotRadius + 2;
125 return height - padding - (height - padding * 2) * percentage * progress;
126 }, [dotRadius]);
127 var getLabelPosition = React.useCallback(function (index, length) {
128 if (index === 0) return 'start';
129 if (index === length - 1) return 'end';
130 return 'middle';
131 }, []);
132 var labels = label && totalCount > 0 ? toConsumableArray._toConsumableArray(Array(totalCount).keys()).map(label) : null;
133 var chartHeight = height - (labels ? LABELS_HEIGHT : 0);
134 var rectangle = /*#__PURE__*/React__default.createElement("rect", {
135 width: width,
136 height: chartHeight,
137 rx: "3",
138 ry: "3",
139 fill: "#ffffff",
140 strokeWidth: "1",
141 stroke: borderColor
142 });
143 return /*#__PURE__*/React__default.createElement(web.Spring, {
144 from: {
145 progress: 0
146 },
147 to: {
148 progress: 1
149 },
150 config: springConfig,
151 delay: animDelay,
152 reset: reset
153 }, function (_ref2) {
154 var progress = _ref2.progress;
155 return /*#__PURE__*/React__default.createElement(_StyledSvg, _extends$1._extends({
156 ref: onSvgRef,
157 viewBox: "0 0 ".concat(width, " ").concat(height),
158 width: widthProps || 'auto',
159 height: "auto"
160 }, props), /*#__PURE__*/React__default.createElement("mask", {
161 id: "chart-mask"
162 }, rectangle), rectangle, /*#__PURE__*/React__default.createElement("g", {
163 mask: "url(#chart-mask)"
164 }, totalCount > 0 && /*#__PURE__*/React__default.createElement("path", {
165 d: "\n ".concat(toConsumableArray._toConsumableArray(new Array(totalCount - 1)).reduce(function (path, _, index) {
166 return "".concat(path, " M ").concat(getX(index), ",").concat(chartHeight, " l 0,-8");
167 }, ''), "\n "),
168 stroke: borderColor,
169 strokeWidth: "1"
170 }), lines.map(function (line, lineIndex) {
171 return /*#__PURE__*/React__default.createElement("g", {
172 key: "line-plot-".concat(line.id || lineIndex)
173 }, /*#__PURE__*/React__default.createElement("path", {
174 d: "\n M\n ".concat(getX(0), ",\n ").concat(getY(line.values[0], progress, chartHeight), "\n\n ").concat(line.values.slice(1).map(function (val, index) {
175 return "L\n ".concat(getX((index + 1) * progress), ",\n ").concat(getY(val, progress, chartHeight), "\n ");
176 }).join(''), "\n "),
177 fill: "transparent",
178 stroke: line.color || color(lineIndex, {
179 lines: lines
180 }),
181 strokeWidth: "2"
182 }), line.values.slice(1, -1).map(function (val, index) {
183 return /*#__PURE__*/React__default.createElement("circle", {
184 key: index,
185 cx: getX(index + 1) * progress,
186 cy: getY(val, progress, chartHeight),
187 r: dotRadius,
188 fill: "white",
189 stroke: line.color || color(lineIndex, {
190 lines: lines
191 }),
192 strokeWidth: "1"
193 });
194 }));
195 }), /*#__PURE__*/React__default.createElement("line", {
196 x1: getX(valuesCount - 1) * progress,
197 y1: "0",
198 x2: getX(valuesCount - 1) * progress,
199 y2: chartHeight,
200 stroke: "#DAEAEF",
201 strokeWidth: "3"
202 })), labels && /*#__PURE__*/React__default.createElement("g", {
203 transform: "translate(0,".concat(chartHeight, ")")
204 }, labels.map(function (label, index) {
205 return /*#__PURE__*/React__default.createElement(_StyledText, {
206 key: index,
207 x: getX(index),
208 y: LABELS_HEIGHT / 2,
209 textAnchor: getLabelPosition(index, labels.length),
210 fill: labelColor
211 }, label);
212 })));
213 });
214}
215
216LineChart.propTypes = {
217 springConfig: proptypes.PropTypes._spring,
218 total: proptypes.PropTypes.number,
219 width: proptypes.PropTypes.number,
220 height: proptypes.PropTypes.number,
221 dotRadius: proptypes.PropTypes.number,
222 animDelay: proptypes.PropTypes.number,
223 borderColor: proptypes.PropTypes.string,
224 labelColor: proptypes.PropTypes.string,
225 reset: proptypes.PropTypes.bool,
226 lines: proptypes.PropTypes.arrayOf(proptypes.PropTypes.oneOfType([proptypes.PropTypes.shape({
227 id: proptypes.PropTypes.number,
228 values: proptypes.PropTypes.arrayOf(proptypes.PropTypes.number).isRequired,
229 // numbers between 0 and 1
230 color: proptypes.PropTypes.string // overrides the color() prop if set
231
232 }), // values can also be passed directly
233 proptypes.PropTypes.arrayOf(proptypes.PropTypes.number)])),
234 label: proptypes.PropTypes.oneOfType([proptypes.PropTypes.func, proptypes.PropTypes._null]),
235 color: proptypes.PropTypes.func
236};
237LineChart.defaultProps = {
238 springConfig: springs.springs.lazy,
239 total: -1,
240 height: 200,
241 dotRadius: 7 / 2,
242 animDelay: 500,
243 reset: false,
244 borderColor: 'rgba(209, 209, 209, 0.5)',
245 labelColor: '#6d777b',
246 lines: [],
247 label: function label(index) {
248 return index + 1;
249 },
250 color: function color(index, _ref3) {
251 var lines = _ref3.lines;
252 return "hsl(".concat((index * (360 / lines.length) + 40) % 360, ", 60%, 70%)");
253 }
254};
255
256exports.default = LineChart;
257//# sourceMappingURL=LineChart.js.map