0

我正在使用 react-chart-js 中的水平条(https://github.com/reactchartjs/react-chartjs-2/blob/react16/example/src/charts/Horizo​​ntalBar.js),如下所示,

  const data = {
    label: ["graph"],
    datasets: [
      {
        label: "A",
        backgroundColor: "red",
        data: [50],
      },
      {
        label: "B",
        backgroundColor: "yellow",
        data: [10],
      },
    ],
  };

  const chartOptions = {
    scales: {
      xAxes: [
        {
          stacked: true,
          barPercentage: 0.2,
        },
      ],
      yAxes: [
        {
          stacked: true,
          barPercentage: 0.2,
        },
      ],
    },
  };

<div>
  <HorizontalBar data={data} options={chartOptions}/>
</div>

我尝试将高度直接应用于 div 或水平条以限制画布的放大,但它仍然可以解决。是否可以分别为图表的画布提供高度和宽度。

4

2 回答 2

2

尝试responsive: false在您的选项对象中设置。这应该根据文档使画布不会调整大小(https://www.chartjs.org/docs/latest/general/responsive.html#configuration-options

于 2021-02-02T13:31:50.680 回答
1

在我的情况下,设置 maintainAspectRatio:false以及在 div 上提供高度/宽度

<div style={{height:'100px',width:'200px'}}>
<HorizontalBar data={data} options={chartOptions}/>
</div>

有助于限制画布的可伸缩性。

相关来源:https ://www.chartjs.org/docs/latest/general/responsive.html#important-note

于 2021-02-07T02:33:05.277 回答