@semcore/d3-chart 2.1.0 → 2.2.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.
Files changed (101) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/lib/cjs/Area.js +70 -29
  3. package/lib/cjs/Area.js.map +1 -1
  4. package/lib/cjs/Bar.js +6 -4
  5. package/lib/cjs/Bar.js.map +1 -1
  6. package/lib/cjs/Bubble.js +1 -0
  7. package/lib/cjs/Bubble.js.map +1 -1
  8. package/lib/cjs/Donut.js +120 -44
  9. package/lib/cjs/Donut.js.map +1 -1
  10. package/lib/cjs/Dots.js +24 -25
  11. package/lib/cjs/Dots.js.map +1 -1
  12. package/lib/cjs/GroupBar.js +7 -7
  13. package/lib/cjs/GroupBar.js.map +1 -1
  14. package/lib/cjs/Line.js +4 -2
  15. package/lib/cjs/Line.js.map +1 -1
  16. package/lib/cjs/ReferenceLine.js +258 -0
  17. package/lib/cjs/ReferenceLine.js.map +1 -0
  18. package/lib/cjs/ResponsiveContainer.js +1 -1
  19. package/lib/cjs/ResponsiveContainer.js.map +1 -1
  20. package/lib/cjs/ScatterPlot.js +1 -0
  21. package/lib/cjs/ScatterPlot.js.map +1 -1
  22. package/lib/cjs/StackBar.js +6 -6
  23. package/lib/cjs/StackedArea.js +7 -7
  24. package/lib/cjs/Tooltip.js +3 -2
  25. package/lib/cjs/Tooltip.js.map +1 -1
  26. package/lib/cjs/createElement.js +4 -3
  27. package/lib/cjs/createElement.js.map +1 -1
  28. package/lib/cjs/index.js +8 -0
  29. package/lib/cjs/index.js.map +1 -1
  30. package/lib/cjs/style/dot.shadow.css +10 -5
  31. package/lib/cjs/style/reference-line.shadow.css +37 -0
  32. package/lib/cjs/types/Area.d.ts +1 -0
  33. package/lib/cjs/types/Bar.d.ts +4 -0
  34. package/lib/cjs/types/Bubble.d.ts +6 -1
  35. package/lib/cjs/types/Donut.d.ts +4 -0
  36. package/lib/cjs/types/ReferenceLine.d.ts +31 -0
  37. package/lib/cjs/types/index.d.ts +3 -0
  38. package/lib/es6/Area.js +68 -27
  39. package/lib/es6/Area.js.map +1 -1
  40. package/lib/es6/Bar.js +6 -4
  41. package/lib/es6/Bar.js.map +1 -1
  42. package/lib/es6/Bubble.js +1 -0
  43. package/lib/es6/Bubble.js.map +1 -1
  44. package/lib/es6/Donut.js +114 -44
  45. package/lib/es6/Donut.js.map +1 -1
  46. package/lib/es6/Dots.js +24 -23
  47. package/lib/es6/Dots.js.map +1 -1
  48. package/lib/es6/GroupBar.js +7 -7
  49. package/lib/es6/GroupBar.js.map +1 -1
  50. package/lib/es6/Line.js +4 -2
  51. package/lib/es6/Line.js.map +1 -1
  52. package/lib/es6/ReferenceLine.js +244 -0
  53. package/lib/es6/ReferenceLine.js.map +1 -0
  54. package/lib/es6/ResponsiveContainer.js +1 -1
  55. package/lib/es6/ResponsiveContainer.js.map +1 -1
  56. package/lib/es6/ScatterPlot.js +1 -0
  57. package/lib/es6/ScatterPlot.js.map +1 -1
  58. package/lib/es6/StackBar.js +6 -6
  59. package/lib/es6/StackedArea.js +7 -7
  60. package/lib/es6/Tooltip.js +3 -2
  61. package/lib/es6/Tooltip.js.map +1 -1
  62. package/lib/es6/createElement.js +3 -3
  63. package/lib/es6/createElement.js.map +1 -1
  64. package/lib/es6/index.js +1 -0
  65. package/lib/es6/index.js.map +1 -1
  66. package/lib/es6/style/dot.shadow.css +10 -5
  67. package/lib/es6/style/reference-line.shadow.css +37 -0
  68. package/lib/es6/types/Area.d.ts +1 -0
  69. package/lib/es6/types/Bar.d.ts +4 -0
  70. package/lib/es6/types/Bubble.d.ts +6 -1
  71. package/lib/es6/types/Donut.d.ts +4 -0
  72. package/lib/es6/types/ReferenceLine.d.ts +31 -0
  73. package/lib/es6/types/index.d.ts +3 -0
  74. package/lib/types/Area.d.ts +1 -0
  75. package/lib/types/Bar.d.ts +4 -0
  76. package/lib/types/Bubble.d.ts +6 -1
  77. package/lib/types/Donut.d.ts +4 -0
  78. package/lib/types/ReferenceLine.d.ts +31 -0
  79. package/lib/types/index.d.ts +3 -0
  80. package/package.json +11 -11
  81. package/src/Area.jsx +41 -8
  82. package/src/Bar.jsx +6 -5
  83. package/src/Bubble.jsx +1 -0
  84. package/src/Donut.jsx +96 -29
  85. package/src/Dots.jsx +11 -14
  86. package/src/GroupBar.jsx +1 -1
  87. package/src/Line.jsx +2 -1
  88. package/src/ReferenceLine.jsx +146 -0
  89. package/src/ResponsiveContainer.jsx +1 -1
  90. package/src/ScatterPlot.jsx +1 -0
  91. package/src/Tooltip.jsx +2 -3
  92. package/src/createElement.jsx +4 -2
  93. package/src/index.js +1 -0
  94. package/src/style/dot.shadow.css +10 -5
  95. package/src/style/reference-line.shadow.css +37 -0
  96. package/src/types/Area.d.ts +1 -0
  97. package/src/types/Bar.d.ts +4 -0
  98. package/src/types/Bubble.d.ts +6 -1
  99. package/src/types/Donut.d.ts +4 -0
  100. package/src/types/ReferenceLine.d.ts +31 -0
  101. package/src/types/index.d.ts +3 -0
