如何在vue-chartjs中使用ChartOptions与Composition API

uqdfh47h  于 2023-08-07  发布在  Vue.js
关注(0)|答案(2)|浏览(187)

我尝试使用vue-chartjs,但使用composition API实现它。大多数例子要么是我不熟悉的typescript,要么是使用选项API。我无法让ChartOptions工作。我不确定我是否应该删除图表选项,只做选项,或者如果有其他东西,我错过了,如进口。我尝试在chart-js import语句中导入ChartOptions,但它会出现错误。任何关于如何实现这一点的帮助都将是非常有帮助的。谢谢你,谢谢

  1. <template>
  2. <Pie
  3. :chart-options="chartOptions"
  4. :chart-data="chartData"
  5. :chart-id="chartId"
  6. :dataset-id-key="datasetIdKey"
  7. :plugins="plugins"
  8. :css-classes="cssClasses"
  9. :styles="styles"
  10. :width="width"
  11. :height="height"
  12. />
  13. </template>
  14. <script>
  15. import {ref, defineComponent, onMounted} from 'vue'
  16. import {Pie} from 'vue-chartjs'
  17. import {Chart as ChartJS, Title, Tooltip, Legend, ArcElement, CategoryScale} from 'chart.js'
  18. ChartJS.register(Title, Tooltip, Legend, ArcElement, CategoryScale)
  19. export default defineComponent({
  20. name: 'SectorPieChart',
  21. components: { Pie },
  22. props: {
  23. chartId: {
  24. type: String,
  25. default: 'pie-chart'
  26. },
  27. datasetIdKey: {
  28. type: String,
  29. default: 'label'
  30. },
  31. width: {
  32. type: Number,
  33. default: 500
  34. },
  35. height: {
  36. type: Number,
  37. default: 500
  38. },
  39. cssClasses: {
  40. default: '',
  41. type: String
  42. },
  43. styles: {
  44. type: Object,
  45. default: () => {}
  46. },
  47. plugins: {
  48. type: Object,
  49. default: () => {}
  50. }
  51. },
  52. setup() {
  53. //stores
  54. const portfolioStore = usePortfolioStore()
  55. const {portfolio} = storeToRefs(portfolioStore)
  56. //dataset
  57. const chartData = ref({
  58. labels: [ 'Basic Materials', 'Consumer Cyclical', 'Financial Services', 'Real Estate', 'Consumer Defensive', 'Healthcare', 'Utilities', 'Communication Services', 'Energy', 'Industrials', 'Technology'],
  59. datasets: [
  60. {
  61. backgroundColor: ['#FF4A4A','#FFAC4A','#FFE9C9','#F9C87C','#F97432','#7a7979','#FFCC00','#FF9900','#86370e','#FFFF66','#ed9e67'],
  62. data: [1,1,1,1,1,1,1,1,1,1,1]
  63. },
  64. {
  65. backgroundColor: ['#FF4A4A','#FFAC4A','#FFE9C9','#F9C87C','#F97432','#7a7979','#FFCC00','#FF9900','#86370e','#FFFF66','#ed9e67'],
  66. data: [1,1,1,1,1,1,1,1,1,1,1]
  67. }
  68. ]
  69. })
  70. //chart options to change settings
  71. const chartOptions = ref({
  72. responsive: true,
  73. maintainAspectRatio: true,
  74. legend: {
  75. display: false,
  76. }
  77. })
  78. //methods
  79. const loadData = () => {
  80. }
  81. //add on mount API request
  82. onMounted(() => {
  83. loadData()
  84. })
  85. return {
  86. chartData, chartOptions, loadData
  87. }
  88. }
  89. })
  90. </script>

字符串

wr98u20j

wr98u20j1#

根据vue-chartjs,你不能导入ChartOptions对象/函数。您只需创建自己的选项数组,并将其绑定到:chart-options prop,这与您已经在做的事情完全相同。
为了正确设置图表选项,您应该遵循chart.js documentation。正如那里的文档所指出的,图例选项的命名空间为options.plugins.legend,并且包含可以关闭图例显示的布尔属性display。这意味着我们应该像这样格式化图表选项:

  1. const chartOptions = ref({
  2. responsive: true,
  3. maintainAspectRatio: true,
  4. plugins: {
  5. legend: {
  6. display: false,
  7. },
  8. },
  9. });

字符串
codesandbox在这里。

p8ekf7hl

p8ekf7hl2#

使您的图表计算选项:

  1. const chartOptions = computed(() => {
  2. return ({
  3. maintainAspectRatio: true,
  4. plugins: {
  5. legend: {
  6. display: false,
  7. },
  8. },
  9. },
  10. })
  11. })

字符串
就这样...

相关问题