11import { _t } from "@odoo/o-spreadsheet-engine" ;
22import { CHART_AXIS_TITLE_FONT_SIZE } from "@odoo/o-spreadsheet-engine/constants" ;
3- import { LineChartRuntime } from "@odoo/o-spreadsheet-engine/types/chart" ;
3+ import { AxisType , LineChartRuntime } from "@odoo/o-spreadsheet-engine/types/chart" ;
44import { SpreadsheetChildEnv } from "@odoo/o-spreadsheet-engine/types/spreadsheet_env" ;
55import { Component , useState } from "@odoo/owl" ;
66import { deepCopy } from "../../../../../helpers" ;
@@ -77,18 +77,28 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
7777 this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
7878 }
7979
80- get axisMin ( ) : number | undefined {
81- return this . currentAxisDesign ?. min ;
80+ get axisMin ( ) : string | number | undefined {
81+ const min = this . currentAxisDesign ?. min ;
82+ return this . isTimeAxis ? this . formatAxisBoundary ( min ) : min ;
8283 }
8384
84- get axisMax ( ) : number | undefined {
85- return this . currentAxisDesign ?. max ;
85+ get axisMax ( ) : string | number | undefined {
86+ const max = this . currentAxisDesign ?. max ;
87+ return this . isTimeAxis ? this . formatAxisBoundary ( max ) : max ;
8688 }
8789
8890 get axisScaleType ( ) : AxisScaleType {
8991 return this . currentAxisDesign ?. scaleType ?? "linear" ;
9092 }
9193
94+ get axisBoundsInputType ( ) : "number" | "date" {
95+ return this . isTimeAxis ? "date" : "number" ;
96+ }
97+
98+ get axisBoundsInputStep ( ) : string | null {
99+ return this . isTimeAxis ? "1" : null ;
100+ }
101+
92102 get isMajorGridEnabled ( ) : boolean {
93103 const designValue = this . currentAxisDesign ?. grid ?. major ;
94104 if ( designValue !== undefined ) {
@@ -114,29 +124,29 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
114124 }
115125
116126 updateAxisMin ( ev : InputEvent ) {
117- const value = ( ev . target as HTMLInputElement ) . value . trim ( ) ;
118- const parsed = value === "" ? undefined : Number ( value ) ;
119- if ( parsed === undefined || ! isNaN ( parsed ) ) {
120- const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
121- axesDesign [ this . state . currentAxis ] = {
122- ...axesDesign [ this . state . currentAxis ] ,
123- min : parsed ,
124- } ;
125- this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
127+ const parsed = this . parseAxisBoundaryValue ( ev ) ;
128+ if ( parsed === null ) {
129+ return ;
126130 }
131+ const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
132+ axesDesign [ this . state . currentAxis ] = {
133+ ...axesDesign [ this . state . currentAxis ] ,
134+ min : parsed ,
135+ } ;
136+ this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
127137 }
128138
129139 updateAxisMax ( ev : InputEvent ) {
130- const value = ( ev . target as HTMLInputElement ) . value . trim ( ) ;
131- const parsed = value === "" ? undefined : Number ( value ) ;
132- if ( parsed === undefined || ! isNaN ( parsed ) ) {
133- const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
134- axesDesign [ this . state . currentAxis ] = {
135- ...axesDesign [ this . state . currentAxis ] ,
136- max : parsed ,
137- } ;
138- this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
140+ const parsed = this . parseAxisBoundaryValue ( ev ) ;
141+ if ( parsed === null ) {
142+ return ;
139143 }
144+ const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
145+ axesDesign [ this . state . currentAxis ] = {
146+ ...axesDesign [ this . state . currentAxis ] ,
147+ max : parsed ,
148+ } ;
149+ this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
140150 }
141151
142152 updateAxisScaleType ( ev : InputEvent ) {
@@ -198,9 +208,12 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
198208 if ( this . state . currentAxis !== "x" ) {
199209 return false ;
200210 }
201- const runtime = this . env . model . getters . getChartRuntime ( this . props . chartId ) as LineChartRuntime ;
202- const axisType = runtime ?. chartJsConfig . options ?. scales ?. x ?. type ;
203- return axisType === undefined || axisType === "time" ;
211+ const axisType = this . getXAxisType ( ) ;
212+ return axisType === undefined || axisType === "category" ;
213+ }
214+
215+ get isTimeAxis ( ) : boolean {
216+ return this . state . currentAxis === "x" && this . getXAxisType ( ) === "time" ;
204217 }
205218
206219 get canChangeMinorGridVisibility ( ) : boolean {
@@ -213,4 +226,43 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
213226 const type = this . props . definition . type ;
214227 return type === "line" || type === "scatter" ;
215228 }
229+
230+ private parseAxisBoundaryValue ( ev : InputEvent ) : number | undefined | null {
231+ const input = ev . target as HTMLInputElement ;
232+ const value = input . value . trim ( ) ;
233+ if ( value === "" ) {
234+ return undefined ;
235+ }
236+ if ( this . isTimeAxis ) {
237+ const timestamp = this . getTimestampFromInput ( input ) ;
238+ return Number . isNaN ( timestamp ) ? null : timestamp ;
239+ }
240+ const parsed = Number ( value ) ;
241+ return Number . isNaN ( parsed ) ? null : parsed ;
242+ }
243+
244+ private formatAxisBoundary ( value : number | string | undefined ) : string | undefined {
245+ if ( value === undefined ) {
246+ return undefined ;
247+ }
248+ const timestamp = typeof value === "number" ? value : Date . parse ( value ) ;
249+ if ( Number . isNaN ( timestamp ) ) {
250+ return typeof value === "string" ? value : undefined ;
251+ }
252+ const date = new Date ( timestamp ) ;
253+ return date . toISOString ( ) . split ( "T" ) [ 0 ] ;
254+ }
255+
256+ private getTimestampFromInput ( input : HTMLInputElement ) : number {
257+ const valueAsNumber = ( input as any ) . valueAsNumber as number | undefined ;
258+ if ( typeof valueAsNumber === "number" && ! Number . isNaN ( valueAsNumber ) ) {
259+ return valueAsNumber ;
260+ }
261+ return Date . parse ( input . value ) ;
262+ }
263+
264+ private getXAxisType ( ) : AxisType | undefined {
265+ const runtime = this . env . model . getters . getChartRuntime ( this . props . chartId ) as LineChartRuntime ;
266+ return runtime ?. chartJsConfig . options ?. scales ?. x ?. type as AxisType | undefined ;
267+ }
216268}
0 commit comments