servlets - Google Chart - Populate with JSON -


i have been trying fill google chart dynamicly, example: https://developers.google.com/chart/interactive/docs/php_example

i'm using dependency datatable class , others:

<dependency>     <groupid>com.google.visualization</groupid>     <artifactid>visualization-datasource</artifactid>     <version>1.1.1</version> </dependency> 

this api datatable class: https://developers.google.com/chart/interactive/docs/dev/dsl_javadocs/com/google/visualization/datasource/datatable/datatable

i'm using java httpservlet contains code (example) populate datatable:

datatable datatable = new datatable(); datatable.addcolumn(new columndescription("uur", valuetype.text, "uur")); datatable.addcolumn(new columndescription("aantal", valuetype.number, "aantal spellen"));         try {     datatable.addrowfromvalues("1", 5, true);     datatable.addrowfromvalues("2", 9, true);     datatable.addrowfromvalues("3", 17, true); } catch (typemismatchexception ex) {     logger.getlogger(verkrijgchartdataservlet.class.getname()).log(level.severe, null, ex); } 

i included make return json:

response.setcontenttype("application/json"); 

then gson convert json string:

new gson().tojson(datatable); 

this returns string of format:

{   "columnindexbyid" : {"aantal":1, "uur":0},   "columns" : [       {"id":"uur","label":"uur","pattern":"","type":"text"},       {"id":"aantal","label":"aantal spellen","pattern":"","type":"number"}     ],   "rows" : [        {"cells":[{"value":{"value":"1"}},{"value":{"value":5.0}}]},       {"cells":[{"value":{"value":"2"}},{"value":{"value":9.0}}]},       {"cells":[{"value":{"value":"3"}},{"value":{"value":17.0}}]},     ],   "warnings" : [] } 

but google chart says: 'table has no colums.' google's example json looks this:

{   "cols": [         {"id":"","label":"topping","pattern":"","type":"string"},         {"id":"","label":"slices","pattern":"","type":"number"}       ],   "rows": [         {"c":[{"v":"mushrooms","f":null},{"v":3,"f":null}]},         {"c":[{"v":"onions","f":null},{"v":1,"f":null}]},         {"c":[{"v":"olives","f":null},{"v":1,"f":null}]},         {"c":[{"v":"zucchini","f":null},{"v":1,"f":null}]},         {"c":[{"v":"pepperoni","f":null},{"v":2,"f":null}]}       ] } 

does has idea of i'm doing wrong? need else? thanx in advance.

a bit late better late never. think know answer question.

you know answer. in question, give google chart json example. json string needs start

{"cols":...} 

while json string, rendered gson, starts

{"columnindexbyid":{...},  "columns":... } 

if pass json string google datatable visualisation, its structure needs google example. instead of "columns", needs "cols".


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -