javascript - Adding sparkline chart to dynamically populated HTML table -
i trying add sparkline chart @ end of each row of dynamically populated table. here code:
function populateoveralloverview(result){ var table = document.getelementbyid("firsttaboverall"); function addcell(tr, text) { var td = tr.insertcell(); td.textcontent = text; return td; } result.foreach(function (item) { // sparkline chart here var dataspan = document.createelement('span'); dataspan.sparkline(amountdata, { type: 'bar', barcolor: '#191919'}); var row = table.insertrow(); addcell(row, item.category); addcell(row, '$ ' + item.currentmonthamt.tofixed(2)); addcell(row, item.topmonthstr + ' ($ ' + item.topamount.tofixed(2) + ')'); }); }
sample data amountdata [5,6,7,2,0,-4,-2,4]. html table:
<table id="firsttaboverall" class="table" style="font-size:13px"> <tr> <th>category <span id="sparkline1"></span></th> <th>current month revenue</th> <th>best selling month</th> </tr> </table>
i trying dynamically create sparkline chart add end of row. however, getting error message:
uncaught (in promise) typeerror: dataspan.sparkline not function
i not sure how dynamically create span id, use id .sparkline. ideas?
$(document).ready(function() { var amountdata = [5, 6, 7, 2, 0, -4, -2, 4]; function populateoveralloverview(result) { var table = document.getelementbyid("firsttaboverall"); function addcell(tr, text) { var td = tr.insertcell(); td.textcontent = text; return td; } result.foreach(function(item) { var row = table.insertrow(); addcell(row, item.category); addcell(row, '$ ' + item.currentmonthamt.tofixed(2)); addcell(row, item.topmonthstr + ' ($ ' + item.topamount.tofixed(2) + ')'); var lastcell = addcell(row, ""); //added blank cell sparkline var dataspan = $("<span>"); dataspan.appendto(lastcell); dataspan.sparkline(amountdata, { type: 'bar', barcolor: '#191919' }); }); } populateoveralloverview([{ category: "transportation", currentmonthamt: 1, topmonthstr: 1, topamount: 1 }, { category: "healthcare", currentmonthamt: 1, topmonthstr: 1, topamount: 1 }]); });
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js"></script> </head> <body> <table id="firsttaboverall" class="table" style="font-size:13px"> <tr> <th>category</th> <th>current month revenue</th> <th colspan=2>best selling month</th> </tr> </table> </body> </html>
Comments
Post a Comment