简体   繁体   English

通过循环为图表js生成多个图表

[英]generate multiple charts via loop for chart js

I am trying to only have table tag, but have js generate the entire table.我试图只有表格标签,但让 js 生成整个表格。 So I use createElement for headers, but within tbody.innerHTML, I am string concatenating string interpolated html that contains tr including canvas inside of a td.所以我使用 createElement 作为标题,但在 tbody.innerHTML 中,我是字符串连接字符串插值 html,其中包含 tr,包括 td 内的 canvas。

I am dynamically generating this and within the for loop, I try rendering a row, then have another function to create generic chart but fit to specific id of canvas.我是动态生成的,在 for 循环中,我尝试渲染一行,然后有另一个 function 来创建通用图表,但适合 canvas 的特定 ID。 Ex: bar_1, bar_2, ... I am also using getElementById for new Chart(ctx)例如:bar_1,bar_2,...我也将 getElementById 用于 new Chart(ctx)

So basically for each row, I want a chart in a td.所以基本上对于每一行,我想要一个 td 中的图表。 The issue is, it only shows me 1 chart and it is for the last row only when it should show up for all rows.问题是,它只向我显示 1 个图表,并且仅当它应该显示在所有行时才用于最后一行。 Which makes me suspect selection by getElementId, but I tried querySelectorAll() id specification, querySelector(#) as well but no luck.这让我怀疑 getElementId 的选择,但我尝试了 querySelectorAll() id 规范,也尝试了 querySelector(#) 但没有运气。

----bar.js---- snippet ----bar.js---- 片段

let body = ``;
for(var item of productList){

body+=`<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.date}</td>
<td><table>`;
for(var props of item.items){
    let currency = props.units==="USD"? "$":""; 
    body+=`<tr>
    <td>${props.item_number}) ${currency}${props.price}</td>
    </tr>`;
}
body+=`</table></td>
<td>
    <div style="border: 2px solid blue;">
    <canvas id="bar_${item.id}"></canvas>
    </div>
</td></tr>`;
tbody.innerHTML += body;

renderChart("bar_"+item.id, item.items, item.name);
body=``;
}
...
function renderChart(id,...){
let config = ... //generic chart config
new Chart(document.getElementById(id),config)
}

The issue is, it only shows me 1 chart and it is for the last row only when it should show up for all rows.问题是,它只向我显示 1 个图表,并且仅当它应该显示在所有行时才用于最后一行。

body=''; line 23第 23 行

This line removes all charts created and searched by the renderChart function.此行删除所有由renderChart function 创建和搜索的图表。 You might want to either remove this line, otherwise the DOMElement you are trying to access will be removed by the time it's accessed, or use const element = document.createElement('DIV') and append your element to the body and then pass the reference to that object to renderChart(element) to avoid ID generation您可能想要删除此行,否则您尝试访问的 DOMElement 将在访问时被删除,或者使用const element = document.createElement('DIV')和 append 将您的元素传递给正文,然后将参考该 object 到renderChart(element)以避免生成 ID

I figured out issue was with async events I didn't know about were happening.我发现问题在于我不知道正在发生的异步事件。 Only time chart js won't let you create multiple charts is on the same canvas or if it is previously occupied until you destroy it.只有时间图表 js 不会让您创建多个图表在同一个 canvas 上,或者如果它以前被占用,直到您销毁它。

let id = "bar_"+item.id;
setTimeout(()=>{renderChart(id, item.items, item.name);},100)

I did something like this within the above snippet.我在上面的代码片段中做了类似的事情。 Issue was the extracting of string concat of id was slower than getElementById, which made getElementById get same one.问题是 id 的字符串 concat 的提取比 getElementById 慢,这使得 getElementById 得到相同的。 However I was still getting diff canvas elements on console.log testing, so I don't 100% understand how solving one issue solved issue of the actual new Chart being created for each unique canvas.但是,我仍然在 console.log 测试中得到了不同的 canvas 元素,所以我不能 100% 理解解决一个问题如何解决为每个独特的 canvas 创建的实际新图表的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM