图表x轴滚动条无法滚动问题

文档演示地址:https://f2.antv.vision/zh/examples/line/multiple#series

删除不相干代码,最后调整如下,结果滚动条不见了?
image.png

  1. import React, { useEffect, useRef } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Chart } from "@antv/f2";
  4. const ScrollBar = require('@antv/f2/lib/plugin/scroll-bar');
  5. require('@antv/f2/lib/interaction/pan');
  6. import './index.less';
  7. // 获取设备像素比:高清方案下不模糊
  8. export const WINDOW_DEVICE_PIXEL_RATIO = window.devicePixelRatio;
  9. // 字体大小
  10. export const FONT_SIZE = 14 * WINDOW_DEVICE_PIXEL_RATIO;
  11. // 数据源
  12. const list = [
  13. {
  14. type: "黄金",
  15. date: "02-28",
  16. value: 17
  17. },
  18. {
  19. type: "白银",
  20. date: "02-28",
  21. value: 17
  22. },
  23. {
  24. type: "黄金",
  25. date: "02-27",
  26. value: 23
  27. },
  28. {
  29. type: "白银",
  30. date: "02-27",
  31. value: 23
  32. },
  33. {
  34. type: "黄金",
  35. date: "02-26",
  36. value: 11
  37. },
  38. {
  39. type: "白银",
  40. date: "02-26",
  41. value: 11
  42. },
  43. {
  44. type: "黄金",
  45. date: "02-25",
  46. value: 26
  47. },
  48. {
  49. type: "白银",
  50. date: "02-25",
  51. value: 26
  52. },
  53. {
  54. type: "黄金",
  55. date: "02-24",
  56. value: 21
  57. },
  58. {
  59. type: "白银",
  60. date: "02-24",
  61. value: 21
  62. },
  63. {
  64. type: "黄金",
  65. date: "02-23",
  66. value: 18
  67. },
  68. {
  69. type: "白银",
  70. date: "02-23",
  71. value: 18
  72. },
  73. {
  74. type: "黄金",
  75. date: "02-22",
  76. value: 22
  77. },
  78. {
  79. type: "白银",
  80. date: "02-22",
  81. value: 22
  82. },
  83. {
  84. type: "黄金",
  85. date: "02-21",
  86. value: 18
  87. },
  88. {
  89. type: "白银",
  90. date: "02-21",
  91. value: 18
  92. },
  93. {
  94. type: "黄金",
  95. date: "02-20",
  96. value: 5
  97. },
  98. {
  99. type: "白银",
  100. date: "02-20",
  101. value: 5
  102. },
  103. {
  104. type: "黄金",
  105. date: "02-19",
  106. value: 25
  107. },
  108. {
  109. type: "白银",
  110. date: "02-19",
  111. value: 25
  112. },
  113. {
  114. type: "黄金",
  115. date: "02-18",
  116. value: 30
  117. },
  118. {
  119. type: "白银",
  120. date: "02-18",
  121. value: 30
  122. },
  123. {
  124. type: "黄金",
  125. date: "02-17",
  126. value: 15
  127. },
  128. {
  129. type: "白银",
  130. date: "02-17",
  131. value: 15
  132. },
  133. {
  134. type: "黄金",
  135. date: "02-16",
  136. value: 20
  137. },
  138. {
  139. type: "白银",
  140. date: "02-16",
  141. value: 20
  142. },
  143. {
  144. type: "黄金",
  145. date: "02-15",
  146. value: 10
  147. },
  148. {
  149. type: "白银",
  150. date: "02-15",
  151. value: 10
  152. }
  153. ];
  154. function App() {
  155. let chartRefCurrent = useRef<any>()?.current || {};
  156. const fetchChart = () => {
  157. chartRefCurrent = new Chart({
  158. id: 'line-chart',
  159. pixelRatio: window.devicePixelRatio,
  160. plugins: [ScrollBar],
  161. });
  162. chartRefCurrent.source(list.reverse());
  163. chartRefCurrent.legend({
  164. itemWidth: null,
  165. align: 'center',
  166. nameStyle: {
  167. fontSize: FONT_SIZE,
  168. fill: '#333',
  169. },
  170. });
  171. chartRefCurrent.scale({
  172. date: {
  173. mask: "MM-DD",
  174. values: ["02-15", "02-16", "02-17", "02-18", "02-19"]
  175. },
  176. value: {
  177. tickCount: 6,
  178. },
  179. });
  180. chartRefCurrent.legend({
  181. itemWidth: null,
  182. align: 'center',
  183. nameStyle: {
  184. fontSize: FONT_SIZE,
  185. },
  186. });
  187. chartRefCurrent.axis('date', {
  188. label() {
  189. return {
  190. fontSize: FONT_SIZE,
  191. };
  192. },
  193. });
  194. chartRefCurrent.axis('value', {
  195. label() {
  196. return {
  197. fontSize: FONT_SIZE,
  198. };
  199. },
  200. });
  201. chartRefCurrent.scrollBar();
  202. chartRefCurrent.interaction('pan');
  203. chartRefCurrent.line().position('date*value').color('type');
  204. chartRefCurrent.render();
  205. };
  206. useEffect(() => {
  207. fetchChart();
  208. }, []);
  209. return (
  210. <div className="page">
  211. <div className="line-chart-container">
  212. <canvas id="line-chart" />
  213. </div>
  214. </div>
  215. );
  216. }
  217. ReactDOM.render(<App />, document.getElementById('app'));

