My ChartJS Line需要先单击颜色图例,然后才能绘制数据

nc1teljy  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(162)

我正在使用Chart JS来显示来自firebase firestore数据库的传感器读数。我遇到了这个奇怪的bug,我的图表最初没有绘制数据〉〉https://imgur.com/gallery/2AoVdo7,但在我点击颜色图例两次〉〉https://imgur.com/gallery/acdblsA或按下f12开发者工具后,绘图突然出现〉〉〉https://imgur.com/gallery/IMxEdZ3。这是bug还是我的代码有问题?
下面是我代码:

import {Getfs} from '../firestore';
import {Line} from 'react-chartjs-2';
import {useEffect} from "react";
import {Chart as ChartJS, Title, Tooltip, LineElement, Legend, CategoryScale, LinearScale, PointElement, } from 'chart.js';

ChartJS.register(
    Title,Tooltip, LineElement, Legend, CategoryScale, LinearScale, PointElement
)

export default function Graph (){
    var sen_readings;
    var c_label = [];
    var ph_data = [];
    var temp_data = [];
    var tur_data = [];
    var dis_data = [];
    useEffect(() => {
        sen_readings = Getfs();
        sen_readings.then(function(result){
            for (var i = 0; i < result.length; i++){
                c_label.push(result[i].time);
                ph_data.push(result[i].pH);
                temp_data.push(result[i].temperature);
                tur_data.push(result[i].turbidity);
                dis_data.push(result[i].disO);
            }
        })
    }, []);

    function makeCHART_PH(){
        const ph_chart = {
            labels: c_label,
            datasets:[{
                label:"pH Readings",
                data: ph_data,
                backgroundColor: 'blue',
            }]
        }
        return ph_chart;
    }

    function makeCHART_DISOLVE(){
        const diso_chart = {
            labels: c_label,
            datasets:[{
                label:"Dissolved Oxygen Readings",
                data: dis_data,
                backgroundColor: 'blue',
            }]
        }
        return diso_chart;
    }

    function makeCHART_TEMP(){
        const temp_chart = {
            labels: c_label,
            datasets:[{
                label:"Temperature Readings",
                data: temp_data,
                backgroundColor: 'blue',
            }]
        }
        return temp_chart;
    }

    function makeCHART_TURBID(){
        const tur_chart = {
            labels: c_label,
            datasets:[{
                label:"Turbidity Readings",
                data: tur_data,
                backgroundColor: 'blue',
            }]
        }
        return tur_chart;
    }

    return(
        <div>
            <form>
                <h3>Select Date Range:</h3>
                <div class="date1">
                    <input type="date" class = "d1"/>
                </div>
                <p>to</p>
                <div class="date2">
                    <input type="date" class = "d2"/>
                </div>
            </form>
            <div class="datasets">
                <h2>Data Table</h2>                
                    <table>
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Time</th>
                                <th>pH</th>
                                <th>Temperature (°C)</th>
                                <th>Dissolved Oxygen (mg/L)</th>
                                <th>Turbidity (JTU)</th>
                            </tr>
                        </thead>
                        <tbody id= "tbody1" ></tbody> 
                    </table>
                 <a href="#">Show All</a>
            </div>
            <div class="chartWrapper">
                <div class="chartAreaWrapper">
                    <Line data ={makeCHART_PH()} style = {{width: '100%', height: '500px' }}></Line>
                    <Line data ={makeCHART_TURBID()} style = {{width: '100%', height: '500px' }}></Line>
                    <Line data ={makeCHART_TEMP()} style = {{width: '100%', height: '500px' }}></Line>
                    <Line data ={makeCHART_DISOLVE()} style = {{width: '100%', height: '500px' }}></Line>
                </div>
            </div>
        </div>

    )
}
yuvru6vn

yuvru6vn1#

将数据作为函数调用。将数据作为状态对象变量。如{“chart_PH”:[],“chart_Dissolve”:[]},并使用函数对其进行设置。
将数据变量添加到useEffect依赖项数组中。例如:从[]到[data]。依赖项中的[]意味着,它将仅在第一次呈现时加载。[data]意味着,它将在每次数据更改时呈现。

相关问题