@easyv/charts 1.4.34 → 1.4.36

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.
@@ -2,50 +2,94 @@ import React, { forwardRef, memo, useRef } from "react";
2
2
  import { scaleLinear } from "d3-scale";
3
3
 
4
4
  export default memo(forwardRef((props, ref) => {
5
- const { props: { control, axes, series, xLineRange, yLineRange, marginTop, axisX, bandLength } } = props;
6
- const _axisX = { ...axisX, scaler: axisX.controlDragScaler }
5
+ const {
6
+ actions:{ setX, setWorking, setControlInfo },
7
+ props: {
8
+ control:{ height, color, drag:{ color:dragColor, width:dragWidth }, margin:{ left, right } }, axes, series, width, top, bandLength
9
+ }
10
+ } = props;
11
+ const barWidth = width-left-right;
12
+ const xAxis = axes.get("x");
13
+ const barRef = useRef();
14
+
15
+ const barStyle={
16
+ position:"absolute",
17
+ top:0,
18
+ width:barWidth*dragWidth/100,
19
+ display:"flex",
20
+ justifyContent:"space-between",
21
+ background:dragColor
22
+ }
23
+ const dragStyle={
24
+ height,
25
+ flex:1,
26
+ cursor:"move"
27
+ }
28
+ const controllerStyle={
29
+ position:"relative",
30
+ width:12,
31
+ height,
32
+ padding:"0 5px",
33
+ cursor:"col-resize",
34
+ background:"rgb(2, 176, 249)",
35
+ WebkitBackgroundClip:"content-box",
36
+ }
37
+ const down=()=>{
38
+ const transform = ref.current.style.transform;
39
+ let movementX = 0;
40
+ let rawTranslateX = parseFloat(transform?transform.match(/\d+\.*\d*/)[0]:0);
41
+ const mouseMoveHandle = (e) => {
42
+ //当前位移的距离
43
+ movementX += e.movementX;
44
+ setX(movementX + rawTranslateX,true);
45
+ };
46
+ const mouseUpHandle = (e) => {
47
+ setWorking(false);
48
+ document.removeEventListener("mousemove", mouseMoveHandle);
49
+ document.removeEventListener("mouseup", mouseUpHandle);
50
+ };
51
+ document.addEventListener("mousemove", mouseMoveHandle);
52
+ document.addEventListener("mouseup", mouseUpHandle);
53
+ setWorking(true);
54
+ }
7
55
  return (
8
- <>
9
- <foreignObject
10
- style={{
11
- width: xLineRange,
12
- height: control.height,
13
- backgroundColor: control.color,
14
- // padding: ` 0 ${control.margin.left}px 0 ${control.margin.right}px`,
15
- transform: `translateY(${yLineRange + marginTop}px)`,
16
- }}
17
- >
18
- <svg width="100%" height="100%">
19
- {series.map(({ Component, yOrZ, ...config }, index) => {
20
- const yAxis = axes.get(yOrZ);
21
- const cloneYAxis = JSON.parse(JSON.stringify(yAxis));
22
- //todo range
23
- cloneYAxis.scaler = scaleLinear()
24
- .domain(yAxis.domain)
25
- .range([control.height, 0]);
26
- return (
27
- yAxis &&
28
- Component && (
29
- <Component
30
- key={index}
31
- {...config}
32
- bandLength={bandLength}
33
- xAxis={_axisX}
34
- yAxis={cloneYAxis}
35
- />
36
- )
37
- );
38
- })}
39
- {/* 控制条 */}
40
- <rect
41
- ref={ref}
42
- height={control.height}
43
- width={(xLineRange * control.drag.width) / 100}
44
- fill={control.drag.color}
45
- ></rect>
46
- </svg>
47
- </foreignObject>
48
- </>
56
+ <div
57
+ style={{
58
+ width: barWidth,
59
+ height: height,
60
+ backgroundColor: color,
61
+ transform: `translate(${left}px,${top}px)`,
62
+ }}
63
+ >
64
+ <svg width="100%" height="100%">
65
+ {series.map(({ Component, yOrZ, ...config }, index) => {
66
+ const yAxis = axes.get(yOrZ);
67
+ const cloneYAxis = JSON.parse(JSON.stringify(yAxis));
68
+ //todo range
69
+ cloneYAxis.scaler = scaleLinear()
70
+ .domain(yAxis.domain)
71
+ .range([height, 0]);
72
+ return (
73
+ yAxis &&
74
+ Component && (
75
+ <Component
76
+ key={index}
77
+ {...config}
78
+ bandLength={bandLength}
79
+ xAxis={xAxis}
80
+ yAxis={cloneYAxis}
81
+ />
82
+ )
83
+ );
84
+ })}
85
+ </svg>
86
+ {/* 控制条 */}
87
+ <div ref={ref} style={barStyle}>
88
+ {/* <div style={{...controllerStyle,transform:"translateX(-6px)"}}></div> */}
89
+ <div style={dragStyle} onMouseDown={down} ref={barRef}></div>
90
+ {/* <div style={{...controllerStyle,transform:`translateX(6px)`}}></div> */}
91
+ </div>
92
+ </div>
49
93
  );
50
94
  }))
