【问题标题】:Chart.js: How to set the height of the rows manuallyChart.js:如何手动设置行高
【发布时间】:2021-08-21 20:54:02
【问题描述】:

是否可以使行有点窄,我做了很多研究,但找不到与此相关的任何东西。请看截图,

这也是我正在使用的代码

条形图选项

  stackedChartOptions: ChartOptions = {
    responsive: true,
    scales: {
      xAxes: [{ stacked: true }],
      yAxes: [{ stacked: true }],
    }
  };
  stackedChartColors: any[] = [{ backgroundColor: ['#adcde1', '#3a77af'] }];

  stackedChartLabels: string[] = [];
  stackedChartData: ChartDataSets[] = [];

模板

   <canvas
      baseChart
      [chartType]="'horizontalBar'"
      [datasets]="stackedChartData"
      [colors]="stackedChartColors"
      [options]="stackedChartOptions"
      [labels]="stackedChartLabels"
  </canvas>

这里也是复制示例。 https://stackblitz.com/edit/ng2-charts-bar-template-si9pkc?file=src%2Fapp%2Fapp.component.html

所以我的问题是:

是否可以手动设置行高?任何帮助将不胜感激

更新:在示例中,行很窄,但不受我控制

【问题讨论】:

    标签: angular chart.js ng2-charts


    【解决方案1】:

    我假设您使用的是 chart.js v2。

    您可以使用barThickness调整条的大小:

    https://www.chartjs.org/docs/2.9.4/charts/bar.html#barthickness

    stackedChartData: ChartDataSets[] = [
        {
          label: 'Dataset 1',
          backgroundColor: '#adcde1',
          data: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
          barThickness: 10 // adjust bar size
        },
        {
          label: 'Dataset 2',
          backgroundColor: '#3a77af',
          data: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100].reverse(),
          barThickness: 10 // adjust bar size
        }
      ];
    

    演示代码框:

    https://stackblitz.com/edit/ng2-charts-bar-template-2eizu5?file=src%2Fapp%2Fapp.component.ts

    【讨论】:

    • 您好@MicFung,非常感谢您的回答,但我试过了,如果您尝试将10 更改为2030,所有项目将相互重叠,不确定我是否错过了什么?
    • @AshotAleqsanyan 它是一个从 0 到 1 的数字,但它只会减小条的宽度,而不是网格刻度部分的宽度
    • @AshotAleqsanyan 我不知道我是否明白你的意思。如果您想获得相对条形大小,如何使用barPercentage。更新了stackblitz,可以看到结果了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    相关资源
    最近更新 更多