移动的视图+ ChartJS v4中的水平条形图上的Y轴标签被切断

pkwftd7m  于 2023-04-20  发布在  Chart.js
关注(0)|答案(1)|浏览(296)

我有一个带Vue 3的Nuxt 3应用程序。我正在使用复合API和typescript。在我的package.json文件中,我有以下依赖项:

  1. {
  2. ...
  3. "dependencies": {
  4. "chart.js": "^4.2.1",
  5. "vue-chartjs": "^5.2.0",
  6. }
  7. ...
  8. }

当图表渲染时,我的y轴标签在移动的视图中被切断。请参见下图。

我的vue组件代码如下:
MovieHorizontalBarChart.vue

  1. <template>
  2. <div class="chart-container">
  3. <ClientOnly>
  4. <Bar :data="data" :options="chartOptions" />
  5. </ClientOnly>
  6. </div>
  7. </template>
  8. <script setup lang="ts">
  9. import {
  10. Chart as ChartJS,
  11. Title,
  12. Tooltip,
  13. Legend,
  14. BarElement,
  15. CategoryScale,
  16. LinearScale,
  17. ChartOptions,
  18. } from "chart.js";
  19. import { Bar } from "vue-chartjs";
  20. ChartJS.register(
  21. CategoryScale,
  22. LinearScale,
  23. BarElement,
  24. Title,
  25. Tooltip,
  26. Legend
  27. );
  28. const data = {
  29. labels: ["Label 1", "Very very very very long name that is cut off"],
  30. datasets: [
  31. {
  32. axis: "y",
  33. label: "Count",
  34. backgroundColor: "#000",
  35. data: [10, 24],
  36. },
  37. ],
  38. };
  39. const chartOptions: ChartOptions<"bar"> = {
  40. responsive: true,
  41. maintainAspectRatio: false,
  42. indexAxis: "y",
  43. plugins: {
  44. title: {
  45. display: true,
  46. text: "Horizontal Bar chart by ChartJS",
  47. font: { size: 14 },
  48. },
  49. },
  50. };
  51. </script>
  52. <style lang="scss" scoped>
  53. .chart-container {
  54. position: relative;
  55. height: 25vh;
  56. }
  57. </style>

问题:Very very very very long name that is cut off标签被切断。我如何让我的y轴标 checkout 现在多行上?或者有任何其他方法可以避免标签被切断?
注意:标签是动态的。我不知道每个标签会有多长。每个新的外部API请求都会生成新的值。我的实际标签代码如下:

  1. const data = {
  2. labels: props.movies.map((movie) => movie.name),
  3. datasets: [
  4. {
  5. axis: "y",
  6. label: "Count",
  7. backgroundColor: "#000",
  8. data: props.movies.map((movies) => movie.count),
  9. },
  10. ],
  11. };
dgiusagp

dgiusagp1#

您可以将标签设置为string或string[]的数组。
参见文档:https://www.chartjs.org/docs/latest/axes/cartesian/category.html#category-axis-specific-options
在您的案例中:

  1. const data = {
  2. labels: ["Label 1", ["Very very very", "very long name", "that is cut off"]],
  3. datasets: [
  4. {
  5. axis: "y",
  6. label: "Count",
  7. backgroundColor: "#000",
  8. data: [10, 24],
  9. },
  10. ],
  11. };

相关问题