@pie-lib/plot 2.27.2 → 2.27.3-next.155

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/src/root.jsx CHANGED
@@ -1,8 +1,7 @@
1
1
  import React from 'react';
2
- import { withStyles } from '@material-ui/core/styles';
2
+ import { styled } from '@mui/material/styles';
3
3
  import PropTypes from 'prop-types';
4
4
  import { select, mouse } from 'd3-selection';
5
- import cn from 'classnames';
6
5
 
7
6
  import { color, Readable } from '@pie-lib/render-ui';
8
7
  import EditableHtml from '@pie-lib/editable-html';
@@ -11,6 +10,91 @@ import { GraphPropsType } from './types';
11
10
  import Label from './label';
12
11
  import { extractTextFromHTML, isEmptyObject, isEmptyString } from './utils';
13
12
 
13
+ const StyledRoot = styled('div')(({ theme }) => ({
14
+ border: `solid 1px ${color.primaryLight()}`,
15
+ color: color.defaults.TEXT,
16
+ backgroundColor: theme.palette.common.white,
17
+ touchAction: 'none',
18
+ position: 'relative',
19
+ boxSizing: 'unset', // to override the default border-box in IBX that breaks the component width layout
20
+ }));
21
+
22
+ const Wrapper = styled('div')({
23
+ display: 'flex',
24
+ position: 'relative',
25
+ });
26
+
27
+ const DefineChartSvg = styled('svg')({
28
+ paddingLeft: '50px',
29
+ overflow: 'visible',
30
+ });
31
+
32
+ const ChartSvg = styled('svg')({
33
+ overflow: 'visible',
34
+ });
35
+
36
+ const GraphBox = styled('g')({
37
+ cursor: 'pointer',
38
+ userSelect: 'none',
39
+ });
40
+
41
+ const GraphTitle = styled('div')(({ theme }) => ({
42
+ color: color.defaults.TEXT,
43
+ fontSize: theme.typography.fontSize + 2,
44
+ padding: `${theme.spacing(1.5)} ${theme.spacing(0.5)} 0`,
45
+ textAlign: 'center',
46
+ '&.disabled': {
47
+ pointerEvents: 'none',
48
+ },
49
+ '&.rightMargin': {
50
+ marginRight: '74px',
51
+ },
52
+ }));
53
+
54
+ const ChartTitle = styled('div')(({ theme }) => ({
55
+ color: color.defaults.TEXT,
56
+ fontSize: theme.typography.fontSize + 4,
57
+ padding: `${theme.spacing(1.5)} ${theme.spacing(0.5)} 0`,
58
+ textAlign: 'center',
59
+ '&.disabled': {
60
+ pointerEvents: 'none',
61
+ },
62
+ '&.rightMargin': {
63
+ marginRight: '74px',
64
+ },
65
+ }));
66
+
67
+ const TopPixelGuides = styled('div')({
68
+ display: 'flex',
69
+ paddingTop: '6px',
70
+ });
71
+
72
+ const TopPixelIndicator = styled('div')({
73
+ display: 'flex',
74
+ flexDirection: 'column',
75
+ alignItems: 'center',
76
+ width: '100px',
77
+ pointerEvents: 'none',
78
+ userSelect: 'none',
79
+ });
80
+
81
+ const SidePixelGuides = styled('div')({
82
+ width: '70px',
83
+ display: 'flex',
84
+ flexDirection: 'column',
85
+ marginRight: '6px',
86
+ });
87
+
88
+ const SidePixelIndicator = styled('div')({
89
+ textAlign: 'right',
90
+ height: '20px',
91
+ pointerEvents: 'none',
92
+ userSelect: 'none',
93
+ '&:not(:last-child)': {
94
+ marginBottom: '80px',
95
+ },
96
+ });
97
+
14
98
  export class Root extends React.Component {
15
99
  constructor(props) {
16
100
  super(props);
@@ -32,7 +116,6 @@ export class Root extends React.Component {
32
116
  labelsPlaceholders: PropTypes.object,
33
117
  onChangeTitle: PropTypes.func,
34
118
  onMouseMove: PropTypes.func,
35
- classes: PropTypes.object.isRequired,
36
119
  showLabels: PropTypes.bool,
37
120
  showTitle: PropTypes.bool,
38
121
  showPixelGuides: PropTypes.bool,
@@ -51,7 +134,7 @@ export class Root extends React.Component {
51
134
  }
52
135
 
53
136
  const { scale, snap } = graphProps;
54
- const coords = mouse(g._groups[0][0]);
137
+ const coords = mouse(g.node());
55
138
  const x = scale.x.invert(coords[0]);
56
139
  const y = scale.y.invert(coords[1]);
57
140
 
@@ -159,7 +242,6 @@ export class Root extends React.Component {
159
242
  titlePlaceholder,
160
243
  graphProps,
161
244
  children,
162
- classes,
163
245
  defineChart,
164
246
  onChangeTitle,
165
247
  isChart,
@@ -200,19 +282,20 @@ export class Root extends React.Component {
200
282
  const nbOfHorizontalLines = parseInt(actualHeight / 100);
201
283
  const sideGridlinesPadding = parseInt(actualHeight % 100);
202
284
  const { titleHeight } = this.state;
285
+
203
286
  return (
204
- <div className={classes.root}>
287
+ <StyledRoot>
205
288
  {showPixelGuides && (
206
- <div className={classes.topPixelGuides} style={{ marginLeft: isChart ? 80 : showLabels ? 30 : 10 }}>
289
+ <TopPixelGuides style={{ marginLeft: isChart ? 80 : showLabels ? 30 : 10 }}>
207
290
  {[...Array(nbOfVerticalLines + 1).keys()].map((value) => (
208
291
  <Readable false key={`top-guide-${value}`}>
209
- <div className={classes.topPixelIndicator}>
292
+ <TopPixelIndicator>
210
293
  <div>{value * 100}px</div>
211
294
  <div>|</div>
212
- </div>
295
+ </TopPixelIndicator>
213
296
  </Readable>
214
297
  ))}
215
- </div>
298
+ </TopPixelGuides>
216
299
  )}
217
300
  {showTitle &&
218
301
  (disabledTitle ? (
@@ -222,31 +305,54 @@ export class Root extends React.Component {
222
305
  ...(isChart && { width: finalWidth }),
223
306
  ...(isEmptyString(extractTextFromHTML(title)) && { display: 'none' }),
224
307
  }}
225
- className={cn(isChart ? classes.chartTitle : classes.graphTitle, classes.disabledTitle)}
226
- dangerouslySetInnerHTML={{ __html: title || '' }}
227
- />
308
+ >
309
+ {isChart ? (
310
+ <ChartTitle className="disabled" dangerouslySetInnerHTML={{ __html: title || '' }} />
311
+ ) : (
312
+ <GraphTitle className="disabled" dangerouslySetInnerHTML={{ __html: title || '' }} />
313
+ )}
314
+ </div>
228
315
  ) : (
229
316
  <div ref={(r) => (this.titleRef = r)}>
230
- <EditableHtml
231
- style={
232
- isChart && {
233
- width: finalWidth,
234
- }
235
- }
236
- className={cn(
237
- { [classes.rightMargin]: showPixelGuides },
238
- isChart ? classes.chartTitle : classes.graphTitle,
239
- )}
240
- markup={title || ''}
241
- onChange={onChangeTitle}
242
- placeholder={
243
- (defineChart && titlePlaceholder) || (!disabledTitle && 'Click here to add a title for this graph')
244
- }
245
- toolbarOpts={{ noPadding: true, noBorder: true }}
246
- activePlugins={activeTitlePlugins}
247
- disableScrollbar
248
- onKeyDown={this.handleKeyDown}
249
- />
317
+ {isChart ? (
318
+ <ChartTitle className={showPixelGuides ? 'rightMargin' : ''}>
319
+ <EditableHtml
320
+ style={
321
+ isChart && {
322
+ width: finalWidth,
323
+ }
324
+ }
325
+ markup={title || ''}
326
+ onChange={onChangeTitle}
327
+ placeholder={
328
+ (defineChart && titlePlaceholder) || (!disabledTitle && 'Click here to add a title for this graph')
329
+ }
330
+ toolbarOpts={{ noPadding: true, noBorder: true }}
331
+ activePlugins={activeTitlePlugins}
332
+ disableScrollbar
333
+ onKeyDown={this.handleKeyDown}
334
+ />
335
+ </ChartTitle>
336
+ ) : (
337
+ <GraphTitle className={showPixelGuides ? 'rightMargin' : ''}>
338
+ <EditableHtml
339
+ style={
340
+ isChart && {
341
+ width: finalWidth,
342
+ }
343
+ }
344
+ markup={title || ''}
345
+ onChange={onChangeTitle}
346
+ placeholder={
347
+ (defineChart && titlePlaceholder) || (!disabledTitle && 'Click here to add a title for this graph')
348
+ }
349
+ toolbarOpts={{ noPadding: true, noBorder: true }}
350
+ activePlugins={activeTitlePlugins}
351
+ disableScrollbar
352
+ onKeyDown={this.handleKeyDown}
353
+ />
354
+ </GraphTitle>
355
+ )}
250
356
  </div>
251
357
  ))}
252
358
  {showLabels && !isChart && (
@@ -262,7 +368,7 @@ export class Root extends React.Component {
262
368
  charactersLimit={labelsCharactersLimit}
263
369
  />
264
370
  )}
265
- <div className={classes.wrapper}>
371
+ <Wrapper>
266
372
  {showLabels && (
267
373
  <Label
268
374
  side="left"
@@ -278,20 +384,35 @@ export class Root extends React.Component {
278
384
  charactersLimit={labelsCharactersLimit}
279
385
  />
280
386
  )}
281
- <svg width={finalWidth} height={finalHeight} className={defineChart ? classes.defineChart : classes.chart}>
282
- <g
283
- ref={(r) => {
284
- this.g = r;
285
- if (rootRef) {
286
- rootRef(r);
287
- }
288
- }}
289
- className={classes.graphBox}
290
- transform={`translate(${leftPadding + (domain.padding || 0)}, ${topPadding + (range.padding || 0)})`}
291
- >
292
- {children}
293
- </g>
294
- </svg>
387
+ {defineChart ? (
388
+ <DefineChartSvg width={finalWidth} height={finalHeight}>
389
+ <GraphBox
390
+ ref={(r) => {
391
+ this.g = r;
392
+ if (rootRef) {
393
+ rootRef(r);
394
+ }
395
+ }}
396
+ transform={`translate(${leftPadding + (domain.padding || 0)}, ${topPadding + (range.padding || 0)})`}
397
+ >
398
+ {children}
399
+ </GraphBox>
400
+ </DefineChartSvg>
401
+ ) : (
402
+ <ChartSvg width={finalWidth} height={finalHeight}>
403
+ <GraphBox
404
+ ref={(r) => {
405
+ this.g = r;
406
+ if (rootRef) {
407
+ rootRef(r);
408
+ }
409
+ }}
410
+ transform={`translate(${leftPadding + (domain.padding || 0)}, ${topPadding + (range.padding || 0)})`}
411
+ >
412
+ {children}
413
+ </GraphBox>
414
+ </ChartSvg>
415
+ )}
295
416
  {showLabels && !isChart && (
296
417
  <Label
297
418
  side="right"
@@ -306,8 +427,7 @@ export class Root extends React.Component {
306
427
  />
307
428
  )}
308
429
  {showPixelGuides && (
309
- <div
310
- className={classes.sidePixelGuides}
430
+ <SidePixelGuides
311
431
  style={{
312
432
  paddingTop: sideGridlinesPadding,
313
433
  marginTop: 31,
@@ -315,12 +435,12 @@ export class Root extends React.Component {
315
435
  >
316
436
  {[...Array(nbOfHorizontalLines + 1).keys()].reverse().map((value) => (
317
437
  <Readable false key={`top-guide-${value}`}>
318
- <div className={classes.sidePixelIndicator}>━ {value * 100}px</div>
438
+ <SidePixelIndicator>━ {value * 100}px</SidePixelIndicator>
319
439
  </Readable>
320
440
  ))}
321
- </div>
441
+ </SidePixelGuides>
322
442
  )}
323
- </div>
443
+ </Wrapper>
324
444
  {showLabels && (
325
445
  <Label
326
446
  side="bottom"
@@ -337,83 +457,9 @@ export class Root extends React.Component {
337
457
  charactersLimit={labelsCharactersLimit}
338
458
  />
339
459
  )}
340
- </div>
460
+ </StyledRoot>
341
461
  );
342
462
  }
343
463
  }
344
464
 
345
- // use default color theme style to avoid color contrast issues
346
- const styles = (theme) => ({
347
- root: {
348
- border: `solid 1px ${color.primaryLight()}`,
349
- color: color.defaults.TEXT,
350
- backgroundColor: theme.palette.common.white,
351
- touchAction: 'none',
352
- position: 'relative',
353
- boxSizing: 'unset', // to override the default border-box in IBX that breaks the component width layout
354
- },
355
- wrapper: {
356
- display: 'flex',
357
- position: 'relative',
358
- },
359
- svg: {},
360
- defineChart: {
361
- paddingLeft: '50px',
362
- overflow: 'visible',
363
- },
364
- chart: {
365
- overflow: 'visible',
366
- },
367
- graphBox: {
368
- cursor: 'pointer',
369
- userSelect: 'none',
370
- },
371
- graphTitle: {
372
- color: color.defaults.TEXT,
373
- fontSize: theme.typography.fontSize + 2,
374
- padding: `${theme.spacing.unit * 1.5}px ${theme.spacing.unit / 2}px 0`,
375
- textAlign: 'center',
376
- },
377
- chartTitle: {
378
- color: color.defaults.TEXT,
379
- fontSize: theme.typography.fontSize + 4,
380
- padding: `${theme.spacing.unit * 1.5}px ${theme.spacing.unit / 2}px 0`,
381
- textAlign: 'center',
382
- },
383
- disabledTitle: {
384
- pointerEvents: 'none',
385
- },
386
- rightMargin: {
387
- marginRight: '74px',
388
- },
389
- topPixelGuides: {
390
- display: 'flex',
391
- paddingTop: '6px',
392
- },
393
- topPixelIndicator: {
394
- display: 'flex',
395
- flexDirection: 'column',
396
- alignItems: 'center',
397
- width: '100px',
398
- pointerEvents: 'none',
399
- userSelect: 'none',
400
- },
401
- sidePixelGuides: {
402
- width: '70px',
403
- display: 'flex',
404
- flexDirection: 'column',
405
- marginRight: '6px',
406
- },
407
- sidePixelIndicator: {
408
- textAlign: 'right',
409
- height: '20px',
410
- pointerEvents: 'none',
411
- userSelect: 'none',
412
-
413
- '&:not(:last-child)': {
414
- marginBottom: '80px',
415
- },
416
- },
417
- });
418
-
419
- export default withStyles(styles)(Root);
465
+ export default Root;
@@ -1,185 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`gridDraggable snapshot render with decimals 1`] = `
4
- <mockConstructor
5
- axis="both"
6
- grid={
7
- Array [
8
- 1,
9
- 1,
10
- ]
11
- }
12
- onDrag={[Function]}
13
- onMouseDown={[Function]}
14
- onStart={[Function]}
15
- onStop={[Function]}
16
- >
17
- <Component
18
- domain={
19
- Object {
20
- "max": 1.6,
21
- "min": -1.5,
22
- "step": 0.3,
23
- }
24
- }
25
- graphProps={
26
- Object {
27
- "domain": Object {
28
- "max": 1,
29
- "min": 0,
30
- "step": 1,
31
- },
32
- "getRootNode": [Function],
33
- "range": Object {
34
- "max": 1,
35
- "min": 0,
36
- "step": 1,
37
- },
38
- "scale": Object {
39
- "x": [MockFunction] {
40
- "calls": Array [
41
- Array [
42
- 1,
43
- ],
44
- Array [
45
- 0,
46
- ],
47
- ],
48
- "results": Array [
49
- Object {
50
- "type": "return",
51
- "value": 1,
52
- },
53
- Object {
54
- "type": "return",
55
- "value": 0,
56
- },
57
- ],
58
- },
59
- "y": [MockFunction] {
60
- "calls": Array [
61
- Array [
62
- 1,
63
- ],
64
- Array [
65
- 0,
66
- ],
67
- ],
68
- "results": Array [
69
- Object {
70
- "type": "return",
71
- "value": 1,
72
- },
73
- Object {
74
- "type": "return",
75
- "value": 0,
76
- },
77
- ],
78
- },
79
- },
80
- "size": Object {
81
- "height": 500,
82
- "width": 500,
83
- },
84
- "snap": Object {
85
- "x": [MockFunction],
86
- "y": [MockFunction],
87
- },
88
- }
89
- }
90
- isDragging={false}
91
- range={
92
- Object {
93
- "max": 3,
94
- "min": -2,
95
- "step": 0.2,
96
- }
97
- }
98
- />
99
- </mockConstructor>
100
- `;
101
-
102
- exports[`gridDraggable snapshot reqular 1`] = `
103
- <mockConstructor
104
- axis="both"
105
- grid={
106
- Array [
107
- 1,
108
- 1,
109
- ]
110
- }
111
- onDrag={[Function]}
112
- onMouseDown={[Function]}
113
- onStart={[Function]}
114
- onStop={[Function]}
115
- >
116
- <Component
117
- graphProps={
118
- Object {
119
- "domain": Object {
120
- "max": 1,
121
- "min": 0,
122
- "step": 1,
123
- },
124
- "getRootNode": [Function],
125
- "range": Object {
126
- "max": 1,
127
- "min": 0,
128
- "step": 1,
129
- },
130
- "scale": Object {
131
- "x": [MockFunction] {
132
- "calls": Array [
133
- Array [
134
- 1,
135
- ],
136
- Array [
137
- 0,
138
- ],
139
- ],
140
- "results": Array [
141
- Object {
142
- "type": "return",
143
- "value": 1,
144
- },
145
- Object {
146
- "type": "return",
147
- "value": 0,
148
- },
149
- ],
150
- },
151
- "y": [MockFunction] {
152
- "calls": Array [
153
- Array [
154
- 1,
155
- ],
156
- Array [
157
- 0,
158
- ],
159
- ],
160
- "results": Array [
161
- Object {
162
- "type": "return",
163
- "value": 1,
164
- },
165
- Object {
166
- "type": "return",
167
- "value": 0,
168
- },
169
- ],
170
- },
171
- },
172
- "size": Object {
173
- "height": 500,
174
- "width": 500,
175
- },
176
- "snap": Object {
177
- "x": [MockFunction],
178
- "y": [MockFunction],
179
- },
180
- }
181
- }
182
- isDragging={false}
183
- />
184
- </mockConstructor>
185
- `;
@@ -1,18 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`root snapshot matches 1`] = `
4
- <div>
5
- <div>
6
- <svg
7
- height={480}
8
- width={540}
9
- >
10
- <g
11
- transform="translate(70, 40)"
12
- >
13
- hi
14
- </g>
15
- </svg>
16
- </div>
17
- </div>
18
- `;