chart.js数据在初始页面加载时不显示

aelbi1ox  于 2023-04-20  发布在  Chart.js
关注(0)|答案(1)|浏览(219)

我有一个图表在react使用chart.js这里是代码

  1. function Bar() {
  2. const [backendData, setBackendData] = useState([{}])
  3. useEffect(() => {
  4. Axios.get('http://localhost:3001/get').then((response) =>{
  5. console.log(response.data)
  6. setBackendData(response.data)
  7. })
  8. }, [])
  9. //const total = backendData[1].total;
  10. //console.log(total)
  11. // const NethEmpty = 30-total
  12. //monday chart data
  13. var nethkinData = {
  14. labels: backendData.map(time => time.time),
  15. datasets: [{
  16. data: backendData.map(total => 30 - (total.total)),
  17. label: "Empty parking spaces",
  18. fill: true,
  19. backgroundColor: "rgba(211,211,211,0.5)",
  20. borderColor: "rgb(211,211,211)"
  21. }]
  22. }
  23. // nethkin chart options
  24. var nethkinOptions = {
  25. scales: {
  26. y: {
  27. suggestedMin: 0,
  28. suggestedMax: 30,
  29. ticks: {
  30. color: 'Grey',
  31. },
  32. grid: {
  33. display: true,
  34. }
  35. },
  36. x: {
  37. ticks: {
  38. color: 'Grey',
  39. }
  40. },
  41. },
  42. animation: {
  43. y: {
  44. duration: 3000,
  45. from: 3000
  46. }
  47. },
  48. }
  49. useEffect(() => {
  50. // var test = document.getElementById("0").getContext('2d');
  51. // test.destroy();
  52. // monday chart
  53. var ctx = document.getElementById('nethkinChart').getContext('2d');
  54. let nethkinChartStatus = Chart.getChart("nethkinChart");
  55. if (nethkinChartStatus != undefined) {
  56. nethkinChartStatus.destroy();
  57. }
  58. var nethkinChart = new Chart(ctx, {
  59. type: 'line',
  60. data: nethkinData,
  61. options: nethkinOptions,
  62. });
  63. }, [])
  64. return (
  65. <>
  66. {/* browser tab title and icon */}
  67. <Head>
  68. <link rel="icon" href="/latech.ico"/>
  69. <title>Parking Statistics</title>
  70. </Head>
  71. <Sidebar pageWrapId={'page-wrap'} outerContainerId={'outer-container'}/>
  72. {/* navbar title */}
  73. <div className={lots.navbar}>
  74. <h2>Parking Statistics</h2>
  75. </div>
  76. {/* page content */}
  77. {/* bar charts */}
  78. {/* monday */}
  79. <div className={stats.chart}>
  80. <h2>Nethkin Stats</h2>
  81. <canvas id='nethkinChart'></canvas>
  82. </div>
  83. </>
  84. )
  85. }
  86. export default Bar;

我的问题是,当我第一次加载页面的数据将不会在那里。而且,当我重新加载页面的数据将不会显示的数据只显示了当我改变图表从线到酒吧然后数据将在那里,直到我离开页面

oalqel3c

oalqel3c1#

你的问题是,当nethkinData更新时,你的第二个useEffect钩子没有被调用,你应该把nethkinData计算到useMemo中,然后把它作为第二个useEffect的依赖数组添加进去。

  1. const nethkinData = useMemo(() => {
  2. return {
  3. labels: backendData.map((time) => time.time),
  4. datasets: [
  5. {
  6. data: backendData.map((total) => 30 - total.total),
  7. label: "Empty parking spaces",
  8. fill: true,
  9. backgroundColor: "rgba(211,211,211,0.5)",
  10. borderColor: "rgb(211,211,211)",
  11. },
  12. ],
  13. };
  14. }, [backendData]);
  15. // ...
  16. useEffect(() => {
  17. var ctx = document.getElementById("nethkinChart").getContext("2d");
  18. let nethkinChartStatus = Chart.getChart("nethkinChart");
  19. if (nethkinChartStatus != undefined) {
  20. nethkinChartStatus.destroy();
  21. }
  22. var nethkinChart = new Chart(ctx, {
  23. type: "line",
  24. data: nethkinData,
  25. options: nethkinOptions,
  26. });
  27. }, [nethkinData]);
展开查看全部

相关问题