Canvas stretching when using column-count 画布在使用 column-count 时会被拉伸。

huangapple go评论85阅读模式
英文:

Canvas stretching when using column-count

问题

以下是您要翻译的部分:

.container {
    display: flex;
}

.row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width: 100%;
    margin-bottom: 30px;
}

.column {
    display: flex;
    flex-direction: column;
    flex-basis: 100%;
    flex: 1;
    align-self: stretch;
    margin-right: 10px;
    margin-left: 10px;
}

container div {
    flex: 1;
}

canvas {
    max-width: 100%;
    display: flex;
}

.pie {
    column-count: 2;
    max-height: 93%;
}
<div class="container">
    <div class="row">
        <div class="pie">
            <canvas baseChart [data]="pieFeeChartData" [type]="pieChartType"
                [options]="pieFeeChartOptions">
            </canvas>
            <canvas baseChart [data]="pieFooChartData" [type]="pieChartType"
                [options]="pieFooChartOptions">
            </canvas>
        </div>
        <div class="column">
            <canvas baseChart [data]="barBlahChartData"
                [options]="barBlahChartOptions" [type]="barChartType">
            </canvas>
        </div>
    </div>
</div>

请注意,我已删除了HTML中的HTML转义字符(&lt;&quot;),以便更好地阅读。如果您需要HTML的转义版本,请告诉我,我可以提供。

英文:

I am using chart.js and I have two pie charts and a bar chart which I would like to display in a single row. I want the bar chart to be larger than the individual pie charts, so I did the following.

To get the two pie charts to fit into a single column I used column-count.

Here is the CSS which I am using

.container {
    display: flex;
}

.row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width: 100%;
    margin-bottom: 30px;
}

.column {
    display: flex;
    flex-direction: column;
    flex-basis: 100%;
    flex: 1;
    align-self: stretch;
    margin-right: 10px;
    margin-left: 10px;
}

.container div {
    flex: 1;
}

canvas {
    max-width: 100%;
    display: flex;
}


.pie {
    column-count: 2;
    max-height: 93%;
}

And the HTML

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
        &lt;div class=&quot;pie&quot;&gt;
            &lt;canvas baseChart [data]=&quot;pieFeeChartData&quot; [type]=&quot;pieChartType&quot;
                [options]=&quot;pieFeeChartOptions&quot;&gt;
            &lt;/canvas&gt;
            &lt;canvas baseChart [data]=&quot;pieFooChartData&quot; [type]=&quot;pieChartType&quot;
                [options]=&quot;pieFooChartOptions&quot;&gt;
            &lt;/canvas&gt;
        &lt;/div&gt;
        &lt;div class=&quot;column&quot;&gt;
            &lt;canvas baseChart [data]=&quot;barBlahChartData&quot;
                [options]=&quot;barBlahChartOptions&quot; [type]=&quot;barChartType&quot;&gt;
            &lt;/canvas&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

My best solution is to set max-height in .pie to 93%. It does give me close to what I want, but it is not exact and the formatting is inconsistent with smaller window sizes. It is not bad, but I am hoping to find a more precise solution without using a hard coded value. I have tried a lot of different flex settings, but I want to keep these CSS settings consistent throughout the project as much as possible, so that is not ideal either.

Here is a screenshot of what is happening when I DONT set max-height: 93%;

Canvas stretching when using column-count
画布在使用 column-count 时会被拉伸。

答案1

得分: 1

你可以通过使用网格来大大简化这个过程。这将使你的项目更具可预测性,更具可重用性,并且在项目不断发展时更容易理解。

此外,请注意 canvas 的高度值使用 !important 来覆盖 Chart.js 内联样式中应用的固定高度值。

.container {
  width: 90%;
  margin: auto;
}

.row {
  display: grid;
  /* 
    标准的12列网格可以均匀分为四分之一和三分之一。
  */
  grid-template-columns: repeat(12, 1fr);
}

.col-full {
  grid-column: span 12;
}

.col-half {
  grid-column: span 6;
}

.col-quarter {
  grid-column: span 3;
}

/* ✨ Magic ✨ */
canvas {
  max-width: 100%;
  height: 100% !important;
}

现在是HTML部分:

<div class="container">
  <div class="row">
    <div class="col-quarter">
      <canvas id="pieChart1"></canvas>
    </div>
    <div class="col-quarter">
      <canvas id="pieChart2"></canvas>
    </div>
    <div class="col-half">
      <canvas id="barChart"></canvas>
    </div>
  </div>
</div>

结果如下:
Canvas stretching when using column-count
画布在使用 column-count 时会被拉伸。
1: https://i.stack.imgur.com/UCJSh.png

英文:

You could greatly simplify this using grid. It will give you more predictable results and it will be more reusable in your project and it will be a lot easier to reason about as your project grows.

Also, notice how the canvas height value uses !important to override the fixed values of the height of your chart that are applied as inline styles by Chart.js.

.container {
  width: 90%;
  margin: auto;
}

.row {
  display: grid;
  /* 
    A standard 12 column grid can be evenly 
    divided into quarters and thirds.
  */
  grid-template-columns: repeat(12, 1fr);
}

.col-full {
  grid-column: span 12;
}

.col-half {
  grid-column: span 6;
}

.col-quarter {
  grid-column: span 3;
}

/* ✨ Magic ✨ */
canvas {
  max-width: 100%;
  height: 100% !important;
}

And now the HTML:

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-quarter&quot;&gt;
      &lt;canvas id=&quot;pieChart1&quot;&gt;&lt;/canvas&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-quarter&quot;&gt;
      &lt;canvas id=&quot;pieChart2&quot;&gt;&lt;/canvas&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-half&quot;&gt;
      &lt;canvas id=&quot;barChart&quot;&gt;&lt;/canvas&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

The result:
Canvas stretching when using column-count
画布在使用 column-count 时会被拉伸。

huangapple
  • 本文由 发表于 2023年2月14日 04:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定