svelteplot 0.4.2-pr-185.1 → 0.4.2-pr-186.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,18 +28,12 @@ Helper component for rendering rectangular marks in SVG
28
28
 
29
29
  import { resolveProp, resolveStyles } from '../../helpers/resolve';
30
30
  import { roundedRect } from '../../helpers/roundedRect';
31
- import type {
32
- BaseMarkProps,
33
- BaseRectMarkProps,
34
- BorderRadius,
35
- ScaledDataRecord,
36
- UsedScales,
37
- PlotContext,
38
- DataRecord
39
- } from '../../index.js';
40
31
  import { addEventHandlers } from './events.js';
41
32
  import { getContext } from 'svelte';
42
33
  import Anchor from './Anchor.svelte';
34
+ import type { BaseMarkProps, BaseRectMarkProps, BorderRadius } from '../../types/mark.js';
35
+ import type { DataRecord, ScaledDataRecord } from '../../types/data.js';
36
+ import type { PlotContext, UsedScales } from '../../types/index.js';
43
37
 
44
38
  let {
45
39
  datum,
@@ -1,4 +1,6 @@
1
- import type { BaseMarkProps, BaseRectMarkProps, ScaledDataRecord, UsedScales, DataRecord } from '../../index.js';
1
+ import type { BaseMarkProps, BaseRectMarkProps } from '../../types/mark.js';
2
+ import type { DataRecord, ScaledDataRecord } from '../../types/data.js';
3
+ import type { UsedScales } from '../../types/index.js';
2
4
  declare class __sveltets_Render<Datum extends DataRecord> {
3
5
  props(): {
4
6
  datum: ScaledDataRecord<Datum_1>;
@@ -56,19 +56,23 @@ export function addEventHandlers(node, { options, datum, getPlotState }) {
56
56
  for (const [eventName, eventHandler] of Object.entries(events)) {
57
57
  if (eventHandler) {
58
58
  const wrappedHandler = (origEvent) => {
59
- const { scales } = getPlotState();
60
- if (origEvent.layerX !== undefined) {
59
+ const { scales, body, options } = getPlotState();
60
+ if (origEvent instanceof MouseEvent || origEvent instanceof PointerEvent) {
61
+ let facetEl = origEvent.target;
62
+ while (facetEl && !facetEl.classList.contains('facet')) {
63
+ facetEl = facetEl.parentElement;
64
+ }
65
+ const facetRect = (facetEl?.firstChild ?? body).getBoundingClientRect();
66
+ const relativeX = origEvent.clientX - facetRect.left + (options.marginLeft ?? 0);
67
+ const relativeY = origEvent.clientY - facetRect.top + (options.marginTop ?? 0);
61
68
  if (scales.projection) {
62
- const [x, y] = scales.projection.invert([
63
- origEvent.layerX,
64
- origEvent.layerY
65
- ]);
69
+ const [x, y] = scales.projection.invert([relativeX, relativeY]);
66
70
  origEvent.dataX = x;
67
71
  origEvent.dataY = y;
68
72
  }
69
73
  else {
70
- origEvent.dataX = invertScale(scales.x, origEvent.layerX);
71
- origEvent.dataY = invertScale(scales.y, origEvent.layerY);
74
+ origEvent.dataX = invertScale(scales.x, relativeX);
75
+ origEvent.dataY = invertScale(scales.y, relativeY);
72
76
  }
73
77
  }
74
78
  eventHandler(origEvent, datum.hasOwnProperty(RAW_VALUE) ? datum[RAW_VALUE] : datum, datum[INDEX]);
@@ -91,10 +95,13 @@ export function addEventHandlers(node, { options, datum, getPlotState }) {
91
95
  }
92
96
  function invertScale(scale, position) {
93
97
  if (scale.type === 'band') {
94
- // invert band scale since scaleBand doesn't have an invert function
98
+ const range = scale.fn.range();
99
+ const domain = scale.fn.domain();
95
100
  const eachBand = scale.fn.step();
96
- const index = Math.floor(position / eachBand);
97
- return scale.fn.domain()[index];
101
+ const extent = range[1] - range[0];
102
+ const posInRange = (position - range[0]) * Math.sign(extent);
103
+ const index = Math.floor(posInRange / eachBand);
104
+ return domain[index];
98
105
  }
99
106
  return scale.fn.invert ? scale.fn.invert(position) : undefined;
100
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.4.2-pr-185.1",
3
+ "version": "0.4.2-pr-186.1",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",