51
95
 
@@ -10,15 +10,12 @@ export default ({
10
10
  y = 0,
11
11
  isControlChart = false,
12
12
  xName = null,
13
- setControlChartTooltipShow = null,
14
- setControlChartTooltipXName = null,
15
- setControlChartTooltipX = null,
16
- setControlChartIndicatorList = null,
17
- controlChartIndicatorList = null,
13
+ ctlIndicatorList,
14
+ setCtlTip
18
15
  }) => {
19
16
  let isShow = true;
20
- if (isControlChart && controlChartIndicatorList) {
21
- isShow = controlChartIndicatorList.find(
17
+ if (isControlChart && ctlIndicatorList) {
18
+ isShow = ctlIndicatorList.find(
22
19
  (item) => item.tick === xName
23
20
  ).isShow;
24
21
  }
@@ -29,28 +26,28 @@ export default ({
29
26
  x={x}
30
27
  onMouseEnter={() => {
31
28
  if (isControlChart) {
32
- setControlChartIndicatorList((v) => {
33
- return v.map((item) => {
29
+ setCtlTip((pre)=>({
30
+ show:true, x:x+width/2, xName,
31
+ indicatorList:pre.indicatorList.map((item)=>{
34
32
  if (item.tick === xName) {
35
33
  return { ...item, isShow: true };
36
34
  } else {
37
35
  return item;
38
36
  }
39
- });
40
- });
41
- setControlChartTooltipShow(true);
42
- setControlChartTooltipXName(xName);
43
- setControlChartTooltipX(x);
37
+ })
38
+ }))
44
39
  }
45
40
  }}
46
41
  onMouseLeave={() => {
47
42
  if (isControlChart) {
48
- setControlChartIndicatorList((v) =>
49
- v.map((item) => ({ ...item, isShow: false }))
50
- );
51
- setControlChartTooltipShow(false);
52
- setControlChartTooltipXName(undefined);
53
- setControlChartTooltipX(undefined);
43
+ setCtlTip((pre)=>({
44
+ show:false,
45
+ x:undefined,
46
+ xName:undefined,
47
+ indicatorList:pre.indicatorList.map((item)=>{
48
+ return { ...item, isShow:false }
49
+ })
50
+ }))
54
51
  }
55
52
  }}
56
53
  fill={isShow ? color : "transparent"}
@@ -18,14 +18,7 @@ export default memo(
18
18
  data,
19
19
  xAxis: { scaler: xScaler, step, direction },
20
20
  yAxis: { scaler: yScaler, isClipAxis, clipValue },
21
- triggerClick,
22
- //控制图相关
23
- indicatorWidth,
24
- setControlChartTooltipShow,
25
- setControlChartTooltipX,
26
- setControlChartTooltipXName,
27
- isControlChart = false,
28
- setControlChartIndicatorList = null,
21
+ triggerClick
29
22
  }) => {
30
23
  const lineType = config.hasOwnProperty("line"); // 堆叠处理
31
24
  const showIcon = icon && icon.show;
@@ -104,32 +97,6 @@ export default memo(
104
97
  <g
105
98
  key={i}
106
99
  onClick={triggerClick}
107
- onMouseEnter={() => {
108
- if (isControlChart) {
109
- setControlChartIndicatorList((v) => {
110
- return v.map((item) => {
111
- if (item.tick === data.x) {
112
- return { ...item, isShow: true };
113
- } else {
114
- return item;
115
- }
116
- });
117
- });
118
- setControlChartTooltipShow(true);
119
- setControlChartTooltipX(xScaler(x) - indicatorWidth / 2);
120
- setControlChartTooltipXName(data.x);
121
- }
122
- }}
123
- onMouseLeave={() => {
124
- if (isControlChart) {
125
- setControlChartIndicatorList((v) =>
126
- v.map((item) => ({ ...item, isShow: false }))
127
- );
128
- setControlChartTooltipShow(false);
129
- setControlChartTooltipXName(undefined);
130
- setControlChartTooltipX(undefined);
131
- }
132
- }}
133
100
  style={{ cursor: "pointer" }}
134
101
  data-data={JSON.stringify(data)}
135
102
  >
@@ -41,7 +41,6 @@ export default memo(
41
41
  ...translateTip,
42
42
  x: translateTip.x + position + marginLeft,
43
43
  };
44
-
45
44
  if (translate3d.x + tipWidth > width) {
46
45
  const newPositon = position + marginLeft - tipWidth - translateTip.x;
47
46
  translate3d.x = newPositon >= 0 ? newPositon : 0;
@@ -60,10 +60,12 @@ const scales = {
60
60
 
61
61
  export default ({
62
62
  axes,
63
- context: { width, height }, //是否为控制图
64
- isControlChart,
65
- control: controlConfig,
63
+ context: { width:chartWidth, height:chartHeight },
64
+ controlInfo, //是否为控制图
66
65
  }) => {
66
+ const { isC, cHeight, cWidth, cPercent } = controlInfo;
67
+ const width = chartWidth;
68
+ const height = chartHeight - cHeight;
67
69
  const _axes = useMemo(() => {
68
70
  const tmp = new Map();
69
71
  const xAxisPositions = [];
@@ -91,9 +93,7 @@ export default ({
91
93
  bottomClipAxisStep,
92
94
  topClipAxisStep,
93
95
  clipValue,
94
- clipPosition,
95
- clipDifferenceValue,
96
- clipAxisMode,
96
+ clipPosition
97
97
  } = item;
98
98
  //如果是断轴类型,输出一套完全不同的values
99
99
 
@@ -103,7 +103,7 @@ export default ({
103
103
  * @param {*} width
104
104
  * @param {*} height
105
105
  * @param {*} paddingOuter
106
- * @returns {start,end,direction}
106
+ * @returns { start, end, direction }
107
107
  */
108
108
  function getChartsConfig(orientation, width, height, paddingOuter) {
109
109
  const direction =
@@ -321,10 +321,8 @@ export default ({
321
321
  });
322
322
  } else {
323
323
  //计算真正需要的tickCount,如果domain区间太小,不能完全按照count来,需要减少count数
324
- const tickCount =
325
- type == "ordinal" ? _count : getTickCount(domain, _count, decimal);
326
- const { start, end, direction, _paddingOuter, length } =
327
- getChartsConfig(orientation, width, height, paddingOuter);
324
+ const tickCount = type == "ordinal" ? _count : getTickCount(domain, _count, decimal);
325
+ const { start, end, direction, _paddingOuter, length } = getChartsConfig(orientation, width, height, paddingOuter);
328
326
 
329
327
  let range =
330
328
  direction === "horizontal"
@@ -336,8 +334,7 @@ export default ({
336
334
  if (type !== "ordinal" && !isNaN(domain[1]) && !auto) {
337
335
  newDomain = getNewDomain(domain, mode, _step);
338
336
  }
339
-
340
- const scaler = scales[type]().domain(newDomain).range(range);
337
+ let scaler = scales[type]().domain(newDomain).range(range);
341
338
 
342
339
  scaler.type = type;
343
340
  if (type !== "ordinal") scaler.clamp(true); //scaler.nice().clamp(true)
@@ -347,9 +344,9 @@ export default ({
347
344
  ? scaler.ticks(tickCount)
348
345
  : scaler.domain();
349
346
  let _ticks = allTicks;
350
- //
347
+
351
348
  if (type === "ordinal") {
352
- if (carousel === false) {
349
+ if (carousel === false || isC) {
353
350
  _ticks = getTicksOfAxis(_ticks, +tickCount, showLast);
354
351
  }
355
352
  } else {
@@ -381,6 +378,25 @@ export default ({
381
378
  }
382
379
  }
383
380
  }
381
+ let step = lengthWithoutPaddingOuter / allTicks.length;
382
+ const controlCfg = {
383
+ controlStep:0,
384
+ controlDragScaler:null
385
+ }
386
+ if(isC){
387
+ controlCfg.controlStep = step / cPercent;
388
+ controlCfg.controlDragScaler = scaler.copy().range([start/cPercent, end/cPercent]);
389
+ const { start:start_, end:end_, direction:direction_, _paddingOuter:outer, length:len } = getChartsConfig(orientation, cWidth, height, paddingOuter);
390
+ let range = direction_ === "horizontal"
391
+ ? [start_, end_]
392
+ : direction_ === "vertical"
393
+ ? [end_, start_]
394
+ : [0, 0];
395
+ scaler = scales[type]().domain(newDomain).range(range);
396
+ scaler.type = type;
397
+ const controlOuter = len - outer;
398
+ step = controlOuter/allTicks.length;
399
+ }
384
400
  tmp.set(axisType, {
385
401
  ...item,
386
402
  count: _count,
@@ -393,8 +409,10 @@ export default ({
393
409
  start,
394
410
  end,
395
411
  lengthWithoutPaddingOuter,
396
- step: lengthWithoutPaddingOuter / allTicks.length,
412
+ step,
413
+ ...controlCfg,
397
414
  allTicks,
415
+ rawTicks: _ticks,
398
416
  ticks: _ticks,
399
417
  });
400
418
  }
@@ -17,14 +17,14 @@ export default (
17
17
  axis,
18
18
  config,
19
19
  isHover,
20
- isControlChart = false,
21
- controlConfig = null
20
+ controlInfo
22
21
  ) => {
23
22
  const { show, interval, duration, hover } = config;
23
+ const { isC, cPercent } = controlInfo;
24
24
  const time = duration + interval;
25
25
 
26
26
  const {
27
- tickCount,
27
+ tickCount:count,
28
28
  allTicks,
29
29
  scaler,
30
30
  start,
@@ -34,12 +34,11 @@ export default (
34
34
  lengthWithoutPaddingOuter,
35
35
  } = axis;
36
36
  const tickLength = ticks.length;
37
+ const tickCount = isC?allTicks.length:count;
37
38
 
38
- const _end =
39
- isControlChart && controlConfig
40
- ? end / (controlConfig.drag.width / 100)
41
- : end;
42
- const controlDragScaler = scaler.copy().range([start, end]);
39
+ const scale = isC?cPercent:1;
40
+ const _start = start/scale;
41
+ const _end = end/scale;
43
42
 
44
43
  const [state, setState] = useState({
45
44
  scaler,
@@ -86,7 +85,7 @@ export default (
86
85
  setState({
87
86
  step,
88
87
  ticks: _ticks,
89
- scaler: scaler.copy().domain(_ticks).range([start, _end]),
88
+ scaler: scaler.copy().domain(_ticks).range([_start, _end]),
90
89
  });
91
90
  } else {
92
91
  animation = animate({
@@ -107,7 +106,7 @@ export default (
107
106
  ticks: _ticks,
108
107
  scaler: scaler
109
108
  .copy()
110
- .range([start, _end + step])
109
+ .range([_start, _end + step])
111
110
  .domain(_ticks),
112
111
  };
113
112
  });
@@ -119,7 +118,7 @@ export default (
119
118
  ...axis,
120
119
  scaler: scaler
121
120
  .copy()
122
- .range([start + step * v, _end + step + step * v]),
121
+ .range([_start + step * v, _end + step + step * v]),
123
122
  };
124
123
  });
125
124
  },
@@ -130,7 +129,7 @@ export default (
130
129
  return {
131
130
  ...axis,
132
131
  ticks: _ticks,
133
- scaler: scaler.copy().range([start, _end]).domain(_ticks),
132
+ scaler: scaler.copy().range([_start, _end]).domain(_ticks),
134
133
  };
135
134
  });
136
135
  },
@@ -140,7 +139,7 @@ export default (
140
139
  const _ticks = scaler.type == "linear" ? scaler.domain() : allTicks;
141
140
  setState({
142
141
  step,
143
- scaler: scaler.copy().domain(_ticks).range([start, _end]),
142
+ scaler: scaler.copy().domain(_ticks).range([_start, _end]),
144
143
  ticks,
145
144
  });
146
145
  }
@@ -151,7 +150,7 @@ export default (
151
150
  tickCount,
152
151
  allTicks,
153
152
  scaler,
154
- start,
153
+ _start,
155
154
  _end,
156
155
  step,
157
156
  ticks,
@@ -163,10 +162,7 @@ export default (
163
162
  return {
164
163
  ...axis,
165
164
  ...state,
166
- rawTicks: axis.ticks,
167
- controlDragScaler,
168
- controlEnd: _end,
169
- isControlChart,
165
+ controlEnd: _end
170
166
  };
171
167
  };
172
168
 
@@ -53,7 +53,13 @@ export default ({ data, series }) => {
53
53
  }, []);
54
54
 
55
55
  useEffect(() => {
56
- setSelected(getInitialSelected(series));
56
+ const map = getInitialSelected(series);
57
+ for(let [ key ] of map.entries()){
58
+ if(selected.has(key)){
59
+ map.set(key, selected.get(key));
60
+ }
61
+ }
62
+ setSelected(map);
57
63
  }, [series]);
58
64
 
59
65
  return [