@pie-lib/graphing 2.4.9 → 2.4.12

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.
Files changed (92) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/lib/axis/arrow.js +17 -13
  3. package/lib/axis/arrow.js.map +1 -1
  4. package/lib/axis/axes.js +57 -51
  5. package/lib/axis/axes.js.map +1 -1
  6. package/lib/axis/index.js +5 -1
  7. package/lib/axis/index.js.map +1 -1
  8. package/lib/bg.js +16 -14
  9. package/lib/bg.js.map +1 -1
  10. package/lib/container/index.js +21 -15
  11. package/lib/container/index.js.map +1 -1
  12. package/lib/coordinates-label.js +4 -4
  13. package/lib/coordinates-label.js.map +1 -1
  14. package/lib/graph-with-controls.js +29 -25
  15. package/lib/graph-with-controls.js.map +1 -1
  16. package/lib/graph.js +56 -42
  17. package/lib/graph.js.map +1 -1
  18. package/lib/grid.js +20 -14
  19. package/lib/grid.js.map +1 -1
  20. package/lib/index.js +5 -1
  21. package/lib/index.js.map +1 -1
  22. package/lib/labels.js +28 -24
  23. package/lib/labels.js.map +1 -1
  24. package/lib/mark-label.js +16 -8
  25. package/lib/mark-label.js.map +1 -1
  26. package/lib/toggle-bar.js +18 -16
  27. package/lib/toggle-bar.js.map +1 -1
  28. package/lib/tool-menu.js +22 -16
  29. package/lib/tool-menu.js.map +1 -1
  30. package/lib/tools/circle/bg-circle.js +26 -18
  31. package/lib/tools/circle/bg-circle.js.map +1 -1
  32. package/lib/tools/circle/component.js +30 -28
  33. package/lib/tools/circle/component.js.map +1 -1
  34. package/lib/tools/circle/index.js +4 -4
  35. package/lib/tools/circle/index.js.map +1 -1
  36. package/lib/tools/index.js +16 -15
  37. package/lib/tools/index.js.map +1 -1
  38. package/lib/tools/line/component.js +12 -6
  39. package/lib/tools/line/component.js.map +1 -1
  40. package/lib/tools/parabola/component.js.map +1 -1
  41. package/lib/tools/parabola/index.js +4 -4
  42. package/lib/tools/parabola/index.js.map +1 -1
  43. package/lib/tools/point/component.js +21 -17
  44. package/lib/tools/point/component.js.map +1 -1
  45. package/lib/tools/point/index.js +2 -2
  46. package/lib/tools/polygon/component.js +55 -47
  47. package/lib/tools/polygon/component.js.map +1 -1
  48. package/lib/tools/polygon/index.js +12 -8
  49. package/lib/tools/polygon/index.js.map +1 -1
  50. package/lib/tools/polygon/line.js +26 -18
  51. package/lib/tools/polygon/line.js.map +1 -1
  52. package/lib/tools/polygon/polygon.js +26 -18
  53. package/lib/tools/polygon/polygon.js.map +1 -1
  54. package/lib/tools/ray/component.js +12 -6
  55. package/lib/tools/ray/component.js.map +1 -1
  56. package/lib/tools/segment/component.js +4 -2
  57. package/lib/tools/segment/component.js.map +1 -1
  58. package/lib/tools/shared/arrow-head.js +4 -4
  59. package/lib/tools/shared/arrow-head.js.map +1 -1
  60. package/lib/tools/shared/line/index.js +46 -44
  61. package/lib/tools/shared/line/index.js.map +1 -1
  62. package/lib/tools/shared/line/line-path.js +28 -20
  63. package/lib/tools/shared/line/line-path.js.map +1 -1
  64. package/lib/tools/shared/line/with-root-edge.js +8 -6
  65. package/lib/tools/shared/line/with-root-edge.js.map +1 -1
  66. package/lib/tools/shared/point/arrow-point.js +21 -15
  67. package/lib/tools/shared/point/arrow-point.js.map +1 -1
  68. package/lib/tools/shared/point/arrow.js +22 -50
  69. package/lib/tools/shared/point/arrow.js.map +1 -1
  70. package/lib/tools/shared/point/base-point.js +22 -16
  71. package/lib/tools/shared/point/base-point.js.map +1 -1
  72. package/lib/tools/shared/point/index.js +8 -4
  73. package/lib/tools/shared/point/index.js.map +1 -1
  74. package/lib/tools/shared/styles.js +1 -1
  75. package/lib/tools/shared/types.js +1 -1
  76. package/lib/tools/sine/component.js.map +1 -1
  77. package/lib/tools/sine/index.js +4 -4
  78. package/lib/tools/sine/index.js.map +1 -1
  79. package/lib/tools/vector/component.js +21 -8
  80. package/lib/tools/vector/component.js.map +1 -1
  81. package/lib/undo-redo.js +20 -16
  82. package/lib/undo-redo.js.map +1 -1
  83. package/lib/use-debounce.js +7 -3
  84. package/lib/use-debounce.js.map +1 -1
  85. package/lib/utils.js +70 -5
  86. package/lib/utils.js.map +1 -1
  87. package/package.json +4 -4
  88. package/src/axis/axes.jsx +1 -1
  89. package/src/graph.jsx +13 -6
  90. package/src/tools/shared/point/arrow.jsx +1 -30
  91. package/src/tools/vector/component.jsx +20 -6
  92. package/src/utils.js +62 -4
