代码之家  ›  专栏  ›  技术社区  ›  AlleXyS

画布强制网格单元无限扩展

  •  0
  • AlleXyS  · 技术社区  · 4 年前

    ts码:

    templateStyle = {
          display: 'grid',
          'grid-template-columns': 'calc(25%-10px) calc(25%-10px) calc(25%-10px) calc(25%-10px)',
          'grid-template-rows': '150px auto auto',
          'grid-gap': '10px',
          border: '1px solid #2196F3',
          padding: '10px'
    }
    
    widgets = [
       { rowStart: 1, rowEnd: 2, colStart: 1, colEnd: 2, ... },
       { rowStart: 1, rowEnd: 2, colStart: 2, colEnd: 3, ... },
       ...
       { rowStart: 2, rowEnd: 3, colStart: 1, colEnd: 2, ... },
       ...
    ];
    
    getStyle(widget): object {
      return {    
          position: 'relative',
          'grid-row-start': widget.rowStart,
          'grid-row-end': widget.rowEnd,
          'grid-column-start': widget.colStart,
          'grid-column-end': widget.colEnd,
          'text-align': 'center',
          'vertical-align': 'middle',
          'min-height': card.height + 'px'
      };
    }
    

    <div [ngStyle]="templateStyle">
      <div *ngFor="let widget of widgets" [ngStyle]="getStyle(widget)">
         <button (click)="addChart()">Add</button>
         <div *ngIf="If chart added by above button action"
            style="width: 100%; height: 100%"> // widht/height 100% to give all space of the parent
           <canvas style="width: 200px; height: 200px;"
                 chartType="pie" 
                 [data]="[200, 100]"
                 [labels]="['A', 'B']"
                 baseChart>
           </canvas>
         </div>
      </div>
    </div>
    

    呃,那条线 style="width: 200px; height: 200px; 被画布忽略,这将强制网格单元在网格外无限增长。我认为发生这种情况是因为我使用百分比声明了网格列。如果我删除画布父样式( width: 100%, height: 100% ),单元格将增长网格中可用的空间。

    grid-template-columns': 'calc(25%-10px) calc(25%-10px) calc(25%-10px) calc(25%-10px) . 我也试着用 fr

    Initial grid widgets

    After adding the pie chart on row 2, column 1

    如果我找不到解决方法,我真的在考虑用d3图表代替chartjs。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Andrei    4 年前

    试试这个

    templateStyle = {
     ....
    'grid-template-columns': '1fr 1fr 1fr 1fr',
    'grid-gap': '10px',