88} from "solid-js" ;
99import { type SetStoreFunction , createStore } from "solid-js/store" ;
1010
11- type SupportedScaleTypes =
11+ export type SupportedScaleTypes =
1212 | d3 . ScaleLinear < number , number , never >
1313 | d3 . ScaleLogarithmic < number , number , never > ;
1414const supportedScales = {
@@ -34,6 +34,7 @@ interface Chart {
3434 scaleY : SupportedScaleTypes ;
3535 formatX : ( value : number ) => string ;
3636 formatY : ( value : number ) => string ;
37+ transformX ?: ( x : number , y : number , scaleY : SupportedScaleTypes ) => number ;
3738}
3839type SetChart = SetStoreFunction < Chart > ;
3940const ChartContext = createContext < [ Chart , SetChart ] > ( ) ;
@@ -95,6 +96,7 @@ export function Chart(props: {
9596 title ?: string ;
9697 formatX ?: ( value : number ) => string ;
9798 formatY ?: ( value : number ) => string ;
99+ transformX ?: ( x : number , y : number , scaleY : SupportedScaleTypes ) => number ;
98100} ) {
99101 const [ hovering , setHovering ] = createSignal ( false ) ;
100102 const [ coords , setCoords ] = createSignal < [ number , number ] > ( [ 0 , 0 ] ) ;
@@ -108,12 +110,20 @@ export function Chart(props: {
108110 if ( props . formatY ) {
109111 updateChart ( "formatY" , ( ) => props . formatY ) ;
110112 }
113+ if ( props . transformX ) {
114+ updateChart ( "transformX" , ( ) => props . transformX ) ;
115+ }
116+
117+ const onMouseMove = ( e : MouseEvent ) => {
118+ let x = e . offsetX - marginLeft ;
119+ const y = e . offsetY - marginTop ;
120+
121+ if ( chart . transformX ) {
122+ x = chart . transformX ( x , y , chart . scaleY ) ;
123+ }
111124
112- const onMouseMove = ( e : MouseEvent ) =>
113- setCoords ( [
114- chart . scaleX . invert ( e . offsetX - marginLeft ) ,
115- chart . scaleY . invert ( e . offsetY - marginTop ) ,
116- ] ) ;
125+ setCoords ( [ chart . scaleX . invert ( x ) , chart . scaleY . invert ( y ) ] ) ;
126+ } ;
117127
118128 const renderXCoord = ( ) =>
119129 hovering ( ) ? `x: ${ chart . formatX ( coords ( ) [ 0 ] ) } ` : "" ;
0 commit comments