简体   繁体   English

图表未使用Google图表显示

[英]Chart is not displaying using google chart

Hi I am using google charts to display a chart but it is not displaying a chart. 嗨,我正在使用Google图表显示图表,但未显示图表。 I can see the json printed as well in the firbug console but still chart is not displaying. 我可以在firbug控制台中看到打印的json,但仍未显示图表。 Below is my code 下面是我的代码

function drawChart() {
        $.ajax({
            url: ipaddress+'/getSpecificFields',
            dataType: "json",
            success: function (jsonData) {
                alert('success');
                if (!$.browser.msie) {
                    console.log(jsonData);

                }
                // Create our data table out of JSON data loaded from server.
                var data = new google.visualization.DataTable(jsonData);
                data.addColumn("string", "abc");
                data.addColumn("string", "cde");
                data.addColumn("string", "fgh");

                data.addRows(jsonData.length);

        var i = 0;
        $.each(jsonData, function () {

            data.setValue(i, 0, this.abc);
            data.setValue(i, 1, this.cde);
            data.setValue(i, 2, this.fgh);
            i++;
        });
                // Instantiate and draw our chart, passing in some options.
                var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                chart.draw(data, {
                    width: 400,
                    height: 240
                });
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + '\n' + errorThrown);
                if (!$.browser.msie) {
                    console.log(jqXHR);
                }
            }
        });
    }}

json which is returning from the server 从服务器返回的json

[Object { abc="leave, cde="80%", fgh="52b83880a36dcda423000001"}, Object { abc="Meeting", cde="60%", fgh="52b83880a36dcda423000002"}, Object { abc="Work", cde="70%", fgh="52b83880a36dcda423000003"}]

from browser eg localhost:3000/something 从浏览器,例如localhost:3000 / something

[
  {
    "abc": "Work",
   "cde": "50%",
    "ghe": "1",    
  },
  {    
    "cde": "50%",
    "abc": "Sick",
    "ghe": "2"
  },
  {
    "abc": "Some",
    "cde": "50%",
    "ghe": "3",    
  }
]

and server side code in node js 和节点js中的服务器端代码

var jsonString = [];
var jsonParse;
test.find({abc:1,cde:1,ghe:1},function (err, list, count) {
        if (err) throw(err);
        list.forEach(function(listLoop){

            jsonParse = JSON.parse(JSON.stringify(listLoop));
            console.log("jsonParse " + JSON.stringify(listLoop)); 
            jsonString.push(jsonParse);
        });        
        res.json(jsonString);
    });

Your JSON doesn't seem correct, you should check the Google docs here: 您的JSON似乎不正确,您应该在此处查看Google文档:

https://developers.google.com/chart/interactive/docs/php_example https://developers.google.com/chart/interactive/docs/php_example

and this SO question which sounds like exactly what you are trying to do: 这样的问题听起来就像您要尝试执行的操作:

Creating Google Pie Chart based on JSON data using CodeIgniter 使用CodeIgniter根据JSON数据创建Google饼图

First off, you need to remove jsonData from the DataTable constructor: 首先,您需要从DataTable构造函数中删除jsonData

var data = new google.visualization.DataTable(jsonData);

should be: 应该:

var data = new google.visualization.DataTable();

Second, your data is comprised entirely of strings, which a PieChart can't do anything with. 其次,您的数据完全由字符串组成,PieChart无法执行任何操作。 PieCharts require two columns of data: 1 string for the labels and 1 number for the values. PieCharts需要两列数据:1个字符串用于标签,1个数字用于值。 If you want to create a number column, make sure that your JSON does not put quotes around the numbers. 如果要创建数字列,请确保JSON不会在数字两边加上引号。

Third (and I assume this one is just a typo from changing your real data to sample data), you pull data from the fgh property of the objects in the data array, but you don't have an fgh property in the objects. 第三(我假设这只是将实际数据更改为样本数据的错字),您从数据数组中对象的fgh属性中提取数据,但是对象中没有fgh属性。

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

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