LineChart.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. "max-height": `${props.height}px`,
  44. ...props.styles
  45. }));
  46. const chartOptions = computed(() => ({
  47. responsive: true,
  48. maintainAspectRatio: false,
  49. resizeDelay: 10,
  50. ...props.options
  51. }));
  52. </script>
  53. <template>
  54. <Line
  55. :ref="`chart-${chartId}`"
  56. :options="chartOptions"
  57. :data="data"
  58. :chart-id="chartId"
  59. :dataset-id-key="datasetIdKey"
  60. :plugins="plugins"
  61. :css-classes="cssClasses"
  62. :style="chartStyles"
  63. :width="width"
  64. :height="height"
  65. />
  66. </template>