简体   繁体   English

如何修改Google折线图的JSON字符串

[英]How to modify the JSON string for google line chart

I am facing issue while plotting Google Multi-line chart. 绘制Google多折线图时遇到问题。 My application is in Laravel 5.1 and I am fetching data from MySQL database then converting it into JSON string. 我的应用程序在Laravel 5.1中,我正在从MySQL数据库中获取数据,然后将其转换为JSON字符串。 This JSON acts as data source for google data table. 此JSON用作Google数据表的数据源。 But due to the JSON data format I am unable to plot Line chart and giving me the error "All the series on given axis must be of the same data type". 但是由于JSON数据格式,我无法绘制折线图,​​并出现错误“给定轴上的所有序列必须具有相同的数据类型”。

My JSON data is in below format: 我的JSON数据采用以下格式:

{"cols":[{"label":"Datetime","type":"date"},{"label":"Make","type":"string"},{"label":"Sales","type":"number"}],"rows":[{"c":[{"v":"Date(2011, 0, 31)"},{"v":"Datsun"},{"v":0}]},{"c":[{"v":"Date(2011, 1, 28)"},{"v":"Datsun"},{"v":0}]},{"c":[{"v":"Date(2011, 2, 31)"},{"v":"Datsun"},{"v":0}]},{"c":[{"v":"Date(2011, 3, 30)"},{"v":"Datsun"},{"v":0}]},{"c":[{"v":"Date(2011, 4, 31)"},{"v":"Datsun"},{"v":0}]},{"c":[{"v":"Date(2011, 5, 30)"},{"v":"Datsun"},{"v":0}]},{"c":[{"v":"Date(2011, 0, 31)"},{"v":"Fiat"},{"v":2150}]},{"c":[{"v":"Date(2011, 1, 28)"},{"v":"Fiat"},{"v":1839}]},{"c":[{"v":"Date(2011, 2, 31)"},{"v":"Fiat"},{"v":1860}]},{"c":[{"v":"Date(2011, 3, 30)"},{"v":"Fiat"},{"v":2030}]},{"c":[{"v":"Date(2011, 4, 31)"},{"v":"Fiat"},{"v":2143}]},{"c":[{"v":"Date(2011, 5, 30)"},{"v":"Fiat"},{"v":1500}]}]}

But Google-chart requires data in below JSON format: 但Google图表需要以下JSON格式的数据:

 {"cols":[{"label":"Datetime","type":"date"},{"label":"Datsun","type":"string"}, {"label":"Fiat","type":"string"}],

"rows":[{"c":[{"v":"Date(2011, 0, 31)"},{"v":"0"},{"v":2150}]},{"c":  [{"v":"Date(2011, 1, 28)"},{"v":"0"},{"v":1839}]},{"c":[{"v":"Date(2011, 2, 31)"},{"v":"0"},{"v":1860}]}]}

In short, Data is in this format: 简而言之,数据采用以下格式:

Datetime       Make     Sales
2011-01-31    'Datsun'   0   
2011-02-28    'Datsun'   0
2011-03-31    'Datsun'   0
2011-01-31    'Fiat'     2150
2011-02-28    'Fiat'     1839
2011-02-31    'Fiat'     1860

I want the data in below format: 我想要以下格式的数据:

Datetime     Datsun     Fiat
2011-01-31   0          2150
2011-02-28   0          1839
2011-3-31    0          1860

How to modify the JSON data in order to make it compatible with google line chart? 如何修改JSON数据以使其与Google折线图兼容?

You can do this something like this. 您可以这样做。 Here I have neglected several edge cases,(like if you dont get DatSun or Fiat in you object, this will fail, but that can also be handled easily). 在这里,我忽略了一些极端的情况(例如,如果您未在对象中添加DatSun或Fiat,这将失败,但也可以轻松处理)。

var tempObj={}
var colName=[]
    obj.rows.forEach(function(r){
    if(!tempObj[r.c[0].v]){
        tempObj[r.c[0].v]={};
        tempObj[r.c[0].v][r.c[1].v]=r.c[2].v;
        colName.indexOf(r.c[1].v)==-1 ? colName.push(r.c[1].v) : null;
    }
    else {
        tempObj[r.c[0].v][r.c[1].v]=r.c[2].v;
        colName.indexOf(r.c[1].v)==-1 ? colName.push(r.c[1].v) : null;
    }
    })

    result={}
    result.cols=[{"label":"Datetime","type":"date"}];
    for(var i=0;i< colName.length ; i++){
      result.cols.push({"label":colName[i],"type":"string"})
    }
    result.rows=[];
    for(var key in tempObj){
        var temp={};
        temp.c=[];
        temp.c.push({"v":key});
        for(var i=0;i< colName.length ; i++){
          temp.c.push({"v":tempObj[key][colName[i]]});
        }

        result.rows.push(temp);
    }

    console.log(result);

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

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