@pie-lib/charting 4.5.13 → 4.5.16

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/axes.jsx CHANGED
@@ -6,6 +6,7 @@ import { color } from '@pie-lib/render-ui';
6
6
  import { AxisLeft, AxisBottom } from '@vx/axis';
7
7
  import { bandKey, getTickValues, getRotateAngle } from './utils';
8
8
  import MarkLabel from './mark-label';
9
+ import Checkbox from '@material-ui/core/Checkbox';
9
10
 
10
11
  export class TickComponent extends React.Component {
11
12
  changeCategory = (index, newLabel) => {
@@ -23,6 +24,13 @@ export class TickComponent extends React.Component {
23
24
  }
24
25
  };
25
26
 
27
+ changeInteractive = (index, value) => {
28
+ const { categories, onChangeCategory } = this.props;
29
+ const category = categories[index];
30
+
31
+ onChangeCategory(index, { ...category, interactive: !category.interactive });
32
+ };
33
+
26
34
  render() {
27
35
  const {
28
36
  classes,
@@ -33,6 +41,7 @@ export class TickComponent extends React.Component {
33
41
  rotate,
34
42
  top,
35
43
  graphProps,
44
+ defineChart,
36
45
  x,
37
46
  y,
38
47
  formattedValue
@@ -61,7 +70,7 @@ export class TickComponent extends React.Component {
61
70
  x={bandWidth ? barX : x - barWidth / 2}
62
71
  y={6}
63
72
  width={barWidth}
64
- height={24}
73
+ height={4}
65
74
  style={{ pointerEvents: 'none', overflow: 'visible' }}
66
75
  >
67
76
  {index === 0 && (
@@ -89,6 +98,16 @@ export class TickComponent extends React.Component {
89
98
  rotate={rotate}
90
99
  correctness={correctness}
91
100
  />
101
+ <MarkLabel
102
+ inputRef={r => (this.input = r)}
103
+ disabled={!(editable && interactive)}
104
+ mark={category}
105
+ graphProps={graphProps}
106
+ onChange={newLabel => this.changeCategory(index, newLabel)}
107
+ barWidth={barWidth}
108
+ rotate={rotate}
109
+ correctness={correctness}
110
+ />
92
111
  </foreignObject>
93
112
  {deletable && !correctness && (
94
113
  <line
@@ -113,6 +132,45 @@ export class TickComponent extends React.Component {
113
132
  <path d="M128 405.429C128 428.846 147.198 448 170.667 448h170.667C364.802 448 384 428.846 384 405.429V160H128v245.429zM416 96h-80l-26.785-32H202.786L176 96H96v32h320V96z" />
114
133
  </svg>
115
134
  )}
135
+ {defineChart && index === 0 && (
136
+ <text
137
+ x={x - 80}
138
+ y={y + 40 + top}
139
+ width={barWidth}
140
+ height={4}
141
+ style={{
142
+ position: 'absolute',
143
+ pointerEvents: 'none',
144
+ wordBreak: 'break-word',
145
+ overflow: 'visible',
146
+ maxWidth: barWidth,
147
+ display: 'inline-block'
148
+ }}
149
+ >
150
+ <tspan x="0" dy=".6em">
151
+ {' '}
152
+ Student can{' '}
153
+ </tspan>
154
+ <tspan x="0" dy="1.2em">
155
+ {' '}
156
+ set value
157
+ </tspan>
158
+ </text>
159
+ )}
160
+ {defineChart && (
161
+ <foreignObject
162
+ x={x - 24}
163
+ y={y + 20 + top}
164
+ width={barWidth}
165
+ height={4}
166
+ style={{ pointerEvents: 'visible', overflow: 'visible' }}
167
+ >
168
+ <Checkbox
169
+ checked={interactive}
170
+ onChange={e => this.changeInteractive(index, e.target.checked)}
171
+ />
172
+ </foreignObject>
173
+ )}
116
174
  </g>
117
175
  );
118
176
  }
@@ -168,6 +226,7 @@ export class RawChartAxes extends React.Component {
168
226
  onChangeCategory,
169
227
  categories = [],
170
228
  top,
229
+ defineChart,
171
230
  theme
172
231
  } = this.props;
173
232
 
@@ -177,6 +236,7 @@ export class RawChartAxes extends React.Component {
177
236
 
178
237
  const bottomScale =
179
238
  xBand && typeof xBand.rangeRound === 'function' && xBand.rangeRound([0, size.width]);
239
+
180
240
  const bandWidth = xBand && typeof xBand.bandwidth === 'function' && xBand.bandwidth();
181
241
  // for chartType "line", bandWidth will be 0, so we have to calculate it
182
242
  const barWidth = bandWidth || (scale.x && scale.x(domain.max) / categories.length);
@@ -199,6 +259,7 @@ export class RawChartAxes extends React.Component {
199
259
  barWidth,
200
260
  rotate,
201
261
  top,
262
+ defineChart,
202
263
  onChangeCategory,
203
264
  onChange,
204
265
  graphProps,
package/src/chart.jsx CHANGED
@@ -150,6 +150,7 @@ export class Chart extends React.Component {
150
150
  theme
151
151
  } = this.props;
152
152
  let { chartType } = this.props;
153
+ const defineChart = this.props.defineChart || false;
153
154
  const { width, height } = size || {};
154
155
 
155
156
  const { ChartComponent } = this.getChart();
@@ -178,7 +179,7 @@ export class Chart extends React.Component {
178
179
  };
179
180
  log('[render] common:', common);
180
181
 
181
- const maskSize = { x: -10, y: -10, width: width + 20, height: height + 20 };
182
+ const maskSize = { x: -10, y: -10, width: width + 20, height: height + 50 };
182
183
  const { scale } = common.graphProps;
183
184
  const xBand = dataToXBand(scale.x, categories, width, chartType);
184
185
 
@@ -189,12 +190,13 @@ export class Chart extends React.Component {
189
190
  const bandWidth = xBand.bandwidth();
190
191
  // for chartType "line", bandWidth will be 0, so we have to calculate it
191
192
  const barWidth = bandWidth || scale.x(correctValues.domain.max) / categories.length;
193
+ const increaseHeight = defineChart ? 50 : 0;
192
194
 
193
195
  // if there are many categories, we have to rotate their names in order to fit
194
196
  // and we have to add extra value on top of some items
195
197
  const top = getTopPadding(barWidth);
196
198
  const rootCommon = cloneDeep(common);
197
- rootCommon.graphProps.size.height += top;
199
+ rootCommon.graphProps.size.height += top + increaseHeight;
198
200
 
199
201
  return (
200
202
  <div className={classNames(classes.class, className)}>
@@ -214,6 +216,7 @@ export class Chart extends React.Component {
214
216
  />
215
217
  <ChartAxes
216
218
  {...common}
219
+ defineChart={defineChart}
217
220
  categories={categories}
218
221
  xBand={xBand}
219
222
  leftAxis={leftAxis}
package/src/utils.js CHANGED
@@ -81,6 +81,21 @@ export const crowdedTicks = (rangeMax, customLabelStep, size, labelFontSize) =>
81
81
  return size.height / numberOfSegments < labelFontSize && size.height / numberOfSegments > 0.5;
82
82
  };
83
83
 
84
+ // multiply values with 10^number_of_decimals if needed because modulo function(%) is only defined for integers
85
+ const modulo = (a, b) => {
86
+ if (Number.isInteger(b)) {
87
+ return a % b;
88
+ }
89
+
90
+ const decimals = b
91
+ .toString()
92
+ .split('.')
93
+ .pop().length;
94
+ const aux = Math.pow(10, decimals);
95
+
96
+ return (a * aux) % (b * aux);
97
+ };
98
+
84
99
  export const getDomainAndRangeByChartType = (domain, range, size, chartType, labelFontSize) => {
85
100
  let { step, labelStep, min, max } = range || {};
86
101
 
@@ -120,7 +135,7 @@ export const getDomainAndRangeByChartType = (domain, range, size, chartType, lab
120
135
  }
121
136
  }
122
137
 
123
- if (max % step !== 0) {
138
+ if (modulo(max, step) !== 0) {
124
139
  max = max + step;
125
140
  }
126
141