将图表类型参数作为谷歌图表函数传递给自定义函数 [英] Passing chart type parameter to custom function as google chart function

查看:164
本文介绍了将图表类型参数作为谷歌图表函数传递给自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用谷歌图表创建图表。为此我创建了一个名为 myJchart 的自定义函数,如下所示。

 功能myJChart(ChartPackage,图图表,ChartData,ChartTitel,ChartSubtitle,Chart3D,ChartHeight,ChartWidth,ChartDivID){

的google.load( 可视化, 1,{软件包:[ChartPackage]}) ;
google.setOnLoadCallback(drawChart);
函数drawChart(){
var data = google.visualization.arrayToDataTable(ChartData);
var options = {
title:ChartTitel,
副标题:ChartSubtitle,
is3D:true,
height:ChartHeight,
width:ChartWidth
};


var chart = new google.visualization.PieChart(document.getElementById(ChartDivID));
chart.draw(data,options);
}

}

和调用函数

  myJChart('corechart','PieChart',<?php echo $ jsonTable;?>,'test','再次测试' ,真实的, '600', '800', 'chart_div'); 

上述功能运作良好。
现在的问题是,当我将 ChartType 参数传递给google google.visualization时。 ChartType
i获取低于错误消息



Uncaught TypeError:google。 visualization.ChartType不是一个函数



我使用了 [ChartType] 但我得到了这个错误意外的标记[
任何建议都会有帮助。

解决方案

我建议您使用 ChartWrapper Class ...



主要是因为您只想调用一次 google.load 一次。



< ChartWrapper将根据图表类型动态加载所需的数据。



< script src =                  https://www.google.com/jsapi\"></script><div id =chart_div>< / div>  


Currently i am creating chart using google chart. for this i have created one custom function called myJchart as below.

    function myJChart(ChartPackage,ChartType,ChartData,ChartTitel,ChartSubtitle,Chart3D,ChartHeight,ChartWidth,ChartDivID){

        google.load("visualization", "1", {packages:[ChartPackage]});
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable(ChartData);
            var options = {
                title: ChartTitel,
                subtitle: ChartSubtitle,
                is3D: true,
                height: ChartHeight,
                width: ChartWidth               
                };


        var chart = new google.visualization.PieChart(document.getElementById(ChartDivID));
            chart.draw(data, options);
      }

    }

And Calling function

myJChart('corechart','PieChart',<?php echo $jsonTable;?>,'test','test again',true,'600','800','chart_div'); 

this above function is working good. now the problem is that when i pass the ChartType parameter to google google.visualization.ChartType i am getting below error message

Uncaught TypeError: google.visualization.ChartType is not a function

i used [ChartType] but i got this error Unexpected token [ any suggestion will help.

解决方案

I would suggest using the ChartWrapper Class for this...

mainly because you only want to call google.load once.

ChartWrapper will dynamically load what it needs depending on the chart type.

google.load("visualization", "1.1", {packages:["controls"]});
google.setOnLoadCallback(googleLoaded);

// test data
var rowData1 = [['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua  Guinea','Rwanda', 'Average'],
                ['2004/05', 165, 938, 522, 998, 450, 114.6],
                ['2005/06', 135, 1120, 599, 1268, 288, 382],
                ['2006/07', 157, 1167, 587, 807, 397, 623],
                ['2007/08', 139, 1110, 615, 968, 215, 409.4],
                ['2008/09', 136, 691, 629, 1026, 366, 569.6]];

function googleLoaded() {
    myJChart('corechart', 'BarChart', rowData1, 'test', 'test again', true, 600, 800, 'chart_div');
}

function myJChart(ChartPackage, ChartType, ChartData, ChartTitel, ChartSubtitle, Chart3D, ChartHeight, ChartWidth, ChartDivID){
    chart = new google.visualization.ChartWrapper({
        chartType: ChartType,
        containerId: ChartDivID,
        dataTable: google.visualization.arrayToDataTable(ChartData),
        options: {
            title: ChartTitel,
            subtitle: ChartSubtitle,
            is3D: Chart3D,
            height: ChartHeight,
            width: ChartWidth
        }
    });

    chart.draw();
}

<script src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>

这篇关于将图表类型参数作为谷歌图表函数传递给自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