package/src/Dots.jsx CHANGED
@@ -3,11 +3,12 @@ import React, { useCallback, useEffect, useState } from 'react';
3
3
  import { sstyled } from '@semcore/core';
4
4
  import { eventToPoint, invert } from './utils';
5
5
  import createElement from './createElement';
6
- import { FadeInOut } from '@semcore/animation';
7
6
  import trottle from '@semcore/utils/lib/rafTrottle';
8
7
 
9
8
  import style from './style/dot.shadow.css';
10
9
 
10
+ const EXCLUDE_PROPS = ['data', 'scale', 'value', 'display'];
11
+
11
12
  function Dots(props) {
12
13
  const {
13
14
  Element: SDot,
@@ -24,6 +25,7 @@ function Dots(props) {
24
25
  scale,
25
26
  duration = 500,
26
27
  } = props;
28
+ const SDots = 'g';
27
29
  const bisect = bisector((d) => d[x]).center;
28
30
  const [activeIndex, setActiveIndex] = useState(null);
29
31
 
@@ -59,27 +61,22 @@ function Dots(props) {
59
61
  unsubscribeMouseMoveRoot();
60
62
  unsubscribeMouseLeaveRoot();
61
63
  };
62
- }, [eventEmitter, data, x, y]);
63
-
64
- const renderCircle = useCallback(
65
- React.forwardRef((props, ref) => {
66
- return <FadeInOut ref={ref} tag="circle" {...props} />;
67
- }),
68
- [props],
69
- );
64
+ }, [eventEmitter, scale, data, x, y]);
70
65
 
