国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 關(guān)于chartjs表格的大小問題

瀏覽:146日期:2023-03-25 14:06:26

問題描述

我創(chuàng)建出來的表格總是和自定義的canvas大小無關(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} }]} }})

圖表顯示了,但屬性卻是這樣的:

javascript - 關(guān)于chartjs表格的大小問題

canvas的寬高和我自定義的完全無關(guān)呢請問哪里出錯(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...

寬高檢測不能直接從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>

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 无锡市| 北京市| 仙桃市| 桐梓县| 平和县| 涪陵区| 昌图县| 韶关市| 丹东市| 米泉市| 梧州市| 阳信县| 榆中县| 镶黄旗| 龙海市| 长阳| 登封市| 通海县| 库尔勒市| 西城区| 宁陕县| 大安市| 孝感市| 沛县| 龙海市| 桃江县| 离岛区| 和静县| 永仁县| 环江| 丽水市| 平原县| 商丘市| 垦利县| 永新县| 云龙县| 林口县| 宜昌市| 开平市| 土默特右旗| 广昌县|