javascript - 關(guān)于chartjs表格的大小問(wèn)題
問(wèn)題描述
我創(chuàng)建出來(lái)的表格總是和自定義的canvas大小無(wú)關(guān)呢
//html<canvas height='300'></canvas>
//js(這就是官網(wǎng)的示例)var ctx = $(’#chartTest’)[0].getContext(’2d’);var chart = new Chart(ctx,{ type: ’bar’, data: {labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],datasets: [{ label: ’# of Votes’, data: [12, 19, 3, 5, 2, 3], backgroundColor: [’rgba(255, 99, 132, 0.2)’,’rgba(54, 162, 235, 0.2)’,’rgba(255, 206, 86, 0.2)’,’rgba(75, 192, 192, 0.2)’,’rgba(153, 102, 255, 0.2)’,’rgba(255, 159, 64, 0.2)’ ], borderColor: [’rgba(255,99,132,1)’,’rgba(54, 162, 235, 1)’,’rgba(255, 206, 86, 1)’,’rgba(75, 192, 192, 1)’,’rgba(153, 102, 255, 1)’,’rgba(255, 159, 64, 1)’ ], borderWidth: 1}] }, options: {scales: { yAxes: [{ticks: { beginAtZero:true} }]} }})
圖表顯示了,但屬性卻是這樣的:

canvas的寬高和我自定義的完全無(wú)關(guān)呢請(qǐng)問(wèn)哪里出錯(cuò)了嗎
問(wèn)題解答
回答1:官方文檔中的說(shuō)明:
// Any of the following formats may be usedvar ctx = document.getElementById('myChart');var ctx = document.getElementById('myChart').getContext('2d');var ctx = $('#myChart');var ctx = 'myChart';
其中并沒(méi)有說(shuō)畫(huà)布的定義方法為var ctx = $(’#chartTest’)[0].getContext(’2d’),試一下將第一行的var ctx = $(’#chartTest’)[0].getContext(’2d’)改為var ctx = document.getElementById(’charTest’)。
Update參見(jiàn):http://www.chartjs.org/docs/l...
寬高檢測(cè)不能直接從canvas獲取,需要在外部嵌套一個(gè)p,設(shè)置p樣式:
#chart-wrapper { position: relative; // 這個(gè)必須要有,否則里面會(huì)生成的iframe絕對(duì)定位,會(huì)以外層第一個(gè)有定位的元素的坐標(biāo)系為準(zhǔn) width: 400px; height: 400px;}
html:
<p id='chart-wrapper'> <canvas id='myChart'></canvas></p>
相關(guān)文章:
1. golang - 用IDE看docker源碼時(shí)的小問(wèn)題2. docker-machine添加一個(gè)已有的docker主機(jī)問(wèn)題3. docker images顯示的鏡像過(guò)多,狗眼被亮瞎了,怎么辦?4. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?5. docker - 如何修改運(yùn)行中容器的配置6. phpstudy8.1沒(méi)集成mysql-front7. 我何時(shí)應(yīng)該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)8. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!9. 前端 - @media query 使用出現(xiàn)的問(wèn)題?10. html5和Flash對(duì)抗是什么情況?

網(wǎng)公網(wǎng)安備