简体   繁体   English

将Google Chart函数添加到对象

[英]Adding Google Chart function to Objects

I'm trying to add a Google Charts feature to an object. 我正在尝试将Google图表功能添加到对象。 I've tested out the following code in NetBeans and it has worked giving it arbitrary data arrays: 我已经在NetBeans中测试了以下代码,并且已经为它提供了任意数据数组:

google.setOnLoadCallback(costChart);
function costChart() {
        var energyType = 'Monthly Cost';
        var energySavings = [1,2,3,4,5,6,7,8,9,10,11,12]; //=this.costSavings
        var month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Month');
        data.addColumn('number', energyType + ' Saved');


        data.addRows([
          [month[0], energySavings[0]],
          [month[1], energySavings[1]],
          [month[2], energySavings[2]],
          [month[3], energySavings[3]],
          [month[4], energySavings[4]],
          [month[5], energySavings[5]],
          [month[6], energySavings[6]],
          [month[7], energySavings[7]],
          [month[8], energySavings[8]],
          [month[9], energySavings[9]],
          [month[10], energySavings[10]],
          [month[11], energySavings[11]]
        ]);

        // Set chart options
        var options = {'title': 'Cost Savings',
                       'width':400,
                       'height':300,
                       'hAxis':{format: 'currency'}
                      };

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.BarChart(document.getElementById('cost_chart_div'));
        chart.draw(data, options);
      }

However, I would like to put this within an object as a method to be used, similar to this.costChart(). 但是,我想把它放在对象中作为要使用的方法,类似于this.costChart()。 Instead of arbitrary data, it would access data within the object, like the commented out this.costSavings on the third line of code. 它将访问对象内的数据,而不是任意数据,就像在第三行代码中注释掉this.costSavings一样。

When I set it up as an object method, I get the error: "Uncaught ReferenceError: google is not defined" 将其设置为对象方法时,出现错误:“未捕获的ReferenceError:未定义google”

Any ideas on how to properly set this method up? 关于如何正确设置此方法的任何想法?

Follow-up: I've added the line of code: 后续:我添加了以下代码行:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
    function run(){
    costChart();

    }

but I'm still getting the same 'google not defined' error. 但我仍然遇到相同的“未定义Google”错误。 I've tried adding the script to the portion but its still nogo. 我尝试将脚本添加到该部分,但仍然没有。 I've tried calling the function separately by passing it the object as an arg, but still the same error. 我尝试通过将对象作为arg传递给函数来单独调用该函数,但仍然是相同的错误。 I have a feeling that it may be due to the way I should be including: 我觉得这可能是由于我应包括的方式所致:

  google.load('visualization', '1.0', {'packages':['corechart']});
  // Set a callback to run when the Google Visualization API is loaded.
  google.setOnLoadCallback(costChart);

but I'm not quite sure where/how to include these in the function. 但我不太确定该在何处/如何在函数中包含这些内容。 Would they go at the top of the script, or within the function that calls costChart(), or within the costChart() function itself, etc...? 它们是放在脚本的顶部,还是在调用costChart()的函数中,还是在costChart()函数本身的内部,等等?

You are getting "google is not defined" because you need this at the top of your code: 您会收到“未定义google”的信息,因为您需要在代码顶部添加以下内容:

  <div id="cost_chart_div"> </div> 

  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <script type="text/javascript">
    // Load the Visualization API and the piechart package.
   google.load('visualization', '1.0', {'packages':['corechart']});

Once you have that in place, you should have no trouble doing what you want with this.costSavings. 一旦有了适当的位置,就可以使用this.costSavings轻松完成所需的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM