【问题标题】:How to push Firestore data to ChartJS?如何将 Firestore 数据推送到 ChartJS?
【发布时间】:2019-10-18 14:23:33
【问题描述】:

我在将数据从 Firestore 数据绘制到 ChartJS 时遇到问题。出于某种原因,它仅在图表底部显示单个值,并在 y 轴上显示随机数而不是日期。我希望有一个人可以帮助我。 我的火库:firestore 我的图表:chart

这是我目前所拥有的。

    db.collection('Items').get().then((snapshot) => {
    snapshot.docs.forEach(doc => {
        var item= doc.data();

        var price= item.price;
        var date = item.date;

    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: [date],
        datasets: [{
            label: 'Items',
            data: [price],  
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
        // Container for pan options
        pan: {
            // Boolean to enable panning
            enabled: true,

            // Panning directions. 
            mode: 'x',

            speed: 1
        },

        // Container for zoom options
        zoom: {
            // enable zooming
            enabled: true,                      
            // Zooming directions. 
            mode: 'x',
            }
        }
    });
  })
})

【问题讨论】:

    标签: javascript html firebase google-cloud-firestore chart.js


    【解决方案1】:

    对于snapshot 中的每个doc,您正在执行var myChart = new Chart()每次创建一个新图表。

    您应该在forEach 中构建您的datalabels 数组,然后(forEach 之外)创建一个新图表并将其传递给数组。

    类似于以下内容(未测试):

    var labelsArray = [];
    var dataArray = [];
    
    db.collection('Items').get().then((snapshot) => {
        snapshot.docs.forEach(doc => {
            var item = doc.data();
    
            var price = item.price;
            dataArray.push(price);
    
            var date = item.date;
            labelsArray.push(date);
        });
    });
    
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: labelsArray,
        datasets: [{
            label: 'Items',
            data: dataArray ,  
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        },
        // Container for pan options
        pan: {
            // Boolean to enable panning
            enabled: true,
    
            // Panning directions. 
            mode: 'x',
    
            speed: 1
        },
    
        // Container for zoom options
        zoom: {
            // enable zooming
            enabled: true,                      
            // Zooming directions. 
            mode: 'x',
            }
        }
    });
    

    【讨论】:

    • 嗨,我的 (dataArray) price 包含 $ 符号的数据似乎有问题,它不显示数据。对于labelsArray,奇怪的是,当我调整浏览器大小时,数据只会显示在图表上。有什么想法吗?
    • 解决了调整窗口大小时图表渲染的问题。如果我没记错的话,chartJS 渲染问题的原因是因为图表是在隐藏元素div 中创建的。参考CharJs render on window resize.
    猜你喜欢
    • 1970-01-01
    • 2022-01-27
    • 2021-12-25
    • 2018-06-13
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2021-07-10
    相关资源
    最近更新 更多