javascript - 關(guān)于chartjs表格的大小問題
問題描述
我創(chuàng)建出來的表格總是和自定義的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)問哪里出錯(cuò)了嗎
問題解答
回答1:官方文檔中的說明:
// 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';
其中并沒有說畫布的定義方法為var ctx = $(’#chartTest’)[0].getContext(’2d’),試一下將第一行的var ctx = $(’#chartTest’)[0].getContext(’2d’)改為var ctx = document.getElementById(’charTest’)。
Update參見: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. angular.js - angular ng-class里面的引號(hào)問題2. css - 對(duì)于類選擇器使用的問題3. javascript - Web微信聊天輸入框解決方案4. docker - 如何修改運(yùn)行中容器的配置5. html - vue項(xiàng)目中用到了elementUI問題6. 網(wǎng)頁(yè)爬蟲 - 用Python3的requests庫(kù)模擬登陸B(tài)ilibili總是提示驗(yàn)證碼錯(cuò)誤怎么辦?7. javascript - Ajax加載Json時(shí),移動(dòng)端頁(yè)面向左上角縮小一截兒,加載完成后才正常顯示,這該如何解決?8. javascript - 有沒有什么好的圖片懶加載的插件,需要包含監(jiān)聽頁(yè)面滾動(dòng)高度,然后再加載的功能9. python - 為什么在main()函數(shù)里result 會(huì)變成那樣?10. mysql無(wú)法添加外鍵
