ChartJS 如何使用单个Chart.vue组件而不是不同的图表组件,然后通过更改API的ID来显示图表

rslzwgfq  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(225)

我显示了四个图表,因此我创建了四个不同的图表组件,但所有组件都相同,只是API id不同。
为了使代码更有效,我想制作一个Chart.vue组件,然后在任何其他组件中按各自的ID显示图表。
下面是我的一个图表组件:

  1. <template>
  2. <div class="chart-container" style="position: relative; height: 25vh; width:100%;">
  3. <canvas id="DisplayChart" ></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. import moment from 'moment'
  8. export default {
  9. name: 'Chart',
  10. async mounted () {
  11. await this.$http.get('/farfrom/chart/3') //Here 3 is the ID of the chart.
  12. .then((response) => {
  13. const result = response.data
  14. const ctx = document.getElementById('DisplayChart').getContext('2d')
  15. const Chart_data = []
  16. for (let i = 0; i < result.date.length; i++) {
  17. Chart_data.push({
  18. x_axis: moment(result.date[i], 'X').toDate(),
  19. y_axis: result.challenge[i]
  20. })
  21. }
  22. let myChart
  23. if (myChart !== undefined) {
  24. myChart.destroy()
  25. }
  26. myChart = new Chart(ctx, {
  27. type: 'line',
  28. data: {
  29. datasets: [
  30. {
  31. label: 'Chart_from_API',
  32. data: Chart_data,
  33. ]
  34. },
  35. options: {
  36. lineTension: 0,
  37. maintainAspectRatio: false,
  38. scales: {
  39. yAxes: [
  40. {
  41. scaleLabel: {
  42. display: false
  43. },
  44. ticks: {
  45. beginAtZero: true,
  46. // eslint-disable-next-line no-unused-vars
  47. callback (value) {
  48. return `${value }k` // y-axis value will append k to it
  49. }
  50. }
  51. }
  52. ],
  53. xAxes: [
  54. {
  55. type: 'time',
  56. time: {
  57. unit: 'month'
  58. },
  59. scaleLabel: {
  60. display: true
  61. }
  62. }
  63. ]
  64. }
  65. }
  66. })
  67. })
  68. .catch((error) => {
  69. console.log(error)
  70. })
  71. }
  72. }
  73. </script>

我已经在另一个组件中导入了此内容,如:

  1. <template>
  2. <div class="chart-container">
  3. <Chart></Chart>
  4. </div>
  5. </template>
  6. <script>
  7. import Chart from 'Chart.vue'
  8. ....
  9. </script>

这里我展示了我的一个图表的例子在4个图表中,我的其他图表也是相同的,只是在API的ID的变化。
现在,正如我所说,我想知道我如何可以使一个单一的图表。vue与一些变量的API ID,并使用它在任何组件各自的ID。请帮助我在这一点上。
目标:我想有一个单一的Chart.vue组件,而不是4个图表,如果我想使用任何图表,我应该能够使用各自的ID。

xienkqul

xienkqul1#

您可以使用props将数据传递给子组件。
https://v2.vuejs.org/v2/guide/components-props.html

示例

  • 应用程序版本 *
  1. <template>
  2. <div id="app">
  3. <Chart :id="chartId" />
  4. </div>
  5. </template>
  6. <script>
  7. import Chart from "@/components/Chart.vue";
  8. // @/ means src/
  9. export default {
  10. name: "App",
  11. components: {
  12. Chart,
  13. },
  14. data: () => ({
  15. chartId: 2,
  16. }),
  17. };
  18. </script>
  • 图表值 *
  1. <template>
  2. <div>
  3. {{ id }}
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "Chart",
  9. props: {
  10. id: {
  11. type: Number,
  12. required: true,
  13. },
  14. },
  15. mounted() {
  16. const apiURL = `/farfrom/chart/${this.id}`;
  17. // API call
  18. }
  19. };
  20. </script>
展开查看全部

相关问题