支付宝端内canvas内存超限

问题复现步骤

在没有定width和height情况下如图所示
image.png

  1. import React, { useEffect, useRef } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Chart } from '@antv/f2/es/index';
  4. import ScrollBar from '@antv/f2/es/plugin/scroll-bar';
  5. import '@antv/f2/es/interaction/pan';
  6. import './index.less';
  7. // 获取设备像素比:高清方案下不模糊
  8. export const WINDOW_DEVICE_PIXEL_RATIO = window.devicePixelRatio;
  9. // 字体大小
  10. export const FONT_SIZE = 14 * WINDOW_DEVICE_PIXEL_RATIO;
  11. // 数据源
  12. const list = [
  13. {
  14. type: "黄金",
  15. date: "02-28",
  16. value: 17
  17. },
  18. {
  19. type: "白银",
  20. date: "02-28",
  21. value: 17
  22. },
  23. {
  24. type: "黄金",
  25. date: "02-27",
  26. value: 23
  27. },
  28. {
  29. type: "白银",
  30. date: "02-27",
  31. value: 23
  32. },
  33. {
  34. type: "黄金",
  35. date: "02-26",
  36. value: 11
  37. },
  38. {
  39. type: "白银",
  40. date: "02-26",
  41. value: 11
  42. },
  43. {
  44. type: "黄金",
  45. date: "02-25",
  46. value: 26
  47. },
  48. {
  49. type: "白银",
  50. date: "02-25",
  51. value: 26
  52. },
  53. {
  54. type: "黄金",
  55. date: "02-24",
  56. value: 21
  57. },
  58. {
  59. type: "白银",
  60. date: "02-24",
  61. value: 21
  62. },
  63. {
  64. type: "黄金",
  65. date: "02-23",
  66. value: 18
  67. },
  68. {
  69. type: "白银",
  70. date: "02-23",
  71. value: 18
  72. },
  73. {
  74. type: "黄金",
  75. date: "02-22",
  76. value: 22
  77. },
  78. {
  79. type: "白银",
  80. date: "02-22",
  81. value: 22
  82. },
  83. {
  84. type: "黄金",
  85. date: "02-21",
  86. value: 18
  87. },
  88. {
  89. type: "白银",
  90. date: "02-21",
  91. value: 18
  92. },
  93. {
  94. type: "黄金",
  95. date: "02-20",
  96. value: 5
  97. },
  98. {
  99. type: "白银",
  100. date: "02-20",
  101. value: 5
  102. },
  103. {
  104. type: "黄金",
  105. date: "02-19",
  106. value: 25
  107. },
  108. {
  109. type: "白银",
  110. date: "02-19",
  111. value: 25
  112. },
  113. {
  114. type: "黄金",
  115. date: "02-18",
  116. value: 30
  117. },
  118. {
  119. type: "白银",
  120. date: "02-18",
  121. value: 30
  122. },
  123. {
  124. type: "黄金",
  125. date: "02-17",
  126. value: 15
  127. },
  128. {
  129. type: "白银",
  130. date: "02-17",
  131. value: 15
  132. },
  133. {
  134. type: "黄金",
  135. date: "02-16",
  136. value: 20
  137. },
  138. {
  139. type: "白银",
  140. date: "02-16",
  141. value: 20
  142. },
  143. {
  144. type: "黄金",
  145. date: "02-15",
  146. value: 10
  147. },
  148. {
  149. type: "白银",
  150. date: "02-15",
  151. value: 10
  152. }
  153. ];
  154. function App() {
  155. let chartRefCurrent = useRef<any>()?.current || {};
  156. const fetchChart = () => {
  157. chartRefCurrent = new Chart({
  158. id: 'line-chart',
  159. pixelRatio: window.devicePixelRatio,
  160. plugins: [ScrollBar],
  161. });
  162. chartRefCurrent.source(list.reverse());
  163. chartRefCurrent.legend({
  164. itemWidth: null,
  165. align: 'center',
  166. nameStyle: {
  167. fontSize: FONT_SIZE,
  168. fill: '#333',
  169. },
  170. });
  171. chartRefCurrent.scale({
  172. date: {
  173. mask: "MM-DD",
  174. values: ["02-15", "02-16", "02-17", "02-18", "02-19"]
  175. },
  176. value: {
  177. tickCount: 6,
  178. },
  179. });
  180. chartRefCurrent.legend({
  181. itemWidth: null,
  182. align: 'center',
  183. nameStyle: {
  184. fontSize: FONT_SIZE,
  185. },
  186. });
  187. chartRefCurrent.axis('date', {
  188. label() {
  189. return {
  190. fontSize: FONT_SIZE,
  191. };
  192. },
  193. });
  194. chartRefCurrent.axis('value', {
  195. label() {
  196. return {
  197. fontSize: FONT_SIZE,
  198. };
  199. },
  200. });
  201. chartRefCurrent.scrollBar();
  202. chartRefCurrent.interaction('pan');
  203. chartRefCurrent.line().position('date*value').color('type');
  204. chartRefCurrent.render();
  205. };
  206. useEffect(() => {
  207. fetchChart();
  208. }, []);
  209. return (
  210. <div className="page">
  211. <div className="line-chart-container">
  212. <canvas id="line-chart" />
  213. </div>
  214. </div>
  215. );
  216. }
  217. ReactDOM.render(<App />, document.getElementById('app'));

