我正在尝试创建一个图表并将其动态添加到DOM。图表的上下文是在使用数据表创建时动态创建的。
这是我尝试过的:
function format(row_data) {
var html = '<div class="row justify-content-center">';
for (let i = 0; i < row_data.items.length; i++) {
var item = row_data.items[i];
var correct = '';
if (i == 0) {
correct = 'text-success';
}
html += `<div class="col-md-5 col-sm-6 col-lg-4 mb-5">
<div class="card border-0 shadow ">
<div class="card-header border-0">
<h6 class="font-weight-bold ${correct}">${item.supplier_name}</h6>
</div>
<div class="card-body row">
<div class="col-6 " data-toggle="tooltip" data-title="Quantity Purchased">
<small>Qty:</small>
<span class="font-weight-bold">${item.qty}</span>
</div>
<div class="col-6 text-right" data-toggle="tooltip" data-title="Purchase Rate">
<div class="font-weight-bold">₹ ${item.avg_purchase_rate.toFixed(2)}</div>
<small>Rate</small>
</div>
<div class="col-6" data-toggle="tooltip" data-toggle="tooltip" data-title='Total Due of this supplier (<i class="fa fa-rupee-sign"></i>)'>
<div class="font-weight-bold" >${(item.opning_balance + item.total_purchase_amount - item.total_purchase_return - item.total_payment).toFixed(2)}</div>
<small>Due Amount</small>
</div>
<div class="col-6 text-right pt-3" data-toggle="tooltip" data-title="You have purchased ${item.count} times from this supplier">
<span class="font-weight-bold">${item.count}</span>
<small>Times</small>
</div>
<canvas id="chart_${i}"></canvas>
</div>
</div>
</div>`;
var myChart = new Chart($(html).find('#chart_' + i), {
type: 'bar',
data: {
labels: item.sequence_qty,
datasets: [{
label: '# of Votes',
data: item.sequence_purchase_rate,
backgroundColor: ['rgba(255, 255, 255, 0.2)'],
borderColor: ['#999'],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
html += '</div>';
return html;
}
以上,format()
函数返回所需的HTML。稍后我将使用数据表child.row
函数将其附加。
我可以log
动态创建的图表,但是如何显示/渲染图表。