javascript - Populate a list (not HTML) from JSON -
javascript - Populate a list (not HTML) from JSON -
i trying utilize wordcloud2.js generate word clouds. works fine using illustration given:
var options = { list : [ ["pear", "9"], ["grape", "3"], ["pineapple", "8"], ["apple", "5"] ], gridsize: math.round(16 * document.getelementbyid('canvas_cloud').offsetwidth / 1024), weightfactor: function (size) { homecoming math.pow(size, 1.9) * document.getelementbyid('canvas_cloud').offsetwidth / 1024; } } wordcloud(document.getelementbyid('canvas_cloud'), options);
however, struggling populating "list :" info json file next structure:
[ { "wordcloud": "manchester", "freq": 2321 }, { "wordcloud": "munich", "freq": 566 }, { ... }, { "wordcloud": "madrid", "freq": 6 } ]
i know because limited knowledges on pushing values arrays. far, tries have been:
$.getjson('json/wordcloudgwt-' + site + '.json', function (data) { var wordcloudgwt = []; (var i=0;i<100;i++) { wordcloudgwt.push([data[i].wordcloud, data[i].freq]); }; console.log(wordcloudgwt); var options = { list : wordcloudgwt, gridsize: math.round(16 * document.getelementbyid('canvas_cloud').offsetwidth / 1024), weightfactor: function (size) { homecoming math.pow(size, 1.9) * document.getelementbyid('canvas_cloud').offsetwidth / 1024; } } wordcloud(document.getelementbyid('canvas_cloud'), options);
console.log(wordcloudgwt); shows array 100 (objects?) length of 2 each, wordcloud doesn't show. see in browser's console error assume because wordcloud2.js not interpreting list : wordcloudgwt (erroneously) think should be.
if bruteforce creation of list way
list : [ [data[0].wordcloud, "9"], [data[1].wordcloud, "3"], [data[2].wordcloud, "8"], [data[3].wordcloud, "5"] ],
the words shown correctly, approach has 2 problems:
real frequency of words (word's size) not considered there surelly more elegant ways generate list manually adding 100 lines of codefor first point, figured solve problem manually editing list way:
list : [ [data[0].wordcloud, data[0].freq], [data[1].wordcloud, data[1].freq], [data[2].wordcloud, data[2].freq], [data[3].wordcloud, data[3].freq] ],
however, doing ends same js error first attempt.
any hint can help me bypass difficulties?
you can utilize array.map
format data:
var formattedlist = responsedata.map(function(item) { homecoming [item.wordcloud, item.freq] });
demo: http://jsfiddle.net/64v75enq/
javascript arrays json word-cloud
Comments
Post a Comment