ChartJS图表不调整大小

bgibtngc  于 2023-10-18  发布在  Chart.js
关注(0)|答案(1)|浏览(188)

我正在创建一个带有两个x轴和一个y轴的图表的程序,但我想让图表更小。
当我试着“宽度:50px”和“高度:50px”,它没有调整大小。我还添加了响应:但这并不管用有什么解决办法吗?
当前代码:
JS:

  1. const data = {
  2. //labels: ['Younger Than 18', '18-30', '31-45', 'Older than 45', 'Unknown'],
  3. datasets: [{
  4. label: 'Races/Ethnicities',
  5. data: [
  6. 20, 30, 30, 40, 50
  7. ],
  8. backgroundColor: 'rgba(255, 26, 104, 0.2)',
  9. borderColor: 'rgba(255, 26, 104, 1)',
  10. tension: 0.4,
  11. yAxisID: 'y'
  12. },
  13. {
  14. label: 'Ages',
  15. data: [
  16. 10, 20, 30, 40, 50
  17. ],
  18. backgroundColor: 'rgba(0, 26, 104, 0.2)',
  19. borderColor: 'rgba(0, 26, 104, 1)',
  20. tension: 0.4,
  21. }]
  22. };
  23. // config
  24. const config = {
  25. maintainAspectRatio: false,
  26. responsive: true,
  27. type: 'line',
  28. data,
  29. options: {
  30. scales: {
  31. y: {
  32. beginAtZero: true,
  33. type: 'linear',
  34. position: 'left'
  35. },
  36. x1: {
  37. labels: ['White (Non-Hispanic)', 'Black (Non-Hispanic)', 'Hispanic (Black Or White)', 'Asian', 'Unknown'],
  38. },
  39. x2: {
  40. position: 'top',
  41. labels: ['Younger than 18', '18-30', '31-45', 'Older than 45', 'Unknown'],
  42. },
  43. }
  44. }
  45. };
  46. // render init block
  47. const myChart = new Chart(
  48. document.getElementById('chart'),
  49. config
  50. );
  51. // Instantly assign Chart.js version
  52. const chartVersion = document.getElementById('chartVersion');
  53. chartVersion.innerText = Chart.version;

超文本标记语言:
<canvas id="chart"></canvas>
CSS:

  1. #chart {
  2. background: black;
  3. padding: 20px;
  4. border-radius: 30px;
  5. margin-bottom: 50px;
  6. width: 20px;
  7. height: 20px;
  8. }
6bc51xsx

6bc51xsx1#

在你的css样式中,需要将width和height改为max-widthmax-height
我自己试过这个代码,没问题。

  1. #chart {
  2. background: black;
  3. padding: 20px;
  4. border-radius: 30px;
  5. margin-bottom: 50px;
  6. max-width: 1000px;//change here to resize
  7. max-height: 500px;
  8. }

我用下面的js代码

  1. <script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js'></script>

democode

展开查看全部

相关问题