LineChart.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <script setup lang="ts">
  2. import { PropType, computed } from "vue";
  3. import { Line } from "vue-chartjs";
  4. import {
  5. Chart as ChartJS,
  6. Title,
  7. Tooltip,
  8. Legend,
  9. LineElement,
  10. PointElement,
  11. CategoryScale,
  12. LinearScale,
  13. LineController,
  14. Plugin
  15. } from "chart.js";
  16. ChartJS.register(
  17. Title,
  18. Tooltip,
  19. Legend,
  20. LineElement,
  21. PointElement,
  22. CategoryScale,
  23. LinearScale,
  24. LineController
  25. );
  26. const props = defineProps({
  27. chartId: { type: String, default: "line-chart" },
  28. datasetIdKey: { type: String, default: "label" },
  29. width: { type: Number, default: 200 },
  30. height: { type: Number, default: 200 },
  31. cssClasses: { default: "", type: String },
  32. styles: {
  33. type: Object as PropType<Partial<CSSStyleDeclaration>>,
  34. default: () => {}
  35. },
  36. plugins: { type: Object as PropType<Plugin<"line">[]>, default: () => {} },
  37. data: { type: Object as PropType<any>, default: () => {} },
  38. options: { type: Object, default: () => {} }
  39. });
  40. const chartStyles = computed(() => ({
  41. position: "relative",
  42. height: `${props.height}px`,
  43. ...props.styles
  44. }));
  45. const chartOptions = computed(() => ({
  46. responsive: true,
  47. maintainAspectRatio: false,
  48. resizeDelay: 10,
  49. ...props.options
  50. }));
  51. </script>
  52. <template>
  53. <Line
  54. :ref="`chart-${chartId}`"
  55. :chart-options="chartOptions"
  56. :chart-data="data"
  57. :chart-id="chartId"
  58. :dataset-id-key="datasetIdKey"
  59. :plugins="plugins"
  60. :css-classes="cssClasses"
  61. :styles="chartStyles"
  62. :width="width"
  63. :height="height"
  64. />
  65. </template>