使用Google Visualizations动态调用构造函数?(Dynamically calling a constructor with Google Visualizations?)

我正在使用谷歌可视化,我正试图解决一些问题。 这对我来说似乎很简单,但我似乎无法找到适合Javascript的文档,也无法在Google文档中找到能够让我清楚知道如何执行此操作的文档。

现在我在我的js文件中有一个看起来像这样的函数:

function drawChart(raw,columns,options,chart) { var data = new google.visualization.arrayToDataTable(JSON.parse(raw)); var view = new google.visualization.DataView(data); view.setColumns(columns); chart.draw(view, options); }

除了构造函数的一个讨厌的问题之外,它看起来很好

看一下函数的示例调用:

drawChart(grabarray("?warid="+warid+"&type=0"),[0, 1,{ calc: "stringify", sourceColumn: 1,type: "string",role: "annotation" },2], { title: "Attempted actions by player", },new google.visualization.BarChart(destination);

(注意AJAX正在正常完成,但我剪切它以使示例更有意义)

正如您在上面所看到的,我被迫在javascript中直接使用以下行:

new google.visualization.BarChart(destination)

当我想在我的AJAX回归中包含值“BarChart”时,我可以做这样的事情

new google.visualization("BarChart",destination)

这将使我能够使用相同的drawChart函数调用创建其他谷歌可视化。

TL:DR,如何动态调用构造函数来实现:

new google.visualization("BarChart",destination) new google.visualization("PieChart",destination) new google.visualization("LineGraph",destination)

I'm working with Google Visualizations and I'm trying to sort something out. This seems simple to me but I can't seem to find the right documentation for Javascript nor in the Google documentation that would give me a clear idea how to do this.

Right now I have a function in my js file that looks like this:

function drawChart(raw,columns,options,chart) { var data = new google.visualization.arrayToDataTable(JSON.parse(raw)); var view = new google.visualization.DataView(data); view.setColumns(columns); chart.draw(view, options); }

Which looks well and good except for one pesky problem with the constructor

Take a look at an example call of the function:

drawChart(grabarray("?warid="+warid+"&type=0"),[0, 1,{ calc: "stringify", sourceColumn: 1,type: "string",role: "annotation" },2], { title: "Attempted actions by player", },new google.visualization.BarChart(destination);

(Note AJAX is being done properly but I cut it to make the example make more sense)

As you can see above I'm forced to have the following line directly in the javascript:

new google.visualization.BarChart(destination)

When I would like to just have the value "BarChart" included in my AJAX return so I could do something like this

new google.visualization("BarChart",destination)

This would enable me to call create other google visualizations all using the same drawChart function.

TL:DR, how can I call a constructor dynamically to achieve this:

new google.visualization("BarChart",destination) new google.visualization("PieChart",destination) new google.visualization("LineGraph",destination)

最满意答案

试试这样......

new google.visualization["BarChart"](destination)

try it like this...

new google.visualization["BarChart"](destination)

使用Google Visualizations动态调用构造函数?(Dynamically calling a constructor with Google Visualizations?)

我正在使用谷歌可视化,我正试图解决一些问题。 这对我来说似乎很简单,但我似乎无法找到适合Javascript的文档,也无法在Google文档中找到能够让我清楚知道如何执行此操作的文档。

现在我在我的js文件中有一个看起来像这样的函数:

function drawChart(raw,columns,options,chart) { var data = new google.visualization.arrayToDataTable(JSON.parse(raw)); var view = new google.visualization.DataView(data); view.setColumns(columns); chart.draw(view, options); }

除了构造函数的一个讨厌的问题之外,它看起来很好

看一下函数的示例调用:

drawChart(grabarray("?warid="+warid+"&type=0"),[0, 1,{ calc: "stringify", sourceColumn: 1,type: "string",role: "annotation" },2], { title: "Attempted actions by player", },new google.visualization.BarChart(destination);

(注意AJAX正在正常完成,但我剪切它以使示例更有意义)

正如您在上面所看到的,我被迫在javascript中直接使用以下行:

new google.visualization.BarChart(destination)

当我想在我的AJAX回归中包含值“BarChart”时,我可以做这样的事情

new google.visualization("BarChart",destination)

这将使我能够使用相同的drawChart函数调用创建其他谷歌可视化。

TL:DR,如何动态调用构造函数来实现:

new google.visualization("BarChart",destination) new google.visualization("PieChart",destination) new google.visualization("LineGraph",destination)

I'm working with Google Visualizations and I'm trying to sort something out. This seems simple to me but I can't seem to find the right documentation for Javascript nor in the Google documentation that would give me a clear idea how to do this.

Right now I have a function in my js file that looks like this:

function drawChart(raw,columns,options,chart) { var data = new google.visualization.arrayToDataTable(JSON.parse(raw)); var view = new google.visualization.DataView(data); view.setColumns(columns); chart.draw(view, options); }

Which looks well and good except for one pesky problem with the constructor

Take a look at an example call of the function:

drawChart(grabarray("?warid="+warid+"&type=0"),[0, 1,{ calc: "stringify", sourceColumn: 1,type: "string",role: "annotation" },2], { title: "Attempted actions by player", },new google.visualization.BarChart(destination);

(Note AJAX is being done properly but I cut it to make the example make more sense)

As you can see above I'm forced to have the following line directly in the javascript:

new google.visualization.BarChart(destination)

When I would like to just have the value "BarChart" included in my AJAX return so I could do something like this

new google.visualization("BarChart",destination)

This would enable me to call create other google visualizations all using the same drawChart function.

TL:DR, how can I call a constructor dynamically to achieve this:

new google.visualization("BarChart",destination) new google.visualization("PieChart",destination) new google.visualization("LineGraph",destination)

最满意答案

试试这样......

new google.visualization["BarChart"](destination)

try it like this...

new google.visualization["BarChart"](destination)