Chartjs:在饼图中只需要左填充和右填充

klr1opcd  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(206)

我在工作中有一个案例,我想使用饼图,为此我使用Chart.js(用react-chartjs-2库作为 Package 器)。
我希望在画布的左右两边都有填充,但由于某种原因,在这种情况下,我总是得到顶部填充,即使我专门键入top:有没有人知道为什么会发生这种情况,以及什么是一种只有左右填充的方法?

const options = {
  layout: {
    padding: {
      left: 100,
      right: 100,
      top: 0,
    }
  }
}

代码如下所示,同时添加了代码沙盒:
https://codesandbox.io/s/exciting-stallman-x18qku?file=/src/App.js:0-1120

import "./styles.css";
import { Box } from "@mui/material";
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js";
import { Pie } from "react-chartjs-2";

ChartJS.register(ArcElement, Tooltip, Legend);

export const data = {
  labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
  datasets: [
    {
      label: "# of Votes",
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        "rgba(255, 99, 132, 0.2)",
        "rgba(54, 162, 235, 0.2)",
        "rgba(255, 206, 86, 0.2)",
        "rgba(75, 192, 192, 0.2)",
        "rgba(153, 102, 255, 0.2)",
        "rgba(255, 159, 64, 0.2)"
      ],
      borderColor: [
        "rgba(255, 99, 132, 1)",
        "rgba(54, 162, 235, 1)",
        "rgba(255, 206, 86, 1)",
        "rgba(75, 192, 192, 1)",
        "rgba(153, 102, 255, 1)",
        "rgba(255, 159, 64, 1)"
      ],
      borderWidth: 1
    }
  ]
};

const options = {
  layout: {
    padding: {
      left: 100,
      right: 100,
      top: 0,
    }
  }
}

export default function App() {
  return (
    <Box>
      <Pie data={data} options={options} />
    </Box>
  );
}
6mzjoqzu

6mzjoqzu1#

将maintainAspectRatio设置为false。

const options = {
  maintainAspectRatio: false,
  layout: {
    padding: {
      left: 100,
      right: 100
    }
  }
}

相关问题