然后给canvas自定义width和height时如图所示
image.png

  1. import { Chart } from '@antv/f2/es/index';
  2. // ...
  3. chartRefCurrent = new Chart({
  4. id: 'line-chart',
  5. pixelRatio: window.devicePixelRatio,
  6. width: 2100,
  7. height: 800,
  8. plugins: [ScrollBar],
  9. });

但这并不符合横屏展示的预期效果,因为没有铺满整个区域,然后继续给canvas自定义width和height加大时如图所示,发现图表不见了???
image.png
然后看控制台得知,是canvas内存超限了

canvas大小限制:https://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element

image.png
这时需要销毁canvas

  1. import { Chart } from '@antv/f2/es/index';
  2. // ...
  3. chartRefCurrent = new Chart({
  4. id: 'line-chart',
  5. width: 0, // 宽度
  6. height: 0, // 高度
  7. });

图表也确实出来了
image.png
然后再让它铺满,也确实铺满了
image.png

  1. import { Chart } from '@antv/f2/es/index';
  2. // ...
  3. chartRefCurrent = new Chart({
  4. id: 'line-chart',
  5. pixelRatio: window.devicePixelRatio,
  6. width: window.innerHeight,
  7. height: window.innerWidth - 100,
  8. plugins: [ScrollBar],
  9. });

此时再回去看控制台,还是有警告,不过图表能正常展示出来
image.png
从代码角度来看,估计这样还是存在隐患的,经过排查与图表库作者确认,是因为宽高太大原因,然后导致白屏(因为无法渲染出来)Why?这个属于IOS系统限制,与f2没有关系