@@ -2,7 +2,14 @@ import { DashboardWidgetProps } from "@/dashboard/types";
22import { GraphWidgetData } from "@/types" ;
33import { computed , reactive , toRefs } from "vue" ;
44import { XYComponentConfigInterface } from "@unovis/ts/core/xy-component/config" ;
5- import { AxisConfigInterface , ColorAccessor , CrosshairConfigInterface , Scale } from "@unovis/ts" ;
5+ import {
6+ AxisConfigInterface ,
7+ ColorAccessor ,
8+ FitMode ,
9+ Scale ,
10+ TextAlign ,
11+ TrimMode
12+ } from "@unovis/ts" ;
613import { timeTickInterval } from 'd3-time' ;
714import { ChartConfig , ChartTooltip , ChartTooltipContent , componentToString } from "@/components/ui/chart" ;
815export type Datum = number [ ] ;
@@ -15,62 +22,65 @@ export function useXYChart(props: DashboardWidgetProps<GraphWidgetData>) {
1522 } ) ;
1623 return res ;
1724 } , [ ] ) ) ;
18- // const timeScale = false;
1925 const timeScale = true ;
2026 const x : XYComponentConfigInterface < Datum > [ 'x' ] = ( d , i ) => {
21- return props . widget . dateLabels && timeScale ? new Date ( props . value . labels [ i ] ) . getTime ( ) : i ;
27+ return props . widget . dateLabels && ! props . widget . showAllLabels && timeScale
28+ ? new Date ( props . value . labels [ i ] ) . getTime ( )
29+ : i ;
2230 } ;
2331 const y = computed ( ( ) : XYComponentConfigInterface < Datum > [ 'y' ] => props . value ?. datasets . map ( ( dataset , i ) => ( d ) => d [ i ] ) ) ;
2432 const xScale = computed ( ( ) : XYComponentConfigInterface < Datum > [ 'xScale' ] => {
25- return props . widget . dateLabels && timeScale
33+ return props . widget . dateLabels && ! props . widget . showAllLabels && timeScale
2634 ? Scale . scaleUtc ( ) . domain ( [ new Date ( props . value . labels [ 0 ] ) , new Date ( props . value . labels . at ( - 1 ) ) ] ) as any
2735 : undefined
2836 } ) ;
29- const xTickValues = computed ( ( ) : AxisConfigInterface < Datum > [ 'tickValues' ] => {
30- // return undefined;
31- if ( props . widget . dateLabels && timeScale ) {
32- return props . value . labels . length < 10
33- ? props . value . labels . map ( ( l ) => new Date ( l ) )
34- : xScale . value . ticks ( 10 ) as any ;
35- }
36- } ) ;
3737 const color = computed ( ( ) : ColorAccessor < Datum | Datum [ ] > => props . value ?. datasets . map ( ( dataset , i ) => dataset . color ) ) ;
3838
39- // const tooltipTemplate = (d: Datum, x: number) => {
40- // const formattedLabel = props.widget.dateLabels
41- // ? new Intl.DateTimeFormat(undefined, { day: '2-digit', month: 'short' }).format(timeScale ? x : new Date(props.value.labels[Math.round(x as number)]))
42- // : props.value.labels[Math.round(x as number)];
43- // return `<div class="mb-1 text-sm">${formattedLabel}</div>
44- // ${
45- // props.value?.datasets.map((dataset, i) =>
46- // `<div class="flex items-center gap-2 text-sm">
47- // <span class="size-2 rounded-full bg-(--color)" style="--color: ${dataset.color}"></span>
48- // ${dataset.label ? `<span class="text-sm">${dataset.label}:</span>` : '' }
49- // ${d[i]}
50- // </div>`
51- // ).join('')
52- // }`;
53- // }
54-
5539 const chartConfig = computed ( ( ) : ChartConfig =>
5640 Object . fromEntries ( props . value ?. datasets . map ( ( dataset , i ) => [ i , ( { label : dataset . label , color : dataset . color } ) ] ) )
5741 ) ;
5842
5943 const tooltipTemplate = componentToString ( chartConfig , ChartTooltipContent , {
6044 labelFormatter : ( x ) => {
6145 return props . widget . dateLabels
62- ? new Intl . DateTimeFormat ( undefined , { day : '2-digit' , month : 'short' } ) . format ( timeScale ? x : new Date ( props . value . labels [ Math . round ( x as number ) ] ) )
46+ ? new Intl . DateTimeFormat ( undefined , { day : '2-digit' , month : 'short' } )
47+ . format ( x instanceof Date ? x : new Date ( props . value . labels [ Math . round ( x as number ) ] ) )
6348 : props . value . labels [ Math . round ( x as number ) ] ;
6449 }
6550 } ) ;
6651
67- const tickFormat : AxisConfigInterface < number [ ] > [ 'tickFormat' ] = ( tick , i ) => {
68- if ( props . widget . dateLabels ) {
69- return new Intl . DateTimeFormat ( undefined , { day : '2-digit' , month : 'short' } )
70- . format ( timeScale ? tick : new Date ( props . value . labels [ tick as number ] ) ) ;
71- }
72- return props . value ?. labels ?. [ tick as number ] ;
73- }
52+ const rotate = computed ( ( ) =>
53+ ! props . widget . options . horizontal
54+ && props . widget . showAllLabels
55+ && props . value ?. labels ?. length >= 10
56+ ) ;
57+
58+ const xAxisConfig = computed ( ( ) : AxisConfigInterface < Datum > => ( {
59+ tickValues : ( ( ) => {
60+ if ( props . widget . showAllLabels ) {
61+ return props . value . labels . map ( ( _ , i ) => i ) ;
62+ }
63+ if ( props . widget . dateLabels && timeScale ) {
64+ return props . value . labels . length < 10
65+ ? props . value . labels . map ( ( l ) => new Date ( l ) )
66+ : xScale . value . ticks ( 10 ) as any ;
67+ }
68+ } ) ( ) ,
69+ tickFormat : ( tick , i ) => {
70+ if ( props . widget . dateLabels ) {
71+ return new Intl . DateTimeFormat ( undefined , { day : '2-digit' , month : 'short' } )
72+ . format ( tick instanceof Date ? tick : new Date ( props . value . labels [ tick as number ] ) ) ;
73+ }
74+ return props . value ?. labels ?. [ tick as number ] ;
75+ } ,
76+ tickTextTrimType : TrimMode . End ,
77+ // tickTextAlign: rotate.value ? TextAlign.Left : props.widget.options.horizontal ? TextAlign.Right : TextAlign.Center,
78+ tickTextAlign : rotate . value ? TextAlign . Right : props . widget . options . horizontal ? TextAlign . Right : TextAlign . Center ,
79+ tickTextFitMode : rotate . value ? FitMode . Wrap : FitMode . Trim ,
80+ // tickTextAngle: rotate.value ? 45 : undefined,
81+ tickTextAngle : rotate . value ? - 45 : undefined ,
82+ tickTextWidth : rotate . value ? 100 : undefined ,
83+ } ) ) ;
7484
7585 return {
7686 data,
@@ -80,8 +90,7 @@ export function useXYChart(props: DashboardWidgetProps<GraphWidgetData>) {
8090 tooltipTemplate,
8191 timeScale,
8292 xScale,
83- xTickValues,
8493 chartConfig,
85- tickFormat ,
94+ xAxisConfig ,
8695 } ;
8796}
0 commit comments