11import { bisectLeft } from 'd3-array' ;
2- import { scaleLinear , scaleLog , scaleSqrt , scaleUtc } from 'd3-scale' ;
2+ import {
3+ scaleLinear ,
4+ scaleLog ,
5+ scaleSqrt ,
6+ scaleUtc ,
7+ ScaleLinear ,
8+ ScaleLogarithmic ,
9+ ScalePower ,
10+ ScaleTime ,
11+ } from 'd3-scale' ;
312import { DateTime } from 'luxon' ;
13+
414import { clamp , mergePartial } from '../commons' ;
515import { ScaleContinuousType , ScaleType , Scale } from './scales' ;
616
17+ /**
18+ * d3 scales excluding time scale
19+ */
20+ type D3ScaleNonTime = ScaleLinear < any , any > | ScaleLogarithmic < any , any > | ScalePower < any , any > ;
21+
22+ /**
23+ * All possible d3 scales
24+ */
25+ type D3Scale = D3ScaleNonTime | ScaleTime < any , any > ;
26+
727const SCALES = {
828 [ ScaleType . Linear ] : scaleLinear ,
929 [ ScaleType . Log ] : scaleLog ,
@@ -129,7 +149,7 @@ export class ScaleContinuous implements Scale {
129149 readonly timeZone : string ;
130150 readonly barsPadding : number ;
131151 readonly isSingleValueHistogram : boolean ;
132- private readonly d3Scale : any ;
152+ private readonly d3Scale : D3Scale ;
133153
134154 constructor ( scaleData : ScaleData , options ?: Partial < ScaleOptions > ) {
135155 const { type, domain, range } = scaleData ;
@@ -144,13 +164,10 @@ export class ScaleContinuous implements Scale {
144164 } = mergePartial ( defaultScaleOptions , options ) ;
145165
146166 this . d3Scale = SCALES [ type ] ( ) ;
147- if ( type === ScaleType . Log ) {
148- this . domain = limitLogScaleDomain ( domain ) ;
149- this . d3Scale . domain ( this . domain ) ;
150- } else {
151- this . domain = domain ;
152- this . d3Scale . domain ( domain ) ;
153- }
167+ const cleanDomain = type === ScaleType . Log ? limitLogScaleDomain ( domain ) : domain ;
168+ this . domain = cleanDomain ;
169+ this . d3Scale . domain ( cleanDomain ) ;
170+
154171 const safeBarPadding = clamp ( barsPadding , 0 , 1 ) ;
155172 this . barsPadding = safeBarPadding ;
156173 this . bandwidth = bandwidth * ( 1 - safeBarPadding ) ;
@@ -182,13 +199,18 @@ export class ScaleContinuous implements Scale {
182199 return currentDateTime . minus ( { minutes : currentOffset } ) . toMillis ( ) ;
183200 } ) ;
184201 } else {
185- if ( this . minInterval > 0 ) {
202+ /**
203+ * This case is for the xScale (minInterval is > 0) when we want to show bars (bandwidth > 0)
204+ *
205+ * We want to avoid displaying inner ticks between bars in a bar chart when using linear x scale
206+ */
207+ if ( minInterval > 0 && bandwidth > 0 ) {
186208 const intervalCount = Math . floor ( ( this . domain [ 1 ] - this . domain [ 0 ] ) / this . minInterval ) ;
187- this . tickValues = new Array ( intervalCount + 1 ) . fill ( 0 ) . map ( ( d , i ) => {
209+ this . tickValues = new Array ( intervalCount + 1 ) . fill ( 0 ) . map ( ( _ , i ) => {
188210 return this . domain [ 0 ] + i * this . minInterval ;
189211 } ) ;
190212 } else {
191- this . tickValues = this . d3Scale . ticks ( ticks ) ;
213+ this . tickValues = ( this . d3Scale as D3ScaleNonTime ) . ticks ( ticks ) ;
192214 }
193215 }
194216 }
@@ -205,12 +227,13 @@ export class ScaleContinuous implements Scale {
205227 ticks ( ) {
206228 return this . tickValues ;
207229 }
208- invert ( value : number ) {
230+ invert ( value : number ) : number {
209231 let invertedValue = this . d3Scale . invert ( value ) ;
210232 if ( this . type === ScaleType . Time ) {
211- invertedValue = DateTime . fromJSDate ( invertedValue ) . toMillis ( ) ;
233+ invertedValue = DateTime . fromJSDate ( invertedValue as Date ) . toMillis ( ) ;
212234 }
213- return invertedValue ;
235+
236+ return invertedValue as number ;
214237 }
215238 invertWithStep (
216239 value : number ,
0 commit comments