javascript - How to dynamically create multiple horizontal bar charts in Google Chart? -
i'm new in google chart api. want use visualize data review on google play store include multiple issues, sentiments , other conditions.
i want build horizontal bar chart x axis containing different app , , each app's containing 7 issues, y axis sum of sentiment of each issue in different app.
i have done horizontal chart containing data in single div element. however, user's convenience, want show 5 data @ in single div element, , dynamically create div element if there more 5 data in current data set. @ last, div elements paint chart horizontally append div element called issuebar_div. user can use scrollbar view different chart.
what i've done:
partition data:
var title = ['app']; var issuerow = {{ projectissues|safe }}; // 7 different issues got django var graph_data = [title.concat(issuerow)]; var cnt = 0; var divide_row = []; var tablerows = ... // tablerows app name mapping sentiment sum corresponding different issue array dictionary. // form app -> [20, 12, -1, 3, 5, 21, 57] (var app in tablerows) { cnt ++; var row = [app]; (var = 0; < tablerows[app].length; i++) { row.push(tablerows[app][i]); } if(cnt < 6){ divide_row.push(row); } else{ graph_data.push(divide_row); divide_row = []; cnt = 0; } }
create div element , draw chart
in order build use scrollbar, add restriction issuebar_div.
<div id="issuebar_div" style="height:400px; overflow-x:scroll; overflow-y:hidden";></div>
dynamically create div element , draw.
function drawissuebar() { var issuebar = document.getelementbyid("issuebar_div"); // titlerow include app , other issue var titlerow = graph_data[0]; delete_element(); for(var = 1;i<graph_data.length;i++){ // create div element , set attribute var my_div = document.createelement("div"); my_div.id = "issuebar_" + i; my_div.style.display = "table-cell"; // append element #issuebar_div issuebar.appendchild(my_div); // sliced data , push total_data var row_data = graph_data[i]; var total_data = [titlerow]; for(var k=0;k<row_data.length;k++){ total_data.push(row_data[k]); } // new data container var data = new google.visualization.arraytodatatable(total_data); var div_width = $("#home").width(); var materialoptions = { height: 400, width: div_width, haxis: { title: 'sum of sentiment' }, vaxis: { title: 'app' }, bars: 'vertical' }; var materialchart = new google.charts.bar(document.getelementbyid("issuebar_" + i)); materialchart.draw(data, materialoptions); } } // delete div element function delete_element(){ = 1; while(true){ element = document.getelementbyid("issuebar_" + i); if(element !== null){ element.outerhtml = ""; delete element; i++; } else{ break; } } }
current problem:
basically, created div elements create expect, chart show 1 chart below.
the fail part when move scrollbar right
how can solve problem?
any answer appreciated. thanks.
all. have solve problem!!!
i notice there important sentence showing on google chart document:
for each chart on page, add call google.charts.setonloadcallback() callback draws chart input
so decide callback drawing method when drawing each bar chart.
the specific solution
1.i use new function callback function. row_data data needing present, , my_div div element store chart.
var drawissuebar = new function("row_data", "my_div", "var data = google.visualization.arraytodatatable(row_data);" + "var div_width = $('#home').width();" + "var materialoptions = {" + "height: 400," + "width: div_width," + "haxis: {" + "title: 'sum of sentiment'" + "}," + "vaxis: {" + "title: 'app'" + "}," + "bars: 'vertical'" + "};"+ "var materialchart = new google.charts.bar(my_div);" + "materialchart.draw(data, materialoptions);" );
2.rewrite data processing.
var titlerow = ... // form of ["app", "issue1", "issue2", ..."issue7"] var cnt = 0; var divide_row = []; (var app in tablerows) { cnt ++; var row = [app].concat(tablerows[app]); if(cnt < 6){ divide_row.push(row); } else{ graph_data.push([titlerow].concat(divide_row)); // console.log(graph_data[graph_data.length - 1]); divide_row = []; cnt = 0; } }
3.write new function called drawissuechart draw bar chart.
function drawissuechart(){ // create div element , draw chart delete_element(); var issuebar = document.getelementbyid('issuebar_div'); for(var = 0;i<graph_data.length;i++){ var my_div = document.createelement("div"); my_div.id = "issuebar_" + i; my_div.style.display = "table-cell"; issuebar.appendchild(my_div); var row_data = graph_data[i]; // use drawissuebar callback google.charts.setonloadcallback(drawissuebar(row_data, my_div)); } }
result
another successful chart when moving scrollbar right
i learn lot solution!
hopefully can people encounter problem.
Comments
Post a Comment