简体   繁体   English

使用HTML中的数据显示ChartJS Donut图表

[英]Using data in HTML to display ChartJS Doughnut chart

I have a method where I am display products and when those products are filtered it shows X (amount that matches the filter) OF X (total number in that category) 我有一种显示产品的方法,当这些产品被过滤时,它会显示X(与过滤器匹配的金额)OF X(该类别中的总数)

I want a corresponding doughnut chart us ChartJS, I need it to use the data points which I currently have set in the html as... 我想要一个对应于我们ChartJS的甜甜圈图,我需要它来使用我目前在html中设置的数据点为...

<h4>
<strong>17</strong>
of 17            
</h4>

I was thinking od possibly wrapping these 2 numbers in a div and giving it a dataset but I am unsure how I would correspond this to ChartJS. 我在想可能将这两个数字包装在一个div中并为其提供数据集,但是我不确定如何将其与ChartJS对应。

You can see this my current progress here above the filters sidebar - http://bootsandlaces.net/products/footballs/ 您可以在过滤器侧栏上方看到我当前的进度-http: //bootsandlaces.net/products/footballs/

Can anyone help me ? 谁能帮我 ?

Well if you have to pull data from the DOM here is the possible solution (not the prettiest one, but it works... ). 好吧,如果您必须从DOM中提取数据,这是可能的解决方案(不是最漂亮的解决方案,但它可以工作...)。 Give the corresponding HTML elements ids eg amount and total. 给出相应的HTML元素ID,例如数量和总计。

var total = document.getElementById("total").innerText;
var amount = document.getElementById("amount").innerText / total * 100;
var left = 100 - amount;

if ( amount && total ) {
 data.datasets[0].data = [amount, left];
}

See this working fiddle 看到这个工作的小提琴

And do some styling... 并做一些造型...

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

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