@pie-lib/plot 2.1.10 → 2.4.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/src/root.jsx CHANGED
@@ -1,38 +1,26 @@
1
1
  import React from 'react';
2
2
  import { ChildrenType } from './types';
3
3
  import { withStyles } from '@material-ui/core/styles';
4
- import Typography from '@material-ui/core/Typography';
5
4
  import { select, mouse } from 'd3-selection';
6
5
  import PropTypes from 'prop-types';
7
6
  import { GraphPropsType } from './types';
8
- import { color } from '@pie-lib/render-ui';
9
- import { findLongestWord, amountToIncreaseWidth } from './utils';
10
-
11
- export const GraphTitle = withStyles(theme => ({
12
- title: {
13
- color: color.text(),
14
- textAlign: 'center',
15
- paddingTop: theme.spacing.unit * 2,
16
- fontSize: theme.typography.fontSize + 6
17
- }
18
- }))(({ value, classes }) => (
19
- <Typography
20
- className={classes.title}
21
- color="primary"
22
- variant="h5"
23
- dangerouslySetInnerHTML={{ __html: value }}
24
- />
25
- ));
7
+ import { color, Readable } from '@pie-lib/render-ui';
8
+ import EditableHtml from '@pie-lib/editable-html';
9
+ import cn from 'classnames';
26
10
 
