11import { _t } from "@odoo/o-spreadsheet-engine" ;
22import { CHART_AXIS_TITLE_FONT_SIZE } from "@odoo/o-spreadsheet-engine/constants" ;
3- import { AxisGridType , AxisId , LineChartRuntime } from "@odoo/o-spreadsheet-engine/types/chart" ;
3+ import { toNumber } from "@odoo/o-spreadsheet-engine/functions/helpers" ;
4+ import {
5+ AxisGridType ,
6+ AxisId ,
7+ AxisType ,
8+ LineChartRuntime ,
9+ } from "@odoo/o-spreadsheet-engine/types/chart" ;
410import { SpreadsheetChildEnv } from "@odoo/o-spreadsheet-engine/types/spreadsheet_env" ;
511import { Component , useState } from "@odoo/owl" ;
6- import { deepCopy } from "../../../../../helpers" ;
12+ import { deepCopy , formatValue } from "../../../../../helpers" ;
713import { getDefinedAxis } from "../../../../../helpers/figures/charts" ;
814import {
915 AxisDesign ,
@@ -12,6 +18,7 @@ import {
1218 TitleDesign ,
1319 UID ,
1420} from "../../../../../types" ;
21+ import { DateInput } from "../../../../date_input/date_input" ;
1522import { NumberInput } from "../../../../number_input/number_input" ;
1623import { BadgeSelection } from "../../../components/badge_selection/badge_selection" ;
1724import { Checkbox } from "../../../components/checkbox/checkbox" ;
@@ -32,7 +39,7 @@ interface Props {
3239
3340export class AxisDesignEditor extends Component < Props , SpreadsheetChildEnv > {
3441 static template = "o-spreadsheet-AxisDesignEditor" ;
35- static components = { Section, ChartTitle, BadgeSelection, Checkbox, NumberInput } ;
42+ static components = { Section, ChartTitle, BadgeSelection, Checkbox, NumberInput, DateInput } ;
3643 static props = { chartId : String , definition : Object , updateChart : Function , axesList : Array } ;
3744
3845 state : { currentAxis : AxisId } = useState ( { currentAxis : "x" } ) ;
@@ -73,12 +80,14 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
7380 this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
7481 }
7582
76- get axisMin ( ) : number | string {
77- return this . currentAxisDesign ?. min ?? "" ;
83+ get axisMin ( ) : string | number {
84+ const min = this . currentAxisDesign ?. min ;
85+ return ( this . isTimeAxis ? this . formatAxisBoundary ( min ) : min ) ?? "" ;
7886 }
7987
80- get axisMax ( ) : number | string {
81- return this . currentAxisDesign ?. max ?? "" ;
88+ get axisMax ( ) : string | number {
89+ const max = this . currentAxisDesign ?. max ;
90+ return ( this . isTimeAxis ? this . formatAxisBoundary ( max ) : max ) ?? "" ;
8291 }
8392
8493 get isMajorGridEnabled ( ) : boolean {
@@ -115,29 +124,55 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
115124 }
116125
117126 updateAxisMin ( value : string ) {
118- const parsed = value === "" ? undefined : Number ( value ) ;
119- if ( parsed === undefined || ! isNaN ( parsed ) ) {
127+ const min = value === "" ? undefined : Number ( value ) ;
128+ if ( min === undefined || ! isNaN ( min ) ) {
120129 const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
121130 axesDesign [ this . state . currentAxis ] = {
122131 ...axesDesign [ this . state . currentAxis ] ,
123- min : parsed ,
132+ min,
124133 } ;
125134 this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
126135 }
127136 }
128137
138+ updateTimeAxisMin ( value : string ) {
139+ const min = this . parseTimeAxisBoundaryValue ( value ) ;
140+ if ( min === null ) {
141+ return ;
142+ }
143+ const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
144+ axesDesign [ this . state . currentAxis ] = {
145+ ...axesDesign [ this . state . currentAxis ] ,
146+ min,
147+ } ;
148+ this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
149+ }
150+
129151 updateAxisMax ( value : string ) {
130- const parsed = value === "" ? undefined : Number ( value ) ;
131- if ( parsed === undefined || ! isNaN ( parsed ) ) {
152+ const max = value === "" ? undefined : Number ( value ) ;
153+ if ( max === undefined || ! isNaN ( max ) ) {
132154 const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
133155 axesDesign [ this . state . currentAxis ] = {
134156 ...axesDesign [ this . state . currentAxis ] ,
135- max : parsed ,
157+ max,
136158 } ;
137159 this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
138160 }
139161 }
140162
163+ updateTimeAxisMax ( value : string ) {
164+ const max = this . parseTimeAxisBoundaryValue ( value ) ;
165+ if ( max === null ) {
166+ return ;
167+ }
168+ const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
169+ axesDesign [ this . state . currentAxis ] = {
170+ ...axesDesign [ this . state . currentAxis ] ,
171+ max,
172+ } ;
173+ this . props . updateChart ( this . props . chartId , { axesDesign } ) ;
174+ }
175+
141176 toggleMajorGrid ( major : boolean ) {
142177 const axesDesign = deepCopy ( this . props . definition . axesDesign ) ?? { } ;
143178 let gridLines : AxisGridType = "none" ;
@@ -193,8 +228,39 @@ export class AxisDesignEditor extends Component<Props, SpreadsheetChildEnv> {
193228 if ( this . isValueAxis ) {
194229 return false ;
195230 }
231+ const axisType = this . getXAxisType ( ) ;
232+ return axisType === undefined || axisType === "category" ;
233+ }
234+
235+ get isTimeAxis ( ) : boolean {
236+ return this . state . currentAxis === "x" && this . getXAxisType ( ) === "time" ;
237+ }
238+
239+ get canChangeMinorGridVisibility ( ) : boolean {
240+ if ( this . isValueAxis ) {
241+ return true ;
242+ }
243+ if ( this . isCategoricalAxis ) {
244+ return false ;
245+ }
246+ const type = this . props . definition . type ;
247+ return type === "line" || type === "scatter" ;
248+ }
249+
250+ private parseTimeAxisBoundaryValue ( value : string ) : number | undefined | null {
251+ const dateNumber = toNumber ( value , this . env . model . getters . getLocale ( ) ) ;
252+ return Number . isNaN ( dateNumber ) ? null : dateNumber ;
253+ }
254+
255+ private formatAxisBoundary ( value : number | undefined ) : string | undefined {
256+ if ( value === undefined ) {
257+ return undefined ;
258+ }
259+ return formatValue ( value , { format : "yyyy-mm-dd" , locale : this . env . model . getters . getLocale ( ) } ) ;
260+ }
261+
262+ private getXAxisType ( ) : AxisType | undefined {
196263 const runtime = this . env . model . getters . getChartRuntime ( this . props . chartId ) as LineChartRuntime ;
197- const axisType = runtime ?. chartJsConfig . options ?. scales ?. x ?. type ;
198- return axisType === undefined || axisType === "time" ;
264+ return runtime ?. chartJsConfig . options ?. scales ?. x ?. type as AxisType | undefined ;
199265 }
200266}
0 commit comments