如何防止ChartJs挤压flexbox绝对窗格?

esbemjvw  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(232)

下面的代码是实际代码的简化版本,但它“正确地”显示了问题--我有一个T型布局的窗口。顶部有工具栏,左下方有一些长的内容,右边应该占用剩余的空间。
我使用“绝对”位置为左窗格,因为我的第一个问题是如何滚动窗格内的长内容,这是我发现的,所以我使用它。
在右边有ChartJs,最初它确实占用了剩余的空间,但如果我将鼠标移到它上面,它会刷新内容,导致左窗格被压成零。
如何防止这种情况(不使用硬编码大小,如“min-width:200 px”)?随着左右窗格大小的变化,我的意图是“左--取所有必要的,右--取其余的”。
代码:

  1. <!DOCTYPE html>
  2. <head>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>
  4. </head>
  5. <body style="background-color: pink; overflow:hidden;margin:0" onload="starter()">
  6. <div style="display: flex;flex-direction:column; height: 100vh;width: 100%">
  7. <!-- TOOLBAR -->
  8. <div style="flex-shrink: 0; flex-grow: 0;">
  9. <button>hello</button>
  10. </div>
  11. <div style="flex-shrink: 0; flex-grow: 1;
  12. display: flex;flex-direction:row; ">
  13. <!-- LEFT PANE -->
  14. <div style="position:relative; background-color: aqua; overflow:scroll;
  15. flex-shrink: 0; flex-grow: 1;">
  16. <div style="position:absolute; width: 100%">
  17. <div>
  18. <h1>
  19. <li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>4</li>
  20. <li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>1</li>
  21. <li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>4</li>
  22. <li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>0</li>
  23. </ul>
  24. </h1>
  25. </div>
  26. <div>left</div>
  27. </div>
  28. </div>
  29. <!-- RIGHT PANE -->
  30. <div style="background-color: green; flex-shrink: 0; flex-grow: 1;
  31. display: flex;flex-direction:column;">
  32. <div style="position: relative;flex-shrink: 1; flex-grow: 1;">
  33. <canvas id="bar-chart" style="" ></canvas>
  34. </div>
  35. <div style="flex-shrink: 0; flex-grow: 0;">right</div>
  36. </div>
  37. </div>
  38. </div>
  39. <script>
  40. function starter(){
  41. // https://tobiasahlin.com/blog/chartjs-charts-to-get-you-started/
  42. new Chart(document.getElementById("bar-chart"), {
  43. type: 'bar',
  44. data: {
  45. labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
  46. datasets: [
  47. {
  48. label: "Population (millions)",
  49. backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
  50. data: [2478,5267,734,784,433]
  51. }]
  52. },
  53. options: {
  54. maintainAspectRatio : false,
  55. legend: { display: false },
  56. title: {
  57. display: true,
  58. text: 'Predicted world population (millions) in 2050'
  59. }
  60. }
  61. });
  62. }
  63. </script>
  64. </body>
  65. </html>
ohfgkhjo

ohfgkhjo1#

所以我从最初的问题开始--如何使用剩余空间并在那里启用滚动?对于两个窗格,Flex布局有很好的解决方案:https://stackoverflow.com/a/68516470/210342,但在我的例子中,当我有嵌套的窗格时,我不能使flex占用整个窗口和仅窗口空间(如果不使用绝对位置的hack)。
因为我不知道CSS,我尝试了网格方法,它工作的权利现场。我只是添加了“高度:100 vh”为整个网格容器和给定的滚动窗格“溢出:自动”,这就是它。
现在我有滚动和大小良好的图表。
不错的网格发电机我用:https://cssgrid-generator.netlify.app/

相关问题