【问题标题】:ChartJS: problem with height when data size is too smallChartJS:当数据太小时高度问题
【发布时间】:2022-10-06 03:20:43
【问题描述】:

我正在使用 ChartJS:3.9.1 + react-chartjs-2:4.3.1 我有一个问题,当数据集太小(2-3 个值)时 - 条之间的空间太大(见图):

当数据集有足够的值时,这个问题是不可重现的:

我的数据集:

barChartData = {
  labels: labels,
  datasets: [
    {
      data: values,
      backgroundColor: \'#F5CB3E\',
      // barThickness: 17,
      borderRadius: 5,
      categoryPercentage: 1.0,
      maxBarThickness: 18,
      barPercentage: 0.5,
      minBarLength: 1,
      yAxisID: \'y\',
    },
  ],
};

我的选择:

 {
maintainAspectRatio: false,
responsive: true,
indexAxis: \'y\',
plugins: {
  legend: {
    display: false,
    position: \'top\' as const,
  },
  title: {
    display: false,
  },
  tooltip: {
    enabled: false,
  },
  datalabels: {
    display: true,
    color: \'#f2f2f2\',
    formatter: Math.round,
    anchor: \'end\',
    align: function (context: DataSetContext) {
      const data = context.dataset.data[context.dataIndex];
      if (data < 10) {
        return \'start\';
      } else {
        return \'end\';
      }
    },
    offset: function (context: DataSetContext) {
      return context.dataset.data[context.dataIndex] < 10 ? -20 : 3;
    },
    clamp: true,
    font: {
      size: 14,
      weight: 600,
    },
  },
},
scales: {
  x: {
    stacked: true,
    grid: {
      display: false,
      drawBorder: false,
    },
    ticks: {
      font: { size: 13 },
      color: \'#f0f0f0\',
      display: false,
    },
    suggestedMax: (1 + 0.2) * configurableOptions.xAxisMaxValue,
  },
  y: {
    ticks: {
      beginAtZero: false,
      color: \'#f0f0f0\',
      font: {
        size: 14,
        weight: 500,
      },
    },
    grid: {
      drawBorder: false,
      display: false,
    },
  },
},

};

我与 barPercentage 和 categoryPercentage 共舞,但没有任何改变。似乎文档不包含我的问题的答案。先感谢您!

    标签: javascript charts chart.js bar-chart


    【解决方案1】:

    您在数据集中设置属性maxBarThickness,这会禁止栏变宽。

    您需要删除此属性或更改画布的大小,以使您的条形越来越靠近彼此。

    【讨论】:

    • 删除了 maxBarThickness,条变成了卷绕器,但是我怎样才能改变画布的高度?所以钢筋总是相同的厚度并且它们之间总是有相同的距离?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2021-06-09
    相关资源
    最近更新 更多