@arcblock/ux 2.4.63 → 2.4.65
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/lib/Sparkline/index.js +201 -0
- package/package.json +4 -4
- package/src/Sparkline/index.js +217 -0
@@ -0,0 +1,201 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = sparkline;
|
7
|
+
|
8
|
+
/* eslint-disable guard-for-in */
|
9
|
+
|
10
|
+
/* eslint-disable no-restricted-syntax */
|
11
|
+
// origin: https://github.com/fnando/sparkline
|
12
|
+
function getY(max, height, diff, value) {
|
13
|
+
return parseFloat((height - value * height / max + diff).toFixed(2));
|
14
|
+
}
|
15
|
+
|
16
|
+
function removeChildren(svg) {
|
17
|
+
[...svg.querySelectorAll('*')].forEach(element => svg.removeChild(element));
|
18
|
+
}
|
19
|
+
|
20
|
+
function defaultFetch(entry) {
|
21
|
+
return entry.value;
|
22
|
+
}
|
23
|
+
|
24
|
+
function buildElement(tag, attrs) {
|
25
|
+
const element = document.createElementNS('http://www.w3.org/2000/svg', tag);
|
26
|
+
|
27
|
+
for (const name in attrs) {
|
28
|
+
element.setAttribute(name, attrs[name]);
|
29
|
+
}
|
30
|
+
|
31
|
+
return element;
|
32
|
+
}
|
33
|
+
|
34
|
+
function sparkline(svg, entries, options) {
|
35
|
+
removeChildren(svg);
|
36
|
+
|
37
|
+
if (entries.length <= 1) {
|
38
|
+
return;
|
39
|
+
} // eslint-disable-next-line no-param-reassign
|
40
|
+
|
41
|
+
|
42
|
+
options = options || {};
|
43
|
+
|
44
|
+
if (typeof entries[0] === 'number') {
|
45
|
+
// eslint-disable-next-line no-param-reassign
|
46
|
+
entries = entries.map(entry => {
|
47
|
+
return {
|
48
|
+
value: entry
|
49
|
+
};
|
50
|
+
});
|
51
|
+
} // This function will be called whenever the mouse moves
|
52
|
+
// over the SVG. You can use it to render something like a
|
53
|
+
// tooltip.
|
54
|
+
|
55
|
+
|
56
|
+
const {
|
57
|
+
onmousemove
|
58
|
+
} = options; // This function will be called whenever the mouse leaves
|
59
|
+
// the SVG area. You can use it to hide the tooltip.
|
60
|
+
|
61
|
+
const {
|
62
|
+
onmouseout
|
63
|
+
} = options; // Should we run in interactive mode? If yes, this will handle the
|
64
|
+
// cursor and spot position when moving the mouse.
|
65
|
+
|
66
|
+
const interactive = 'interactive' in options ? options.interactive : !!onmousemove; // Define how big should be the spot area.
|
67
|
+
|
68
|
+
const spotRadius = options.spotRadius || 2;
|
69
|
+
const spotDiameter = spotRadius * 2; // Define how wide should be the cursor area.
|
70
|
+
|
71
|
+
const cursorWidth = options.cursorWidth || 2; // Get the stroke width; this is used to compute the
|
72
|
+
// rendering offset.
|
73
|
+
|
74
|
+
const strokeWidth = parseFloat(svg.attributes['stroke-width'].value); // By default, data must be formatted as an array of numbers or
|
75
|
+
// an array of objects with the value key (like `[{value: 1}]`).
|
76
|
+
// You can set a custom function to return data for a different
|
77
|
+
// data structure.
|
78
|
+
|
79
|
+
const fetch = options.fetch || defaultFetch; // Retrieve only values, easing the find for the maximum value.
|
80
|
+
|
81
|
+
const values = entries.map(entry => fetch(entry)); // The rendering width will account for the spot size.
|
82
|
+
|
83
|
+
const width = parseFloat(svg.attributes.width.value) - spotDiameter * 2; // Get the SVG element's full height.
|
84
|
+
// This is used
|
85
|
+
|
86
|
+
const fullHeight = parseFloat(svg.attributes.height.value); // The rendering height accounts for stroke width and spot size.
|
87
|
+
|
88
|
+
const height = fullHeight - strokeWidth * 2 - spotDiameter; // The maximum value. This is used to calculate the Y coord of
|
89
|
+
// each sparkline datapoint.
|
90
|
+
|
91
|
+
const max = Math.max(...values) === 0 ? 1 : Math.max(...values); // Some arbitrary value to remove the cursor and spot out of
|
92
|
+
// the viewing canvas.
|
93
|
+
|
94
|
+
const offscreen = -1000; // Cache the last item index.
|
95
|
+
|
96
|
+
const lastItemIndex = values.length - 1; // Calculate the X coord base step.
|
97
|
+
|
98
|
+
const offset = width / lastItemIndex; // Hold all datapoints, which is whatever we got as the entry plus
|
99
|
+
// x/y coords and the index.
|
100
|
+
|
101
|
+
const datapoints = []; // Hold the line coordinates.
|
102
|
+
|
103
|
+
const pathY = getY(max, height, strokeWidth + spotRadius, values[0]);
|
104
|
+
let pathCoords = "M".concat(spotDiameter, " ").concat(pathY);
|
105
|
+
values.forEach((value, index) => {
|
106
|
+
const x = index * offset + spotDiameter;
|
107
|
+
const y = getY(max, height, strokeWidth + spotRadius, value);
|
108
|
+
datapoints.push(Object.assign({}, entries[index], {
|
109
|
+
index,
|
110
|
+
x,
|
111
|
+
y
|
112
|
+
}));
|
113
|
+
pathCoords += " L ".concat(x, " ").concat(y);
|
114
|
+
});
|
115
|
+
const path = buildElement('path', {
|
116
|
+
class: 'sparkline--line',
|
117
|
+
d: pathCoords,
|
118
|
+
fill: 'none'
|
119
|
+
});
|
120
|
+
const fillCoords = "".concat(pathCoords, " V ").concat(fullHeight, " L ").concat(spotDiameter, " ").concat(fullHeight, " Z");
|
121
|
+
const fill = buildElement('path', {
|
122
|
+
class: 'sparkline--fill',
|
123
|
+
d: fillCoords,
|
124
|
+
stroke: 'none'
|
125
|
+
});
|
126
|
+
svg.appendChild(fill);
|
127
|
+
svg.appendChild(path);
|
128
|
+
|
129
|
+
if (!interactive) {
|
130
|
+
return;
|
131
|
+
}
|
132
|
+
|
133
|
+
const cursor = buildElement('line', {
|
134
|
+
class: 'sparkline--cursor',
|
135
|
+
x1: offscreen,
|
136
|
+
x2: offscreen,
|
137
|
+
y1: 0,
|
138
|
+
y2: fullHeight,
|
139
|
+
'stroke-width': cursorWidth
|
140
|
+
});
|
141
|
+
const spot = buildElement('circle', {
|
142
|
+
class: 'sparkline--spot',
|
143
|
+
cx: offscreen,
|
144
|
+
cy: offscreen,
|
145
|
+
r: spotRadius
|
146
|
+
});
|
147
|
+
svg.appendChild(cursor);
|
148
|
+
svg.appendChild(spot);
|
149
|
+
const interactionLayer = buildElement('rect', {
|
150
|
+
width: svg.attributes.width.value,
|
151
|
+
height: svg.attributes.height.value,
|
152
|
+
style: 'fill: transparent; stroke: transparent',
|
153
|
+
class: 'sparkline--interaction-layer'
|
154
|
+
});
|
155
|
+
svg.appendChild(interactionLayer);
|
156
|
+
interactionLayer.addEventListener('mouseout', event => {
|
157
|
+
cursor.setAttribute('x1', offscreen);
|
158
|
+
cursor.setAttribute('x2', offscreen);
|
159
|
+
spot.setAttribute('cx', offscreen);
|
160
|
+
|
161
|
+
if (onmouseout) {
|
162
|
+
onmouseout(event);
|
163
|
+
}
|
164
|
+
});
|
165
|
+
interactionLayer.addEventListener('mousemove', event => {
|
166
|
+
const mouseX = event.offsetX;
|
167
|
+
let nextDataPoint = datapoints.find(entry => {
|
168
|
+
return entry.x >= mouseX;
|
169
|
+
});
|
170
|
+
|
171
|
+
if (!nextDataPoint) {
|
172
|
+
nextDataPoint = datapoints[lastItemIndex];
|
173
|
+
}
|
174
|
+
|
175
|
+
const previousDataPoint = datapoints[datapoints.indexOf(nextDataPoint) - 1];
|
176
|
+
let currentDataPoint;
|
177
|
+
let halfway;
|
178
|
+
|
179
|
+
if (previousDataPoint) {
|
180
|
+
halfway = previousDataPoint.x + (nextDataPoint.x - previousDataPoint.x) / 2;
|
181
|
+
currentDataPoint = mouseX >= halfway ? nextDataPoint : previousDataPoint;
|
182
|
+
} else {
|
183
|
+
currentDataPoint = nextDataPoint;
|
184
|
+
}
|
185
|
+
|
186
|
+
const {
|
187
|
+
x
|
188
|
+
} = currentDataPoint;
|
189
|
+
const {
|
190
|
+
y
|
191
|
+
} = currentDataPoint;
|
192
|
+
spot.setAttribute('cx', x);
|
193
|
+
spot.setAttribute('cy', y);
|
194
|
+
cursor.setAttribute('x1', x);
|
195
|
+
cursor.setAttribute('x2', x);
|
196
|
+
|
197
|
+
if (onmousemove) {
|
198
|
+
onmousemove(event, currentDataPoint);
|
199
|
+
}
|
200
|
+
});
|
201
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@arcblock/ux",
|
3
|
-
"version": "2.4.
|
3
|
+
"version": "2.4.65",
|
4
4
|
"description": "Common used react components for arcblock products",
|
5
5
|
"keywords": [
|
6
6
|
"react",
|
@@ -47,11 +47,11 @@
|
|
47
47
|
"react": ">=18.1.0",
|
48
48
|
"react-ga": "^2.7.0"
|
49
49
|
},
|
50
|
-
"gitHead": "
|
50
|
+
"gitHead": "54eb0e1aefdc859dee7c8883f4d9c89c5aa70273",
|
51
51
|
"dependencies": {
|
52
52
|
"@arcblock/did-motif": "^1.1.10",
|
53
|
-
"@arcblock/icons": "^2.4.
|
54
|
-
"@arcblock/react-hooks": "^2.4.
|
53
|
+
"@arcblock/icons": "^2.4.65",
|
54
|
+
"@arcblock/react-hooks": "^2.4.65",
|
55
55
|
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
|
56
56
|
"@emotion/react": "^11.10.4",
|
57
57
|
"@emotion/styled": "^11.10.4",
|
@@ -0,0 +1,217 @@
|
|
1
|
+
/* eslint-disable guard-for-in */
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
3
|
+
// origin: https://github.com/fnando/sparkline
|
4
|
+
function getY(max, height, diff, value) {
|
5
|
+
return parseFloat((height - (value * height) / max + diff).toFixed(2));
|
6
|
+
}
|
7
|
+
|
8
|
+
function removeChildren(svg) {
|
9
|
+
[...svg.querySelectorAll('*')].forEach((element) => svg.removeChild(element));
|
10
|
+
}
|
11
|
+
|
12
|
+
function defaultFetch(entry) {
|
13
|
+
return entry.value;
|
14
|
+
}
|
15
|
+
|
16
|
+
function buildElement(tag, attrs) {
|
17
|
+
const element = document.createElementNS('http://www.w3.org/2000/svg', tag);
|
18
|
+
|
19
|
+
for (const name in attrs) {
|
20
|
+
element.setAttribute(name, attrs[name]);
|
21
|
+
}
|
22
|
+
|
23
|
+
return element;
|
24
|
+
}
|
25
|
+
|
26
|
+
export default function sparkline(svg, entries, options) {
|
27
|
+
removeChildren(svg);
|
28
|
+
|
29
|
+
if (entries.length <= 1) {
|
30
|
+
return;
|
31
|
+
}
|
32
|
+
|
33
|
+
// eslint-disable-next-line no-param-reassign
|
34
|
+
options = options || {};
|
35
|
+
|
36
|
+
if (typeof entries[0] === 'number') {
|
37
|
+
// eslint-disable-next-line no-param-reassign
|
38
|
+
entries = entries.map((entry) => {
|
39
|
+
return { value: entry };
|
40
|
+
});
|
41
|
+
}
|
42
|
+
|
43
|
+
// This function will be called whenever the mouse moves
|
44
|
+
// over the SVG. You can use it to render something like a
|
45
|
+
// tooltip.
|
46
|
+
const { onmousemove } = options;
|
47
|
+
|
48
|
+
// This function will be called whenever the mouse leaves
|
49
|
+
// the SVG area. You can use it to hide the tooltip.
|
50
|
+
const { onmouseout } = options;
|
51
|
+
|
52
|
+
// Should we run in interactive mode? If yes, this will handle the
|
53
|
+
// cursor and spot position when moving the mouse.
|
54
|
+
const interactive = 'interactive' in options ? options.interactive : !!onmousemove;
|
55
|
+
|
56
|
+
// Define how big should be the spot area.
|
57
|
+
const spotRadius = options.spotRadius || 2;
|
58
|
+
const spotDiameter = spotRadius * 2;
|
59
|
+
|
60
|
+
// Define how wide should be the cursor area.
|
61
|
+
const cursorWidth = options.cursorWidth || 2;
|
62
|
+
|
63
|
+
// Get the stroke width; this is used to compute the
|
64
|
+
// rendering offset.
|
65
|
+
const strokeWidth = parseFloat(svg.attributes['stroke-width'].value);
|
66
|
+
|
67
|
+
// By default, data must be formatted as an array of numbers or
|
68
|
+
// an array of objects with the value key (like `[{value: 1}]`).
|
69
|
+
// You can set a custom function to return data for a different
|
70
|
+
// data structure.
|
71
|
+
const fetch = options.fetch || defaultFetch;
|
72
|
+
|
73
|
+
// Retrieve only values, easing the find for the maximum value.
|
74
|
+
const values = entries.map((entry) => fetch(entry));
|
75
|
+
|
76
|
+
// The rendering width will account for the spot size.
|
77
|
+
const width = parseFloat(svg.attributes.width.value) - spotDiameter * 2;
|
78
|
+
|
79
|
+
// Get the SVG element's full height.
|
80
|
+
// This is used
|
81
|
+
const fullHeight = parseFloat(svg.attributes.height.value);
|
82
|
+
|
83
|
+
// The rendering height accounts for stroke width and spot size.
|
84
|
+
const height = fullHeight - strokeWidth * 2 - spotDiameter;
|
85
|
+
|
86
|
+
// The maximum value. This is used to calculate the Y coord of
|
87
|
+
// each sparkline datapoint.
|
88
|
+
const max = Math.max(...values) === 0 ? 1 : Math.max(...values);
|
89
|
+
|
90
|
+
// Some arbitrary value to remove the cursor and spot out of
|
91
|
+
// the viewing canvas.
|
92
|
+
const offscreen = -1000;
|
93
|
+
|
94
|
+
// Cache the last item index.
|
95
|
+
const lastItemIndex = values.length - 1;
|
96
|
+
|
97
|
+
// Calculate the X coord base step.
|
98
|
+
const offset = width / lastItemIndex;
|
99
|
+
|
100
|
+
// Hold all datapoints, which is whatever we got as the entry plus
|
101
|
+
// x/y coords and the index.
|
102
|
+
const datapoints = [];
|
103
|
+
|
104
|
+
// Hold the line coordinates.
|
105
|
+
const pathY = getY(max, height, strokeWidth + spotRadius, values[0]);
|
106
|
+
let pathCoords = `M${spotDiameter} ${pathY}`;
|
107
|
+
|
108
|
+
values.forEach((value, index) => {
|
109
|
+
const x = index * offset + spotDiameter;
|
110
|
+
const y = getY(max, height, strokeWidth + spotRadius, value);
|
111
|
+
|
112
|
+
datapoints.push(
|
113
|
+
Object.assign({}, entries[index], {
|
114
|
+
index,
|
115
|
+
x,
|
116
|
+
y,
|
117
|
+
})
|
118
|
+
);
|
119
|
+
|
120
|
+
pathCoords += ` L ${x} ${y}`;
|
121
|
+
});
|
122
|
+
|
123
|
+
const path = buildElement('path', {
|
124
|
+
class: 'sparkline--line',
|
125
|
+
d: pathCoords,
|
126
|
+
fill: 'none',
|
127
|
+
});
|
128
|
+
|
129
|
+
const fillCoords = `${pathCoords} V ${fullHeight} L ${spotDiameter} ${fullHeight} Z`;
|
130
|
+
|
131
|
+
const fill = buildElement('path', {
|
132
|
+
class: 'sparkline--fill',
|
133
|
+
d: fillCoords,
|
134
|
+
stroke: 'none',
|
135
|
+
});
|
136
|
+
|
137
|
+
svg.appendChild(fill);
|
138
|
+
svg.appendChild(path);
|
139
|
+
|
140
|
+
if (!interactive) {
|
141
|
+
return;
|
142
|
+
}
|
143
|
+
|
144
|
+
const cursor = buildElement('line', {
|
145
|
+
class: 'sparkline--cursor',
|
146
|
+
x1: offscreen,
|
147
|
+
x2: offscreen,
|
148
|
+
y1: 0,
|
149
|
+
y2: fullHeight,
|
150
|
+
'stroke-width': cursorWidth,
|
151
|
+
});
|
152
|
+
|
153
|
+
const spot = buildElement('circle', {
|
154
|
+
class: 'sparkline--spot',
|
155
|
+
cx: offscreen,
|
156
|
+
cy: offscreen,
|
157
|
+
r: spotRadius,
|
158
|
+
});
|
159
|
+
|
160
|
+
svg.appendChild(cursor);
|
161
|
+
svg.appendChild(spot);
|
162
|
+
|
163
|
+
const interactionLayer = buildElement('rect', {
|
164
|
+
width: svg.attributes.width.value,
|
165
|
+
height: svg.attributes.height.value,
|
166
|
+
style: 'fill: transparent; stroke: transparent',
|
167
|
+
class: 'sparkline--interaction-layer',
|
168
|
+
});
|
169
|
+
svg.appendChild(interactionLayer);
|
170
|
+
|
171
|
+
interactionLayer.addEventListener('mouseout', (event) => {
|
172
|
+
cursor.setAttribute('x1', offscreen);
|
173
|
+
cursor.setAttribute('x2', offscreen);
|
174
|
+
|
175
|
+
spot.setAttribute('cx', offscreen);
|
176
|
+
|
177
|
+
if (onmouseout) {
|
178
|
+
onmouseout(event);
|
179
|
+
}
|
180
|
+
});
|
181
|
+
|
182
|
+
interactionLayer.addEventListener('mousemove', (event) => {
|
183
|
+
const mouseX = event.offsetX;
|
184
|
+
|
185
|
+
let nextDataPoint = datapoints.find((entry) => {
|
186
|
+
return entry.x >= mouseX;
|
187
|
+
});
|
188
|
+
|
189
|
+
if (!nextDataPoint) {
|
190
|
+
nextDataPoint = datapoints[lastItemIndex];
|
191
|
+
}
|
192
|
+
|
193
|
+
const previousDataPoint = datapoints[datapoints.indexOf(nextDataPoint) - 1];
|
194
|
+
let currentDataPoint;
|
195
|
+
let halfway;
|
196
|
+
|
197
|
+
if (previousDataPoint) {
|
198
|
+
halfway = previousDataPoint.x + (nextDataPoint.x - previousDataPoint.x) / 2;
|
199
|
+
currentDataPoint = mouseX >= halfway ? nextDataPoint : previousDataPoint;
|
200
|
+
} else {
|
201
|
+
currentDataPoint = nextDataPoint;
|
202
|
+
}
|
203
|
+
|
204
|
+
const { x } = currentDataPoint;
|
205
|
+
const { y } = currentDataPoint;
|
206
|
+
|
207
|
+
spot.setAttribute('cx', x);
|
208
|
+
spot.setAttribute('cy', y);
|
209
|
+
|
210
|
+
cursor.setAttribute('x1', x);
|
211
|
+
cursor.setAttribute('x2', x);
|
212
|
+
|
213
|
+
if (onmousemove) {
|
214
|
+
onmousemove(event, currentDataPoint);
|
215
|
+
}
|
216
|
+
});
|
217
|
+
}
|