27
11
  export class Root extends React.Component {
28
12
  static propTypes = {
29
13
  title: PropTypes.string,
30
14
  children: ChildrenType,
15
+ disabledTitle: PropTypes.bool,
31
16
  graphProps: GraphPropsType.isRequired,
17
+ onChangeTitle: PropTypes.func,
32
18
  onMouseMove: PropTypes.func,
33
19
  classes: PropTypes.object.isRequired,
34
- rootRef: PropTypes.func,
35
- paddingLeft: PropTypes.number
20
+ showLabels: PropTypes.bool,
21
+ showTitle: PropTypes.bool,
22
+ showPixelGuides: PropTypes.bool,
23
+ rootRef: PropTypes.func
36
24
  };
37
25
 
38
26
  mouseMove = g => {
@@ -66,46 +54,155 @@ export class Root extends React.Component {
66
54
  }
67
55
 
68
56
  render() {
69
- const { graphProps, children, classes, title, rootRef, paddingLeft } = this.props;
70
- const { size, domain } = graphProps;
71
- const padding = 50;
72
- const longestWord = findLongestWord(domain.axisLabel);
73
- const increaseWidth = amountToIncreaseWidth(longestWord);
57
+ const {
58
+ disabledTitle,
59
+ graphProps,
60
+ children,
61
+ classes,
62
+ onChangeTitle,
63
+ showLabels,
64
+ showPixelGuides,
65
+ showTitle,
66
+ title,
67
+ rootRef
68
+ } = this.props;
69
+ const {
70
+ size: { width = 500, height = 500 },
71
+ domain,
72
+ range
73
+ } = graphProps;
74
+
75
+ const padding = showLabels ? 70 : 40;
76
+ const extraPadding = showLabels ? 16 : 40;
77
+ const finalWidth = width + padding * 2 + (domain.padding || 0) * 2 + extraPadding;
78
+ const finalHeight = height + padding * 2 + (range.padding || 0) * 2;
74
79
 
75
- const finalWidth = size.width + padding * 2 + increaseWidth;
76
- const finalHeight = size.height + padding * 2;
80
+ const activeTitlePlugins = [
81
+ 'bold',
82
+ 'italic',
83
+ 'underline',
84
+ 'strikethrough',
85
+ 'math'
86
+ // 'languageCharacters'
87
+ ];
88
+
89
+ const nbOfVerticalLines = parseInt(width / 100);
90
+ const nbOfHorizontalLines = parseInt(height / 100);
91
+ const sideGridlinesPadding = parseInt(height % 100);
77
92
 
78
93
  return (
79
94
  <div className={classes.root}>
80
- {title && <GraphTitle value={title} />}
81
- <svg width={finalWidth} height={finalHeight} className={classes.svg}>
82
- <g
83
- ref={r => {
84
- this.g = r;
85
- if (rootRef) {
86
- rootRef(r);
87
- }
88
- }}
89
- className={classes.graphBox}
90
- transform={`translate(${paddingLeft || padding}, ${padding})`}
91
- >
92
- {children}
93
- </g>
94
- </svg>
95
+ {showPixelGuides && (
96
+ <div className={classes.topPixelGuides}>
97
+ {[...Array(nbOfVerticalLines + 1).keys()].map(value => (
98
+ <Readable false key={`top-guide-${value}`}>
99
+ <div className={classes.topPixelIndicator}>
100
+ <div>{value * 100}px</div>
101
+ <div>|</div>
102
+ </div>
103
+ </Readable>
104
+ ))}
105
+ </div>
106
+ )}
107
+ {showTitle && (
108
+ <EditableHtml
109
+ className={cn(
110
+ {
111
+ [classes.disabledTitle]: disabledTitle
112
+ },
113
+ classes.graphTitle
114
+ )}
115
+ markup={title || ''}
116
+ width={finalWidth}
117
+ onChange={onChangeTitle}
118
+ placeholder={!disabledTitle && 'Click here to add a title for this graph'}
119
+ toolbarOpts={{ noBorder: true }}
120
+ activePlugins={activeTitlePlugins}
121
+ />
122
+ )}
123
+ <div className={classes.wrapper}>
124
+ <svg width={finalWidth} height={finalHeight} className={classes.svg}>
125
+ <g
126
+ ref={r => {
127
+ this.g = r;
128
+ if (rootRef) {
129
+ rootRef(r);
130
+ }
131
+ }}
132
+ className={classes.graphBox}
133
+ transform={`translate(${padding}, ${padding})`}
134
+ >
135
+ {children}
136
+ </g>
137
+ </svg>
138
+ {showPixelGuides && (
139
+ <div className={classes.sidePixelGuides} style={{ paddingTop: sideGridlinesPadding }}>
140
+ {[...Array(nbOfHorizontalLines + 1).keys()].reverse().map(value => (
141
+ <Readable false key={`top-guide-${value}`}>
142
+ <div className={classes.sidePixelIndicator}>━ {value * 100}px</div>
143
+ </Readable>
144
+ ))}
145
+ </div>
146
+ )}
147
+ </div>
95
148
  </div>
96
149
  );
97
150
  }
98
151
  }
99
- const styles = () => ({
152
+ const styles = theme => ({
100
153
  root: {
101
154
  border: `solid 1px ${color.primaryLight()}`,
102
155
  color: color.text(),
103
156
  backgroundColor: color.background()
104
157
  },
158
+ wrapper: {
159
+ display: 'flex'
160
+ },
105
161
  svg: {},
106
162
  graphBox: {
107
163
  cursor: 'pointer',
108
164
  userSelect: 'none'
165
+ },
166
+ graphTitle: {
167
+ color: color.text(),
168
+ fontSize: theme.typography.fontSize + 2,
169
+ padding: '12px 4px 0',
170
+ textAlign: 'center'
171
+ },
172
+ disabledTitle: {
173
+ pointerEvents: 'none'
174
+ },
175
+ topPixelGuides: {
176
+ display: 'flex',
177
+ paddingTop: '6px',
178
+ marginLeft: '10px'
179
+ },
180
+ topPixelIndicator: {
181
+ color: color.primaryLight(),
182
+ display: 'flex',
183
+ flexDirection: 'column',
184
+ alignItems: 'center',
185
+ width: '100px',
186
+ pointerEvents: 'none',
187
+ userSelect: 'none'
188
+ },
189
+ sidePixelGuides: {
190
+ width: '70px',
191
+ display: 'flex',
192
+ flexDirection: 'column',
193
+ marginTop: '40px',
194
+ marginRight: '6px'
195
+ },
196
+ sidePixelIndicator: {
197
+ color: color.primaryLight(),
198
+ textAlign: 'right',
199
+ height: '20px',
200
+ pointerEvents: 'none',
201
+ userSelect: 'none',
202
+
203
+ '&:not(:last-child)': {
204
+ marginBottom: '80px'
205
+ }
109
206
  }
110
207
  });
111
208
 
package/src/utils.js CHANGED
@@ -139,7 +139,7 @@ export const isDomainRangeEqual = (graphProps, nextGraphProps) => {
139
139
  );
140
140
  };
141
141
 
142
- // findLongestWord is also used in grapghing
142
+ // findLongestWord is also used in graphing
143
143
  export const findLongestWord = label => {
144
144
  let longestWord = (label || '')
145
145
  .replace(/<[^>]+>/g, '')
@@ -149,7 +149,7 @@ export const findLongestWord = label => {
149
149
  return longestWord[0].length;
150
150
  };
151
151
 
152
- // amountToIncreaseWidth is also used in grapghing
152
+ // amountToIncreaseWidth is also used in graphing
153
153
  export const amountToIncreaseWidth = longestWord => {
154
154
  if (!longestWord) {
155
155
  return 0;