@czi-sds/data-viz 0.2.0 → 0.3.0

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/dist/index.esm.js CHANGED
@@ -1,4 +1,7 @@
1
- import React, { forwardRef } from 'react';
1
+ import { jsx } from 'react/jsx-runtime';
2
+ import { init } from 'echarts';
3
+ import { useMemo, useEffect, memo, forwardRef, useState, useRef } from 'react';
4
+ import throttle from 'lodash/throttle';
2
5
  import { styled } from '@mui/material/styles';
3
6
 
4
7
  /******************************************************************************
@@ -15,6 +18,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
18
  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
19
  PERFORMANCE OF THIS SOFTWARE.
17
20
  ***************************************************************************** */
21
+ /* global Reflect, Promise */
22
+
18
23
 
19
24
  var __assign = function() {
20
25
  __assign = Object.assign || function __assign(t) {
@@ -27,17 +32,229 @@ var __assign = function() {
27
32
  return __assign.apply(this, arguments);
28
33
  };
29
34
 
35
+ function __rest(s, e) {
36
+ var t = {};
37
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
38
+ t[p] = s[p];
39
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
40
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
41
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
42
+ t[p[i]] = s[p[i]];
43
+ }
44
+ return t;
45
+ }
46
+
30
47
  function __makeTemplateObject(cooked, raw) {
31
48
  if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
32
49
  return cooked;
33
50
  }
34
51
 
35
- var StyledHeatmap = styled("div")(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n width: 100%;\n height: 100px;\n background-color: #212121;\n display: flex;\n justify-content: center;\n align-items: center;\n color: white;\n font-size: 1rem;\n"], ["\n width: 100%;\n height: 100px;\n background-color: #212121;\n display: flex;\n justify-content: center;\n align-items: center;\n color: white;\n font-size: 1rem;\n"])));
52
+ var EMPTY_OBJECT = {};
53
+
54
+ var DEFAULT_ITEM_STYLE = {
55
+ color: function () {
56
+ return "rgb(0, 0, 0)";
57
+ },
58
+ };
59
+ function createChartOptions(_a) {
60
+ var data = _a.data, xAxisData = _a.xAxisData, yAxisData = _a.yAxisData, width = _a.width, height = _a.height, encode = _a.encode, _b = _a.itemStyle, itemStyle = _b === void 0 ? DEFAULT_ITEM_STYLE : _b, symbolSize = _a.symbolSize, gridProp = _a.grid;
61
+ var defaultGrid = {
62
+ height: "".concat(height, "px"),
63
+ left: 0,
64
+ top: 0,
65
+ // (atarashansky): this is the key change to align x and y axis
66
+ // labels to fixed spacings
67
+ width: "".concat(width, "px"),
68
+ };
69
+ var customGrid = typeof gridProp === "function" ? gridProp(defaultGrid) : gridProp;
70
+ return {
71
+ animation: false,
72
+ /**
73
+ * Display reference line and axis value under mouse pointer
74
+ * https://echarts.apache.org/en/option.html#axisPointer
75
+ */
76
+ axisPointer: {
77
+ label: { show: false },
78
+ show: true,
79
+ triggerOn: "mousemove",
80
+ },
81
+ dataset: {
82
+ source: data,
83
+ },
84
+ grid: customGrid || defaultGrid,
85
+ series: [
86
+ {
87
+ emphasis: {
88
+ itemStyle: {
89
+ color: "inherit",
90
+ },
91
+ scale: false,
92
+ },
93
+ encode: encode,
94
+ itemStyle: itemStyle,
95
+ legendHoverLink: false,
96
+ symbolSize: symbolSize,
97
+ type: "scatter",
98
+ },
99
+ ],
100
+ xAxis: [
101
+ {
102
+ axisLabel: { fontSize: 0, rotate: 90 },
103
+ axisLine: {
104
+ show: false,
105
+ },
106
+ axisTick: {
107
+ show: false,
108
+ },
109
+ boundaryGap: true,
110
+ data: xAxisData,
111
+ splitLine: {
112
+ show: false,
113
+ },
114
+ type: "category",
115
+ },
116
+ ],
117
+ yAxis: [
118
+ {
119
+ axisLabel: { fontSize: 0 },
120
+ axisLine: {
121
+ show: false,
122
+ },
123
+ axisTick: {
124
+ show: false,
125
+ },
126
+ boundaryGap: true,
127
+ data: yAxisData,
128
+ splitLine: {
129
+ show: false,
130
+ },
131
+ },
132
+ ],
133
+ };
134
+ }
135
+
136
+ var UPDATE_THROTTLE_MS = 1 * 100;
137
+ function useUpdateChart(_a) {
138
+ var chart = _a.chart, data = _a.data, xAxisData = _a.xAxisData, yAxisData = _a.yAxisData, width = _a.width, height = _a.height, encode = _a.encode, itemStyle = _a.itemStyle, symbolSize = _a.symbolSize, grid = _a.grid;
139
+ var throttledUpdateChart = useMemo(function () {
140
+ return throttle(function () {
141
+ if (!chart || !data || !xAxisData || !yAxisData) {
142
+ return;
143
+ }
144
+ // (thuang): resize() needs to be called before setOption() to prevent
145
+ // TypeError: Cannot read properties of undefined (reading 'shouldBePainted')
146
+ chart.resize();
147
+ chart.setOption(createChartOptions({
148
+ data: data,
149
+ encode: encode,
150
+ grid: grid,
151
+ height: height,
152
+ itemStyle: itemStyle,
153
+ symbolSize: symbolSize,
154
+ width: width,
155
+ xAxisData: xAxisData,
156
+ yAxisData: yAxisData,
157
+ }));
158
+ }, UPDATE_THROTTLE_MS,
159
+ // (thuang): Trailing guarantees that the last call to the function will
160
+ // be executed
161
+ { trailing: true });
162
+ }, [
163
+ chart,
164
+ data,
165
+ xAxisData,
166
+ yAxisData,
167
+ width,
168
+ height,
169
+ encode,
170
+ itemStyle,
171
+ symbolSize,
172
+ grid,
173
+ ]);
174
+ useEffect(function () {
175
+ return function () { return throttledUpdateChart.cancel(); };
176
+ }, [throttledUpdateChart]);
177
+ // Update the charts
178
+ useEffect(function () {
179
+ throttledUpdateChart();
180
+ }, [
181
+ chart,
182
+ data,
183
+ xAxisData,
184
+ yAxisData,
185
+ throttledUpdateChart,
186
+ width,
187
+ height,
188
+ encode,
189
+ itemStyle,
190
+ symbolSize,
191
+ grid,
192
+ ]);
193
+ }
194
+
195
+ var ChartContainer = styled("div")(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n"], ["\n ", "\n"])), getWidthAndHeight);
196
+ function getWidthAndHeight(_a) {
197
+ var width = _a.width, height = _a.height;
198
+ return "\n width: ".concat(width, "px;\n height: ").concat(height, "px;\n ");
199
+ }
36
200
  var templateObject_1;
37
201
 
38
- var Heatmap = forwardRef(function (props, ref) {
39
- var children = props.children;
40
- return (React.createElement(StyledHeatmap, __assign({ ref: ref }, props), children));
41
- });
202
+ function HeatmapChart(props, ref) {
203
+ var width = props.width, height = props.height, _a = props.echartsRendererMode, echartsRendererMode = _a === void 0 ? "canvas" : _a, onChartMouseMove = props.onChartMouseMove, xAxisData = props.xAxisData, yAxisData = props.yAxisData, data = props.data, encode = props.encode, itemStyle = props.itemStyle, _b = props.symbolSize, symbolSize = _b === void 0 ? 5 : _b, grid = props.grid, rest = __rest(props, ["width", "height", "echartsRendererMode", "onChartMouseMove", "xAxisData", "yAxisData", "data", "encode", "itemStyle", "symbolSize", "grid"]);
204
+ if (!width || !height) {
205
+ throw Error("Chart must have width and height > 0");
206
+ }
207
+ var _c = useState(false), isChartInitialized = _c[0], setIsChartInitialized = _c[1];
208
+ var _d = useState(null), chart = _d[0], setChart = _d[1];
209
+ var innerRef = useRef(null);
210
+ // Initialize charts
211
+ useEffect(function () {
212
+ var current = innerRef.current;
213
+ if (!current ||
214
+ isChartInitialized ||
215
+ // (thuang): echart's `init()` will throw error if the container has 0 width or height
216
+ (current === null || current === void 0 ? void 0 : current.getAttribute("height")) === "0" ||
217
+ (current === null || current === void 0 ? void 0 : current.getAttribute("width")) === "0") {
218
+ return;
219
+ }
220
+ setIsChartInitialized(true);
221
+ var rawChart = init(current, EMPTY_OBJECT, {
222
+ renderer: echartsRendererMode,
223
+ useDirtyRect: true,
224
+ });
225
+ if (onChartMouseMove) {
226
+ rawChart.getZr().on("mousemove", function (event) {
227
+ onChartMouseMove(event, rawChart);
228
+ });
229
+ }
230
+ setChart(rawChart);
231
+ }, [innerRef, isChartInitialized, echartsRendererMode, onChartMouseMove]);
232
+ useUpdateChart({
233
+ chart: chart,
234
+ data: data,
235
+ encode: encode,
236
+ grid: grid,
237
+ height: height,
238
+ itemStyle: itemStyle,
239
+ symbolSize: symbolSize,
240
+ width: width,
241
+ xAxisData: xAxisData,
242
+ yAxisData: yAxisData,
243
+ });
244
+ return (jsx(ChartContainer, __assign({ height: height, width: width, ref: handleRef }, rest)));
245
+ function handleRef(element) {
246
+ innerRef.current = element;
247
+ if (!ref)
248
+ return;
249
+ // (thuang): `ref` from `forwardRef` can be a function or a ref object
250
+ if (typeof ref === "function") {
251
+ ref(element);
252
+ }
253
+ else {
254
+ ref.current = element;
255
+ }
256
+ }
257
+ }
258
+ var index = memo(forwardRef(HeatmapChart));
42
259
 
43
- export { Heatmap };
260
+ export { index as HeatmapChart };