@easyv/charts 1.3.2 → 1.3.5

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.
@@ -64,7 +64,6 @@ var Chart = /*#__PURE__*/(0, _react.memo)(function (_ref2) {
64
64
  var chartWidth = width - marginLeft - marginRight;
65
65
  var chartHeight = height - marginTop - marginBottom;
66
66
  var triggerOnRelative = (0, _react.useCallback)(function (data) {
67
- console.log("trigger");
68
67
  if (!interaction) return;
69
68
  var callbacks = interaction.callbacks,
70
69
  remoteControls = interaction.remoteControls;
@@ -49,18 +49,17 @@ var _default = /*#__PURE__*/(0, _react.memo)(function (_ref) {
49
49
 
50
50
  var _useContext = (0, _react.useContext)(_context.chartContext),
51
51
  width = _useContext.width,
52
- height = _useContext.height;
53
-
54
- var _getSeriesInfo = (0, _utils.getSeriesInfo)({
55
- step: step,
56
- bandLength: bandLength,
57
- paddingInner: paddingInner,
58
- paddingOuter: paddingOuter
59
- }),
60
- seriesStep = _getSeriesInfo.seriesStep,
61
- seriesWidth = _getSeriesInfo.seriesWidth,
62
- seriesStart = _getSeriesInfo.seriesStart;
52
+ height = _useContext.height; // const { seriesStep, seriesWidth, seriesStart } = getSeriesInfo({
53
+ // step,
54
+ // bandLength,
55
+ // paddingInner,
56
+ // paddingOuter,
57
+ // });
63
58
 
59
+
60
+ var seriesStep = (1 + paddingInner) * step,
61
+ seriesWidth = step,
62
+ seriesStart = paddingOuter * step;
64
63
  var isVertical = direction === 'vertical';
65
64
  var _position = label.position;
66
65
  return /*#__PURE__*/React.createElement("g", {
@@ -27,66 +27,81 @@ var _default = /*#__PURE__*/(0, _react.memo)(function (props) {
27
27
  _props$speed = props.speed,
28
28
  speed = _props$speed === void 0 ? 5 : _props$speed;
29
29
  var dom = (0, _react.useRef)(null);
30
- var content = (0, _react.useRef)(null);
30
+ var target = (0, _react.useRef)(null);
31
+ var observe = (0, _react.useRef)(null);
32
+ var speed_ = (0, _react.useRef)(0); //这里必须用一个ref绑定speed,否则animation中获取不到最新的speed
33
+
34
+ var timer = (0, _react.useRef)();
31
35
 
32
36
  var _useState = (0, _react.useState)(false),
33
37
  _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
34
- fresh = _useState2[0],
35
- setFresh = _useState2[1];
36
-
37
- var textList = (0, _react.useRef)([value]);
38
- var duration = (0, _react.useRef)(10 / speed);
39
- (0, _react.useEffect)(function () {
40
- if (dom.current && content.current) {
41
- var domRectWidth = dom.current.getBoundingClientRect().width;
42
- var contentRectWidth = content.current.getBoundingClientRect().width;
43
-
44
- if (domRectWidth < contentRectWidth) {
45
- textList.current = [value, value];
46
- duration.current = contentRectWidth / domRectWidth / speed * 10;
47
- setFresh(!fresh);
48
- }
49
- }
50
- }, [value, speed]); //在head标签中注入keyframes动画
51
-
52
- (0, _react.useEffect)(function () {
53
- var md_marquee = document.getElementById('md_marquee'); //先查找是否已经存在md_marquee
54
-
55
- if (!md_marquee) {
56
- //不存在时再注入style标签
57
- var keyframes = "@keyframes md_marquee {0%{\n transform: translate(0,0);\n }\n 100%{\n transform: translate(-100%,0);\n }}";
38
+ overflow = _useState2[0],
39
+ setOverflow = _useState2[1];
58
40
 
59
- var _style = document.createElement('style');
41
+ speed_.current = speed;
60
42
 
61
- _style.id = 'md_marquee';
62
- _style.innerHTML = keyframes; // 将style样式存放到head标签
43
+ var animation = function animation(timestamp) {
44
+ var frame = Math.floor(timestamp * speed_.current) % 10000 / 100;
45
+ target.current.style.transform = "translate(-".concat(frame, "%,0px)");
46
+ target.current.nextSibling.style.transform = "translate(-".concat(frame, "%,0px)");
47
+ timer.current = requestAnimationFrame(animation);
48
+ };
63
49
 
64
- document.getElementsByTagName('head')[0].appendChild(_style);
65
- }
50
+ (0, _react.useEffect)(function () {
51
+ //初始化观察器,利用观察器来监视组件可视区域变化
52
+ observe.current = new IntersectionObserver(function (entries) {
53
+ var entrie = entries[0];
54
+
55
+ if (entrie.boundingClientRect.width < entrie.rootBounds.width >= 1) {
56
+ //表示文字全部可视
57
+ cancelAnimationFrame(timer.current);
58
+ target.current.style.transform = "translate(0px,0px)"; //重置偏移
59
+
60
+ setOverflow(false);
61
+ return;
62
+ } else {
63
+ //否则文本溢出
64
+ if (!overflow) {
65
+ cancelAnimationFrame(timer.current);
66
+ timer.current = requestAnimationFrame(animation);
67
+ setOverflow(true);
68
+ }
69
+ }
70
+ }, {
71
+ root: dom.current,
72
+ rootMargin: "0px 0px 0px 0px",
73
+ threshold: new Array(101).fill(0).map(function (d, i) {
74
+ return i / 100;
75
+ }) //这里设置了[0-1]之间所有的阈值,保证每一帧的变化都能被观察到
76
+
77
+ }); // start observing
78
+
79
+ observe.current.observe(target.current);
80
+ return function () {
81
+ cancelAnimationFrame(timer.current);
82
+ observe.current.unobserve(target.current);
83
+ observe.current.disconnect();
84
+ };
66
85
  }, []);
67
- var overflow = textList.current.length == 2;
68
- return /*#__PURE__*/React.createElement("span", {
86
+ var textList = [value, value];
87
+ return /*#__PURE__*/React.createElement("div", {
69
88
  style: _objectSpread(_objectSpread({
70
- width: '100%'
89
+ width: "100%"
71
90
  }, style), {}, {
72
- display: 'flex',
73
- whiteSpace: 'nowrap',
74
- overflow: 'hidden',
91
+ display: "flex",
92
+ whiteSpace: "nowrap",
93
+ overflow: "hidden",
75
94
  justifyContent: overflow ? "start" : style.justifyContent || ""
76
95
  }),
77
96
  ref: dom
78
- }, textList.current.map(function (item, itemIndex) {
97
+ }, textList.map(function (item, i) {
79
98
  return /*#__PURE__*/React.createElement("span", {
80
- key: itemIndex,
81
- style: overflow ? {
82
- //如果文本数量为2,则需要滚动
83
- animationName: 'md_marquee',
84
- animationTimingFunction: 'linear',
85
- animationIterationCount: 'infinite',
86
- animationDuration: duration.current + 's'
87
- } : {},
88
- ref: itemIndex == 0 ? content : null
89
- }, "\xA0", item);
99
+ key: i,
100
+ ref: i == 0 ? target : null,
101
+ style: {
102
+ display: i == 1 && !overflow ? "none" : "inline"
103
+ }
104
+ }, item, "\xA0");
90
105
  }));
91
106
  });
92
107
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyv/charts",
3
- "version": "1.3.2",
3
+ "version": "1.3.5",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  "@babel/preset-env": "^7.17.10",
20
20
  "@babel/preset-react": "^7.16.7",
21
21
  "@babel/preset-typescript": "^7.16.7",
22
- "@commitlint/cli": "^16.2.4",
22
+ "@commitlint/cli": "^17.0.3",
23
23
  "@commitlint/config-conventional": "^16.2.4",
24
24
  "@types/d3": "^7.0.0",
25
25
  "@types/react": "^18.0.12",
@@ -26,13 +26,16 @@ export default memo(
26
26
 
27
27
  if (!(data.length && (showIcon || showLabel))) return null;
28
28
  const { width, height } = useContext(chartContext);
29
- const { seriesStep, seriesWidth, seriesStart } = getSeriesInfo({
30
- step,
31
- bandLength,
32
- paddingInner,
33
- paddingOuter,
34
- });
35
29
 
30
+ // const { seriesStep, seriesWidth, seriesStart } = getSeriesInfo({
31
+ // step,
32
+ // bandLength,
33
+ // paddingInner,
34
+ // paddingOuter,
35
+ // });
36
+
37
+ const seriesStep = (1 + paddingInner) * step,
38
+ seriesWidth = step, seriesStart = paddingOuter * step;
36
39
  const isVertical = direction === 'vertical';
37
40
  const _position = label.position;
38
41
  return (
@@ -1,4 +1,4 @@
1
- import { useEffect, memo, useRef, useState } from 'react';
1
+ import { useEffect, memo, useRef, useState, useCallback } from "react";
2
2
 
3
3
  /**
4
4
  * 文字跑马灯组件
@@ -13,76 +13,80 @@ export default memo((props) => {
13
13
  } = props;
14
14
 
15
15
  const dom = useRef(null);
16
- const content = useRef(null);
16
+ const target = useRef(null);
17
+ const observe = useRef(null);
18
+ const speed_ = useRef(0); //这里必须用一个ref绑定speed,否则animation中获取不到最新的speed
19
+ const timer = useRef();
20
+ const [overflow, setOverflow] = useState(false);
21
+ speed_.current = speed;
17
22
 
18
- const [fresh,setFresh] = useState(false);
19
- let textList = useRef([value]);
20
- let duration = useRef(10/speed);
23
+ const animation = (timestamp) => {
24
+ let frame = (Math.floor(timestamp * speed_.current) % 10000) / 100;
25
+ target.current.style.transform = `translate(-${frame}%,0px)`;
26
+ target.current.nextSibling.style.transform = `translate(-${frame}%,0px)`;
27
+ timer.current = requestAnimationFrame(animation);
28
+ };
21
29
 
22
30
  useEffect(() => {
23
- if(dom.current && content.current){
24
- let domRectWidth = dom.current.getBoundingClientRect().width;
25
- let contentRectWidth = content.current.getBoundingClientRect().width;
26
- if (domRectWidth < contentRectWidth) {
27
- textList.current = [value,value];
28
- duration.current = contentRectWidth / domRectWidth / speed * 10;
29
- setFresh(!fresh);
31
+ //初始化观察器,利用观察器来监视组件可视区域变化
32
+ observe.current = new IntersectionObserver(
33
+ function (entries) {
34
+ let entrie = entries[0];
35
+
36
+ if (entrie.boundingClientRect.width < entrie.rootBounds.width >= 1) {
37
+ //表示文字全部可视
38
+ cancelAnimationFrame(timer.current);
39
+ target.current.style.transform = "translate(0px,0px)"; //重置偏移
40
+ setOverflow(false);
41
+ return;
42
+ } else {
43
+ //否则文本溢出
44
+ if (!overflow) {
45
+ cancelAnimationFrame(timer.current);
46
+ timer.current = requestAnimationFrame(animation);
47
+ setOverflow(true);
48
+ }
49
+ }
50
+ },
51
+ {
52
+ root: dom.current,
53
+ rootMargin: "0px 0px 0px 0px",
54
+ threshold: new Array(101).fill(0).map((d, i) => i / 100), //这里设置了[0-1]之间所有的阈值,保证每一帧的变化都能被观察到
30
55
  }
31
- }
32
- }, [value,speed]);
33
-
34
- //在head标签中注入keyframes动画
35
- useEffect(() => {
36
- let md_marquee = document.getElementById('md_marquee'); //先查找是否已经存在md_marquee
37
- if (!md_marquee) {
38
- //不存在时再注入style标签
39
- const keyframes = `@keyframes md_marquee {0%{
40
- transform: translate(0,0);
41
- }
42
- 100%{
43
- transform: translate(-100%,0);
44
- }}`;
45
- const style = document.createElement('style');
46
- style.id = 'md_marquee';
47
- style.innerHTML = keyframes;
48
- // 将style样式存放到head标签
49
- document.getElementsByTagName('head')[0].appendChild(style);
50
- }
56
+ );
57
+ // start observing
58
+ observe.current.observe(target.current);
59
+ return () => {
60
+ cancelAnimationFrame(timer.current);
61
+ observe.current.unobserve(target.current);
62
+ observe.current.disconnect();
63
+ };
51
64
  }, []);
52
- let overflow = textList.current.length==2;
65
+
66
+ let textList = [value, value];
53
67
  return (
54
- <span
68
+ <div
55
69
  style={{
56
- width: '100%',
70
+ width: "100%",
57
71
  ...style,
58
- display: 'flex',
59
- whiteSpace: 'nowrap',
60
- overflow: 'hidden',
61
- justifyContent:overflow?"start":(style.justifyContent || "")
72
+ display: "flex",
73
+ whiteSpace: "nowrap",
74
+ overflow: "hidden",
75
+ justifyContent: overflow ? "start" : style.justifyContent || "",
62
76
  }}
63
77
  ref={dom}
64
78
  >
65
- {textList.current.map((item, itemIndex) => {
79
+ {textList.map((item, i) => {
66
80
  return (
67
81
  <span
68
- key={itemIndex}
69
- style={
70
- overflow
71
- ? {
72
- //如果文本数量为2,则需要滚动
73
- animationName: 'md_marquee',
74
- animationTimingFunction: 'linear',
75
- animationIterationCount: 'infinite',
76
- animationDuration: duration.current + 's',
77
- }
78
- : {}
79
- }
80
- ref={itemIndex == 0 ? content : null}
82
+ key={i}
83
+ ref={i == 0 ? target : null}
84
+ style={{ display: i == 1 && !overflow ? "none" : "inline" }}
81
85
  >
82
- &nbsp;{item}
86
+ {item}&nbsp;
83
87
  </span>
84
88
  );
85
89
  })}
86
- </span>
90
+ </div>
87
91
  );
88
92
  });