71
- return data.reduce((acc, d, i) => {
66
+ const dots = data.reduce((acc, d, i) => {
72
67
  const isPrev = d3.defined()(data[i - 1] || {});
73
68
  const isNext = d3.defined()(data[i + 1] || {});
74
69
  const active = i === activeIndex;
70
+ const visible = display || i === activeIndex || (!isPrev && !isNext);
75
71
  if (!d3.defined()(d)) return acc;
72
+ if (!visible) return acc;
76
73
  acc.push(
77
74
  sstyled(styles)(
78
75
  <SDot
79
76
  key={i}
80
- render={renderCircle}
81
- visible={display || i === activeIndex || (!isPrev && !isNext)}
82
- __excludeProps={['data', 'scale', 'value', 'display']}
77
+ render="circle"
78
+ visible={visible}
79
+ __excludeProps={EXCLUDE_PROPS}
83
80
  value={d}
84
81
  index={i}
85
82
  cx={d3.x()(d)}
@@ -87,12 +84,12 @@ function Dots(props) {
87
84
  active={active}
88
85
  hide={hide}
89
86
  color={color}
90
- use:duration={`${duration}ms`}
91
87
  />,
92
88
  ),
93
89
  );
94
90
  return acc;
95
91
  }, []);
92
+ return sstyled(styles)(<SDots use:duration={`${duration}ms`}>{dots}</SDots>);
96
93
  }
97
94
 
98
95
  Dots.style = style;
package/src/GroupBar.jsx CHANGED
@@ -14,7 +14,7 @@ class GroupBarRoot extends Component {
14
14
 
15
15
  getScaleGroup() {
16
16
  const { Children, scale, scaleGroup, x } = this.asProps;
17
- // TODO: классный костыль =)
17
+ // TODO: love that hack (by lsroman)
18
18
  const xyScale = x ? scale[0] : scale[1];
19
19
 
20
20
  if (scaleGroup) return scaleGroup;
package/src/Line.jsx CHANGED
@@ -27,12 +27,13 @@ class LineRoot extends Component {
27
27
  };
28
28
 
29
29
  getDotsProps() {
30
- const { x, y, d3, color } = this.asProps;
30
+ const { x, y, d3, color, duration } = this.asProps;
31
31
  return {
32
32
  x,
33
33
  y,
34
34
  d3,
35
35
  color,
36
+ duration,
36
37
  };
37
38
  }
38
39
 
@@ -0,0 +1,146 @@
1
+ import React from 'react';
2
+ import { Component, sstyled } from '@semcore/core';
3
+ import createElement from './createElement';
4
+ import { scaleOfBandwidth } from './utils';
5
+
6
+ import style from './style/reference-line.shadow.css';
7
+
8
+ const MAP_ORIENTATION = {
9
+ left: 'vertical',
10
+ right: 'vertical',
11
+ top: 'horizontal',
12
+ bottom: 'horizontal',
13
+ };
14
+
15
+ const MAP_POSITION_LINE = {
16
+ vertical: ([xScale, yScale], value) => {
17
+ const yRange = yScale.range();
18
+ const x = scaleOfBandwidth(xScale, value);
19
+ return {
20
+ x: x,
21
+ y: yRange[1],
22
+ width: 0.1,
23
+ height: yRange[0] - yRange[1],
24
+ };
25
+ },
26
+ horizontal: ([xScale, yScale], value) => {
27
+ const xRange = xScale.range();
28
+ const y = scaleOfBandwidth(yScale, value);
29
+ return {
30
+ x: xRange[0],
31
+ y: y,
32
+ width: xRange[1] - xRange[0],
33
+ height: 0.1,
34
+ };
35
+ },
36
+ };
37
+
38
+ const OFFSET_TITLE = 10;
39
+ const MAP_POSITION_TITlE = {
40
+ left: ([xScale, yScale], value) => {
41
+ const yRange = yScale.range();
42
+ const x = scaleOfBandwidth(xScale, value);
43
+ return {
44
+ x: x - OFFSET_TITLE,
45
+ y: (yRange[0] + yRange[1]) / 2,
46
+ };
47
+ },
48
+ right: ([xScale, yScale], value) => {
49
+ const yRange = yScale.range();
50
+ const x = scaleOfBandwidth(xScale, value);
51
+ return {
52
+ x: x + OFFSET_TITLE,
53
+ y: (yRange[0] + yRange[1]) / 2,
54
+ };
55
+ },
56
+ top: ([xScale, yScale], value) => {
57
+ const xRange = xScale.range();
58
+ const y = scaleOfBandwidth(yScale, value);
59
+ return {
60
+ x: (xRange[1] + xRange[0]) / 2,
61
+ y: y - OFFSET_TITLE,
62
+ };
63
+ },
64
+ bottom: ([xScale, yScale], value) => {
65
+ const xRange = xScale.range();
66
+ const y = scaleOfBandwidth(yScale, value);
67
+ return {
68
+ x: (xRange[1] + xRange[0]) / 2,
69
+ y: y + OFFSET_TITLE,
70
+ };
71
+ },
72
+ };
73
+
74
+ class ReferenceLineRoot extends Component {
75
+ static displayName = 'ReferenceLine';
76
+ static style = style;
77
+ static defaultProps = {
78
+ position: 'left',
79
+ };
80
+
81
+ getTitleProps() {
82
+ const { position, value } = this.asProps;
83
+ return { position, value };
84
+ }
85
+
86
+ getBackgroundProps() {
87
+ const { position, value } = this.asProps;
88
+ return { position, value };
89
+ }
90
+
91
+ render() {
92
+ const SReferenceLine = this.Element;
93
+ const { title, scale, position, value, color, styles } = this.asProps;
94
+ const pos = MAP_POSITION_LINE[MAP_ORIENTATION[position]];
95
+
96
+ return sstyled(styles)(
97
+ <>
98
+ <SReferenceLine
99
+ render="rect"
100
+ __excludeProps={['data', 'scale', 'format', 'value', 'color']}
101
+ stroke={color}
102
+ {...pos(scale, value)}
103
+ />
104
+ {title && <ReferenceLine.Title>{title}</ReferenceLine.Title>}
105
+ </>,
106
+ );
107
+ }
108
+ }
109
+
110
+ function Title(props) {
111
+ const { Element: STitle, styles, scale, position, value } = props;
112
+ const { x, y } = MAP_POSITION_TITlE[position](scale, value);
113
+
114
+ const sstyles = sstyled(styles);
115
+ const sTitleStyles = sstyles.cn('STitle', {
116
+ 'transform-origin': `${x.toFixed(2)}px ${y.toFixed(2)}px`,
117
+ });
118
+
119
+ return sstyled(styles)(
120
+ <STitle
121
+ render="text"
122
+ childrenPosition="inside"
123
+ className={sTitleStyles.className}
124
+ style={sTitleStyles.style}
125
+ position={position}
126
+ x={x}
127
+ y={y}
128
+ />,
129
+ );
130
+ }
131
+
132
+ function Background(props) {
133
+ const { Element: SBackground, styles, scale, position, value } = props;
134
+ const pos = MAP_POSITION_LINE[MAP_ORIENTATION[position]];
135
+
136
+ return sstyled(styles)(
137
+ <SBackground render="rect" childrenPosition="inside" {...pos(scale, value)} />,
138
+ );
139
+ }
140
+
141
+ const ReferenceLine = createElement(ReferenceLineRoot, {
142
+ Title,
143
+ Background,
144
+ });
145
+
146
+ export default ReferenceLine;
@@ -61,7 +61,7 @@ class ResponsiveContainerRoot extends Component {
61
61
 
62
62
  componentDidMount() {
63
63
  if (this.$container) {
64
- // TODO ускорить можно?
64
+ // TODO: may be we can increase perfomance here? (by lsroman)
65
65
  this.observer.observe(this.$container);
66
66
  }
67
67
  }
@@ -66,6 +66,7 @@ class ScatterPlotRoot extends Component {
66
66
  >
67
67
  <SScatterPlot
68
68
  id={`${uid}${i}`}
69
+ index={i}
69
70
  render="circle"
70
71
  clipPath={`url(#${uid})`}
71
72
  cx={xScale(d[x]) + offset[0]}
package/src/Tooltip.jsx CHANGED
@@ -39,7 +39,6 @@ class TooltipRoot extends Component {
39
39
  (visible, data, node) => {
40
40
  this.setState(
41
41
  {
42
- // TODO: тут бага если одно поля нету,то оно не перезапишет стейт
43
42
  ...data,
44
43
  $visible: visible,
45
44
  },
@@ -124,8 +123,8 @@ function Dot(props) {
124
123
  const SDotGroup = Root;
125
124
  const SDot = Box;
126
125
  return sstyled(styles)(
127
- <SDotGroup render={Box}>
128
- <SDot __excludeProps={['data', 'scale']} color={resolveColor(color)} />
126
+ <SDotGroup render={Box} __excludeProps={['data', 'scale']}>
127
+ <SDot color={resolveColor(color)} />
129
128
  <Children />
130
129
  </SDotGroup>,
131
130
  );
@@ -7,10 +7,12 @@ import propsForElement from '@semcore/utils/lib/propsForElement';
7
7
  import getOriginChildren from '@semcore/utils/lib/getOriginChildren';
8
8
  import hoistNonReactStatics from 'hoist-non-react-statics';
9
9
 
10
+ const EXCLUDE_PROPS = ['data', 'scale'];
11
+
10
12
  function createElementRender() {
11
13
  const Element = React.forwardRef(function (
12
14
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
- { render, tag, childrenPosition = 'below', x: xS, y: yS, ...source },
15
+ { render, childrenPosition = 'below', x: xS, y: yS, ...source },
14
16
  ref,
15
17
  ) {
16
18
  const {
@@ -61,7 +63,7 @@ function createElementRender() {
61
63
  <Tag
62
64
  {...propsForElement(
63
65
  {
64
- __excludeProps: ['data', 'scale'],
66
+ __excludeProps: EXCLUDE_PROPS,
65
67
  ...mergedProps,
66
68
  },
67
69
  Tag,
package/src/index.js CHANGED
@@ -19,6 +19,7 @@ export { default as Venn } from './Venn';
19
19
  export { default as Tooltip } from './Tooltip';
20
20
 
21
21
  export { default as ResponsiveContainer } from './ResponsiveContainer';
22
+ export { default as ReferenceLine } from './ReferenceLine';
22
23
 
23
24
  export { HoverLine, HoverRect } from './Hover';
24
25
 
@@ -1,20 +1,21 @@
1
1
  @import '@semcore/utils/style/var.css';
2
2
  @import '@semcore/d3-chart/src/style/var.css';
3
3
 
4
+ SDots {
5
+ & SDot {
6
+ transition-duration: var(--duration);
7
+ }
8
+ }
9
+
4
10
  SDot {
5
11
  stroke-width: 2px;
6
12
  stroke: #fff;
7
13
  r: 6px;
8
14
  fill: var(--blue-03);
9
15
  transition-property: cx, cy;
10
- transition-duration: var(--duration);
11
16
  transition-timing-function: ease-in-out;
12
17
  }
13
18
 
14
- SDot[color] {
15
- fill: var(--color);
16
- }
17
-
18
19
  SDot[hide] {
19
20
  display: none;
20
21
  }
@@ -22,3 +23,7 @@ SDot[hide] {
22
23
  SDot[active] {
23
24
  r: 8px;
24
25
  }
26
+
27
+ SDot[color] {
28
+ fill: var(--color);
29
+ }
@@ -0,0 +1,37 @@
1
+ @import '@semcore/utils/style/var.css';
2
+
3
+ SReferenceLine {
4
+ fill: none;
5
+ stroke: var(--gray-300);
6
+ }
7
+
8
+ STitle {
9
+ font-size: var(--fs-100);
10
+ fill: var(--gray-500);
11
+ transform-origin: var(--transform-origin);
12
+ }
13
+
14
+ STitle[position='top'] {
15
+ text-anchor: middle;
16
+ }
17
+
18
+ STitle[position='bottom'] {
19
+ text-anchor: middle;
20
+ alignment-baseline: hanging;
21
+ }
22
+
23
+ STitle[position='right'] {
24
+ transform: rotate(-90deg);
25
+ alignment-baseline: middle;
26
+ text-anchor: middle;
27
+ }
28
+
29
+ STitle[position='left'] {
30
+ transform: rotate(-90deg);
31
+ text-anchor: middle;
32
+ alignment-baseline: middle;
33
+ }
34
+
35
+ SBackground {
36
+ fill: color-mod(var(--gray-200) a(20%));
37
+ }
@@ -42,6 +42,7 @@ export interface IAreaNullProps extends IContext {
42
42
  declare const Area: (<T>(props: CProps<IAreaProps & T>) => ReturnEl) & {
43
43
  Dots: <T>(props: CProps<IAreaDotsProps & T, IAreaDotsContext>) => ReturnEl;
44
44
  Null: <T>(props: CProps<IAreaNullProps & T>) => ReturnEl;
45
+ Line: <T>(props: CProps<IContext & T>) => ReturnEl;
45
46
  };
46
47
 
47
48
  export default Area;
@@ -19,6 +19,10 @@ export interface IBarProps extends IContext {
19
19
  * @default 2
20
20
  */
21
21
  r?: number | number[];
22
+ /** Minimal height
23
+ * @default 4
24
+ */
25
+ hMin?: number;
22
26
  }
23
27
 
24
28
  export interface IBarContext {
@@ -22,6 +22,11 @@ export interface IBubbleProps extends IContext {
22
22
  duration?: number;
23
23
  }
24
24
 
25
- declare const Bubble: <T>(props: CProps<IBubbleProps & T>) => ReturnEl;
25
+ export interface IBubbleContext {
26
+ /** Index element of data */
27
+ index: number;
28
+ }
29
+
30
+ declare const Bubble: <T>(props: CProps<IBubbleProps & T, IBubbleContext>) => ReturnEl;
26
31
 
27
32
  export default Bubble;
@@ -27,6 +27,10 @@ export interface IPieProps extends IContext {
27
27
  @default #50aef4
28
28
  **/
29
29
  color?: string;
30
+ /**
31
+ * Active sector
32
+ * */
33
+ active?: boolean;
30
34
  }
31
35
 
32
36
  export interface IEmptyDataProps extends IContext {}
@@ -0,0 +1,31 @@
1
+ import { CProps, ReturnEl } from '@semcore/core';
2
+ import IContext from './context';
3
+
4
+ export interface IReferenceLineProps extends IContext {
5
+ /** The position of the title relative reference line
6
+ * @default 'left' */
7
+ position?: 'top' | 'right' | 'bottom' | 'left';
8
+ /** Value element of data */
9
+ value: any;
10
+ }
11
+
12
+ export interface IReferenceLineTitleProps extends IContext {
13
+ /** The position of the axis relative reference line */
14
+ position?: 'top' | 'right' | 'bottom' | 'left';
15
+ /** Value element of data */
16
+ value: any;
17
+ }
18
+
19
+ export interface IReferenceLineBackgroundProps extends IContext {
20
+ /** The position of the axis relative reference line */
21
+ position?: 'top' | 'right' | 'bottom' | 'left';
22
+ /** Value element of data */
23
+ value: any;
24
+ }
25
+
26
+ declare const ReferenceLine: (<T>(props: CProps<IReferenceLineProps & T>) => ReturnEl) & {
27
+ Title: <T>(props: CProps<IReferenceLineTitleProps & T>) => ReturnEl;
28
+ Background: <T>(props: CProps<IReferenceLineBackgroundProps & T>) => ReturnEl;
29
+ };
30
+
31
+ export default ReferenceLine;
@@ -51,3 +51,6 @@ export * from './ScatterPlot';
51
51
 
52
52
  export { default as Bubble } from './Bubble';
53
53
  export * from './Bubble';
54
+
55
+ export { default as ReferenceLine } from './ReferenceLine';
56
+ export * from './ReferenceLine';