@pie-lib/plot 2.2.0 → 2.4.1

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