Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
this.someProperty不是JavaScript对象中的函数_Javascript_Jquery_Google Visualization - Fatal编程技术网

this.someProperty不是JavaScript对象中的函数

this.someProperty不是JavaScript对象中的函数,javascript,jquery,google-visualization,Javascript,Jquery,Google Visualization,我试图通过添加函数作为属性对象来抽象一些JavaScript代码。最终目标是使用谷歌图表动态呈现数据。下面是我的图表对象,当我在页面上调用它时,它现在可以工作。这是假设一个单独的config和util对象具有一些其他方法: app.charts = { init: function(){ // Load the Visualization API and the piechart package, wait until loaded google.load('visuali

我试图通过添加函数作为属性对象来抽象一些JavaScript代码。最终目标是使用谷歌图表动态呈现数据。下面是我的
图表
对象,当我在页面上调用它时,它现在可以工作。这是假设一个单独的
config
util
对象具有一些其他方法:

app.charts = {
  init: function(){

    // Load the Visualization API and the piechart package, wait until loaded
    google.load('visualization', '1.0', {'packages':['corechart']});
    google.setOnLoadCallback(this.createChart);
  },
  createChart: function(){

    // draw charts to their id
    chartData = app.utils.getAjax(s.urls.dashUrl, function(data){

      // Convert to a table
      var jsonDataTable = new google.visualization.DataTable(data);

      // Select html element and draw the chart
      var chartJson = new google.visualization.BarChart(document.getElementById('json'));
      chartJson.draw(jsonDataTable);
      this.dataTable();
    });
  },
  dataTable: function(){
    console.log("whatever");
  }

};
我想做的是抽象
var chartJson=new google.visualization.BarChart(document.getElementById('json'))行,这样我可以提供一个选项并切换到不同的图表,而不是硬编码每个我想要的图表(这样我可以让用户选择图表类型)

每当我尝试编写另一个方法时,我会得到一个
google.visualization是未定义的
错误。我不明白为什么,因为我在
init
中的google load回调之后才调用任何东西

为了简单起见,我尝试使用
dataTable:
函数返回一个新的
new google.visualization.dataTable(data);
然后我收到了
引用错误:dataTable未定义


我不确定是什么原因导致这些值无法找到或使用,非常感谢您的帮助。

我刚刚编写了一些代码,认为您的意思是一件事,但我不确定它现在是否正在阅读后续评论…我正在发布它,仅供参考:

app.charts = {
  init: function(){

    // Load the Visualization API and the piechart package, wait until loaded
    google.load('visualization', '1.0', {'packages':['corechart']});
    google.setOnLoadCallback(this.createChart);
  },

  createChart: function(){

    // draw charts to their id
    chartData = app.utils.getAjax(s.urls.dashUrl, function(data){

      // Convert to a table
      var jsonDataTable = new google.visualization.DataTable(data);

      // Select html element and draw the chart
      var input = document.getElementById('user-input').value;
      this.chartType(input);
      this.dataTable();
    });

  },

  dataTable: function(){
    console.log("whatever");
  },

  chartType: function(input){

    switch(input)
    {
        case 'whatever':
            var chartJson = new google.visualization.BarChart(document.getElementById('json'));
            chartJson.draw(jsonDataTable);
        break;
        case 'whatever2':
            // etc
        break;
    }

  }

};
HTML

<select id="user-input">
    <option value="whatever">whataver</option>
    <option value="whatever2">whataver2</option>
</select>

什么
什么

回调中的这个
不是你想象中的对象。
app.charts.dataTable
另一方面应该可以工作。@KevinB这很有趣。我假设
这个
实际上是指
createChart
函数,对吗?我想我正在尝试调整一些范围。不,
 这将是
app.utils.getAjax
作为上下文提供的内容,类似于事件处理程序通常将dom元素作为上下文,而不是事件处理程序绑定到的范围内的内容。好的,感谢您的澄清。