ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## **使用示例** > 参考文档:[Chart.js中文网]([http://chartjs.cn/](http://chartjs.cn/)) > 版本:2.8.0 > 代码案例来自:[https://blog.csdn.net/json\_vip/article/details/93320580](https://blog.csdn.net/json_vip/article/details/93320580) ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Chart.js</title> <script src="every_js/Require.js" type="text/javascript" charset="utf-8"></script> <script src="every_js/Every.js" type="text/javascript" charset="utf-8"></script> </head> <body> <canvas id="chart"></canvas> </body> <script type="text/javascript"> var chart; Every.use(['chart'],function(c){ chart = c; renderChart(); }); var dataLabels = ['1h', '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', '10h', '11h', '12h', '13h', '14h', '15h', '16h', '17h', '18h', '19h', '20h', '21h', '22h', '23h', '0h' ]; var dataPV = [133058, 253219, 255194, 233058, 253219, 277318, 277714, 273337, 255194, 277318, 277714, 273337, 233058, 253219, 277318, 253219, 277318, 277714, 273337, 255194, 277714, 273337, 255194, 293058 ]; var dataUV = [10651, 22039, 23955, 23754, 22664, 10651, 22039, 23765, 23955, 23754, 22664, 23765, 23955, 23754, 22664, 10651, 22039, 23765, 10651, 22039, 23765, 23955, 23754, 22664 ]; var config = { type: 'line', data: { labels: dataLabels, datasets: [{ label: 'PV', data: dataPV, backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', fill: false, }, { label: 'UV', data: dataUV, backgroundColor: 'rgb(75, 192, 192)', borderColor: 'rgb(75, 192, 192)', fill: false, } ] }, options: { responsive: true, title: { display: true, text: 'PV/UV 实时统计' }, } }; function renderChart(){ var ctx = document.getElementById('chart').getContext('2d'); var chart = new Chart(ctx, config); setInterval(function() { if (config.data.datasets.length > 0) { var last = parseInt(dataLabels[dataLabels.length - 1]); var label = last + 1; if (last >= 23) { label = 0; } label = label + 'h'; dataLabels.push(label); dataPV.push(getRandomNum(200000, 300000)); dataUV.push(getRandomNum(10000, 80000)); dataLabels.shift(); dataPV.shift(); dataUV.shift(); chart.update(); } }, 1000); } function getRandomNum(min, max) { var range = max - min; var rand = Math.random(); return (min + Math.round(rand * range)); } </script> </html> ```