6

如何使用 ng2-charts 设置图表的宽度和高度?我正在制作条形图,就像 ng2-charts 网站上的演示一样。

public doughnutChartLabels:string[] = ['EMI', 'Car', 'Food', 'Fuel'];
data:number[] = [3350, 5450, 4100, 1000];
  public doughnutChartType:string = 'doughnut';
  options: { responsive: true, }
  colorsEmptyObject: Array<Color> = [{}];
  public datasets: any[] = [
  {
    data: this.data,
    backgroundColor: [
      "#FF6384",
      "#36A2EB",
      "#FFCE56"
    ],
    hoverBackgroundColor: [
      "#FF6384",
      "#36A2EB",
      "#FFCE56"
    ]
  }];
  // events
  public chartClicked(e:any):void {
    console.log(e);
  }

  public chartHovered(e:any):void {
    console.log(e);
  }
4

1 回答 1

11

对于矩形图形(线、条),您可以在 html中包含一个width和标记:height

<canvas baseChart width="2" height="1"
        [datasets]="chartData"
        [labels]="chartLabels"
        [options]="chartOptions"
        [colors]="chartColors"
        [legend]=true
        chartType=line></canvas>

宽度和高度设置纵横比,而不是实际大小。

可以对方形图形(甜甜圈、雷达、饼图、极坐标)执行此操作,但这没有多大意义。它将保持方形,但在侧面或顶部和底部有更大的填充。

相反,您可以在div包含图表的情况下设置所需的确切大小:

<div style="display: block; width: 200px; height: 200px;">
    <canvas baseChart
            [data]="doughnutChartData"
            [labels]="doughnutChartLabels"
            [chartType]="doughnutChartType"
            (chartHover)="chartHovered($event)"
            (chartClick)="chartClicked($event)"></canvas>
</div>
于 2017-05-31T14:34:45.383 回答