@@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
5
5
  import { types } from '@pie-lib/plot';
6
6
  import classNames from 'classnames';
7
7
  import { withStyles } from '@material-ui/core/styles';
8
+ import { getDistanceBetweenTwoPoints } from '../../utils';
8
9
 
9
10
  const lineStyles = theme => ({
10
11
  line: styles.line(theme),
@@ -14,8 +15,20 @@ const lineStyles = theme => ({
14
15
  });
15
16
 
16
17
  export const Line = props => {
17
- const { className, classes, disabled, correctness, graphProps, from, to, ...rest } = props;
18
- const { scale } = graphProps;
18
+ const {
19
+ className,
20
+ classes,
21
+ disabled,
22
+ correctness,
23
+ graphProps: { scale },
24
+ from,
25
+ to,
26
+ ...rest
27
+ } = props;
28
+ const startPoint = { x: scale.x(from.x), y: scale.y(from.y) };
29
+ const endPoint = { x: scale.x(to.x), y: scale.y(to.y) };
30
+ const length = getDistanceBetweenTwoPoints(startPoint, endPoint);
31
+
19
32
  return (
20
33
  <line
21
34
  className={classNames(
@@ -24,10 +37,11 @@ export const Line = props => {
24
37
  classes[correctness],
25
38
  className
26
39
  )}
27
- x1={scale.x(from.x)}
28
- y1={scale.y(from.y)}
29
- x2={scale.x(to.x)}
30
- y2={scale.y(to.y)}
40
+ x1={startPoint.x}
41
+ y1={startPoint.y}
42
+ x2={endPoint.x}
43
+ y2={endPoint.y}
44
+ strokeDasharray={length - 7}
31
45
  {...rest}
32
46
  />
33
47
  );
package/src/utils.js CHANGED
@@ -21,14 +21,14 @@ export const getTickValues = prop => {
21
21
 
22
22
  while (tickVal >= prop.min && tickValues.indexOf(tickVal) < 0) {
23
23
  tickValues.push(tickVal);
24
- tickVal = Math.round((tickVal - prop.step) * 1000) / 1000;
24
+ tickVal = Math.round((tickVal - prop.step) * 10000) / 10000;
25
25
  }
26
26
 
27
- tickVal = Math.round(prop.step * 1000) / 1000;
27
+ tickVal = Math.round(prop.step * 10000) / 10000;
28
28
 
29
29
  while (tickVal <= prop.max && tickValues.indexOf(tickVal) < 0) {
30
30
  tickValues.push(tickVal);
31
- tickVal = Math.round((tickVal + prop.step) * 1000) / 1000;
31
+ tickVal = Math.round((tickVal + prop.step) * 10000) / 10000;
32
32
  }
33
33
 
34
34
  // return only ticks that are inside the min-max interval
@@ -114,7 +114,7 @@ export const getMiddleOfTwoPoints = (a, b) => ({
114
114
  y: (a.y + b.y) / 2
115
115
  });
116
116
 
117
- export const roundNumber = number => parseFloat(number.toFixed(3));
117
+ export const roundNumber = number => parseFloat(number.toFixed(4));
118
118
 
119
119
  export const sameAxes = (p1, p2) =>
120
120
  p1 && p2 && (roundNumber(p1.x) === roundNumber(p2.x) || roundNumber(p1.y) === roundNumber(p2.y));
@@ -175,3 +175,61 @@ export const getAdjustedGraphLimits = graphProps => {
175
175
  }
176
176
  };
177
177
  };
178
+
179
+ export const getDistanceBetweenTwoPoints = (a, b) =>
180
+ Math.sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
181
+
182
+ const sortPoints = array => (array || []).sort((a, b) => a.x - b.x || a.y - b.y);
183
+
184
+ // check colliniarity of 3 points (source: https://www.geeksforgeeks.org/program-check-three-points-collinear/)
185
+ const checkCollinearity = (a, b, c) => (a.x - b.x) * (c.y - b.y) === (c.x - b.x) * (a.y - b.y);
186
+
187
+ // 2 lines are overlapping if all 4 points are collinear
188
+ const isSameLine = (markA, markB) =>
189
+ checkCollinearity(markA.from, markB.from, markB.to) &&
190
+ checkCollinearity(markA.to, markB.from, markB.to);
191
+
192
+ const isSameCircle = (markA, markB) =>
193
+ equalPoints(markA.root, markB.root) &&
194
+ getDistanceBetweenTwoPoints(markB.root, markB.edge) ===
195
+ getDistanceBetweenTwoPoints(markA.root, markA.edge);
196
+
197
+ export const isDuplicatedMark = (mark, marks, oldMark) => {
198
+ const { type, building } = mark;
199
+
200
+ if (building) {
201
+ return false;
202
+ }
203
+
204
+ const filteredMarks = (marks || []).filter(m => m.type === type && !m.building);
205
+ const index = filteredMarks.findIndex(m => isEqual(m, oldMark));
206
+
207
+ if (index !== -1) {
208
+ filteredMarks.splice(index, 1);
209
+ }
210
+
211
+ const duplicated = filteredMarks.find(m => {
212
+ if (type === 'circle' || type === 'parabola' || type === 'sine') {
213
+ const { root, edge } = mark;
214
+
215
+ return (
216
+ (equalPoints(root, m.root) && equalPoints(edge, m.edge)) ||
217
+ (type === 'circle' && isSameCircle(m, mark))
218
+ );
219
+ } else if (type === 'line' || type === 'ray' || type === 'segment' || type === 'vector') {
220
+ const { from, to } = mark;
221
+
222
+ return (
223
+ (equalPoints(from, m.from) && equalPoints(to, m.to)) ||
224
+ (equalPoints(from, m.to) && equalPoints(to, m.from)) ||
225
+ (type === 'line' && isSameLine(m, mark))
226
+ );
227
+ } else if (type === 'polygon') {
228
+ return isEqual(sortPoints(cloneDeep(mark.points)), sortPoints(cloneDeep(m.points)));
229
+ } else if (type === 'point') {
230
+ return equalPoints(m, mark);
231
+ }
232
+ });
233
+
234
+ return !!duplicated;
235
+ };