Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “错误”;google.charts.load()在.45或更早版本中不能被多次调用;_Javascript_Reactjs_Charts_Google Visualization - Fatal编程技术网

Javascript “错误”;google.charts.load()在.45或更早版本中不能被多次调用;

Javascript “错误”;google.charts.load()在.45或更早版本中不能被多次调用;,javascript,reactjs,charts,google-visualization,Javascript,Reactjs,Charts,Google Visualization,我正在使用带有React和Redux的GoogleCharts API。获取“错误google.charts.load()不能在版本45或更低版本中多次调用” 我不确定如何解决这个问题,因为每次由于用户与应用程序的交互而导致数据更改时,我都需要重新呈现图表。我有三个图表是在ComponentWillMount中加载数据后绘制的 componentWillMount() { //Loads all sales belonging to a user this.p

我正在使用带有React和Redux的GoogleCharts API。获取“错误google.charts.load()不能在版本45或更低版本中多次调用”

我不确定如何解决这个问题,因为每次由于用户与应用程序的交互而导致数据更改时,我都需要重新呈现图表。我有三个图表是在ComponentWillMount中加载数据后绘制的

componentWillMount() {

        //Loads all sales belonging to a user

        this.props.fetchSales(this.props.activeUser._id).then(() => {

            // 3 below functions load total revenue for today, the week, and the month

            this.calculateTodaysRevenue();
            this.calculateWeekRevenue();
            this.calculateMonthRevenue();
        });

    }
如果我们看一看calculateTodaysRevenue(),您将看到这是我在.setState({})完成后的最后第一次调用google.charts.load():

calculateTodaysRevenue() {

        //all of today's date values pulled from state

        const { currentDay, currentMonth, currentYear } = this.state;

        let todaysTotalRevenue = 0;
        let dataRowsForTodaysRevenue = [];

        this.props.allSales.map((sale) => {

            //checks the date key of each sale to see if it matches today's date

            if (sale.date.day === currentDay && sale.date.month === currentMonth && sale.date.year === currentYear) {

                todaysTotalRevenue += sale.total; //Adds each sale's total revenue per interation

                let time = [];
                time.push(sale.date.hour, sale.date.minutes);
                dataRowsForTodaysRevenue.push([time, todaysTotalRevenue]);

            } else { return; }

        });

        //sets profits and data rows for the graph only for today's data in local state

        this.setState({
            todaysTotalRevenue,
            dataRowsForTodaysRevenue
        }, () => {

            //After revenue is calculated and data for today is set in local state, draw chart for today's revenue

            google.charts.load('current', {packages: ['corechart', 'line']});
            google.charts.setOnLoadCallback(this.drawTodaysChart.bind(this));

        });

    }

我不知道如何在不调用google.charts.load的情况下将数据加载到图表中。还有人知道我如何解决这个问题吗?我是不是把google.charts.load()和google.charts.setOnLoadCallback()放错地方了?我在componentWillMount()中调用这些函数的原因是,每次我输入此页面的路线时,数据通常会不同,需要重新计算销售额,因此图表的数据会不同。每个图表都有自己的容器div,因此它们也不会在同一个位置绘制。

我将对google.charts.load()的调用移动到index.html中,这样就不会每次运行componentWillMount()时都调用它。您可以看到,每当CalculateToDaysReference()在componentWillMount()中运行时,都会调用google.charts.load()。这就是问题标题中列出的错误的原因。

load
应该在加载dom/页面时调用一次。使用
setOnLoadCallback
了解
load
何时完成。在那之后不需要任何一条语句——所以等待这些语句完成,然后开始其他所有语句。在
setState
之后直接调用
DrawTodayshart
…听起来不错,我会尝试一下,然后更新这个。如果我想传递setOnLoadCallback多个不同的回调函数,这也是可以接受的吗?我想在这个组件的3个单独的div中总共绘制3个图表。不,传递
setOnLoadCallback
一个调用其余部分的函数…因此,如果我不将setOnLoadCallback或load放入componentWillMount,在React组件中放置它们的最佳位置是哪里?在组件的构造函数中?看一下--您可能还考虑使用