|
| 1 | +<script setup lang="ts"> |
| 2 | + import { GraphWidgetData } from "@/types"; |
| 3 | + import { DashboardWidgetProps } from "@/dashboard/types"; |
| 4 | + import { VisXYContainer, VisAxis, VisLine, VisTooltip, VisCrosshair, VisScatter, VisArea } from "@unovis/vue"; |
| 5 | + import { |
| 6 | + AxisConfigInterface, BulletLegendConfigInterface, |
| 7 | + CrosshairConfigInterface, |
| 8 | + CurveType, |
| 9 | + LineConfigInterface, ScatterConfigInterface, XYContainerConfigInterface, |
| 10 | + Scale, TextAlign, FitMode, Scatter, AreaConfigInterface, |
| 11 | + } from "@unovis/ts"; |
| 12 | + import { Datum, useXYChart } from "@/dashboard/components/widgets/graph/useXYChart"; |
| 13 | + import { ChartContainer, ChartLegendContent, ChartTooltip, ChartCrosshair } from "@/components/ui/chart"; |
| 14 | + import { computed } from "vue"; |
| 15 | +
|
| 16 | + const props = defineProps<DashboardWidgetProps<GraphWidgetData>>(); |
| 17 | +
|
| 18 | + const { data, x, y, color, tooltipTemplate, xScale, chartConfig, xAxisConfig, yAxisConfig } = useXYChart(props); |
| 19 | +
|
| 20 | + const svgDefs = computed(() => props.value?.datasets.map((dataset, i) => ` |
| 21 | + <linearGradient id="fill-${i}" x1="0" y1="0" x2="0" y2="1"> |
| 22 | + <stop |
| 23 | + offset="5%" |
| 24 | + stop-color="${dataset.color}" |
| 25 | + stop-opacity="0.8" |
| 26 | + /> |
| 27 | + <stop |
| 28 | + offset="95%" |
| 29 | + stop-color="${dataset.color}" |
| 30 | + stop-opacity="0.1" |
| 31 | + /> |
| 32 | + </linearGradient> |
| 33 | + `).join('')); |
| 34 | +</script> |
| 35 | + |
| 36 | +<template> |
| 37 | + <ChartContainer class="flex flex-col" :config="chartConfig" cursor> |
| 38 | + <VisXYContainer class="flex-1 min-h-0" |
| 39 | + v-bind="{ |
| 40 | + xScale: xScale, |
| 41 | + svgDefs: svgDefs, |
| 42 | + } as XYContainerConfigInterface<Datum>" |
| 43 | + :data="data" |
| 44 | + > |
| 45 | + <template v-if="!props.widget.minimal"> |
| 46 | + <VisAxis |
| 47 | + v-bind="{ |
| 48 | + type: 'x', |
| 49 | + gridLine: false, |
| 50 | + domainLine: false, |
| 51 | + ...xAxisConfig, |
| 52 | + } as AxisConfigInterface<Datum>" |
| 53 | + /> |
| 54 | + <VisAxis v-bind="{ |
| 55 | + type: 'y', |
| 56 | + domainLine: false, |
| 57 | + ...yAxisConfig, |
| 58 | + } as AxisConfigInterface<Datum>" /> |
| 59 | + </template> |
| 60 | + |
| 61 | + <VisLine |
| 62 | + v-bind="{ |
| 63 | + x: x, |
| 64 | + y: y, |
| 65 | + color: color, |
| 66 | + lineWidth: 1, |
| 67 | + curveType: props.widget.options.curved ? CurveType.MonotoneX : CurveType.Linear, |
| 68 | + // stacked: true, |
| 69 | + } as LineConfigInterface<Datum>" |
| 70 | + /> |
| 71 | + |
| 72 | + <template v-for="(dataset, i) in props.value?.datasets"> |
| 73 | + <VisArea |
| 74 | + v-bind="{ |
| 75 | + x: x, |
| 76 | + // y: y, |
| 77 | + y: (d) => d[i], |
| 78 | + // color: props.value?.datasets.map((dataset, i) => props.widget.options.gradient ? `url(#fill-${i})` : dataset.color), |
| 79 | + color: props.widget.options.gradient ? `url(#fill-${i})` : dataset.color, |
| 80 | + opacity: props.widget.options.opacity, |
| 81 | + curveType: props.widget.options.curved ? CurveType.MonotoneX : CurveType.Linear, |
| 82 | + } as AreaConfigInterface<Datum>" |
| 83 | + /> |
| 84 | + </template> |
| 85 | + |
| 86 | + <ChartCrosshair |
| 87 | + v-bind="{ |
| 88 | + color: color, |
| 89 | + template: tooltipTemplate, |
| 90 | + hideWhenFarFromPointer: false, |
| 91 | + } as CrosshairConfigInterface<Datum>" |
| 92 | + /> |
| 93 | + |
| 94 | + <ChartTooltip /> |
| 95 | + </VisXYContainer> |
| 96 | + |
| 97 | + <template v-if="props.widget.showLegend && !props.widget.minimal"> |
| 98 | + <ChartLegendContent /> |
| 99 | + </template> |
| 100 | + </ChartContainer> |
| 101 | +</template> |
0 commit comments