svelte-plots-basic 3.1.6 → 3.2.0

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.
package/README.md CHANGED
@@ -15,6 +15,11 @@ These websites and web-applications use `svelte-plots-basic` library:
15
15
 
16
16
  ## News
17
17
 
18
+ ### 3.2.0
19
+
20
+ * Added mouse click handler option for `Heatmap.svelte` component (returns row and column index of the element).
21
+
22
+
18
23
  ### 3.1.1-3.1.6
19
24
 
20
25
  * Fixed bug which sometimes caused an error when manual text tick labels provided.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "svelte-plots-basic",
3
3
  "description": "Svelte components for creating simple plots",
4
- "version": "3.1.6",
4
+ "version": "3.2.0",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -38,9 +38,11 @@
38
38
  values, // Matrix with values to show heatmap for
39
39
  breaks, // vector with breaks to distribute the values in
40
40
  colmap = null, // array with colors for each interval
41
+ onclick = null, // callback for onclick (returns row and column indices of element)
41
42
  } = $props();
42
43
 
43
44
 
45
+
44
46
  // check that values are provided as a matrix
45
47
  let v = $derived(ismatrix(values) && values.nrows > 0 && values.ncols > 0 ? values : null);
46
48
 
@@ -110,19 +112,35 @@
110
112
  let rw = $derived(transformObjects([1], axes.tX()));
111
113
  let rh = $derived(transformObjects([1], axes.tY()));
112
114
 
115
+
116
+ // mouse click handler
117
+ function handleClick(e) {
118
+ if (!onclick || typeof onclick !== 'function') return;
119
+ const el = e.target;
120
+ if (el.tagName !== 'rect') return;
121
+ const r = el.getBoundingClientRect();
122
+ const x = r.x + r.width / 2;
123
+ const y = r.y - r.height / 2;
124
+ onclick(Math.round(y / r.height) - 1, Math.round(x / r.width) - 1)
125
+ e.stopPropagation();
126
+ }
127
+
128
+
113
129
  // check status
114
130
  let isOk = $derived(rx && ry && rx.length === ry.length);
115
131
  </script>
116
132
 
133
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
117
134
  {#if isOk}
118
- <g class="heatmap" style="stroke:0;stroke-width:0px;">
135
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
136
+ <g class="heatmap" style="stroke:0;stroke-width:0px;" onclick={handleClick}>
119
137
  <!-- loop over colors/intervals -->
120
138
  {#each lc as col, i}
121
139
  <g title="heatmap-group" style="fill:{col};">
122
140
  {#if rx[i].length > 0}
123
141
  <!-- loop over elements which fall into the interval -->
124
142
  {#each rx[i] as v, j}
125
- <rect x={rx[i][j]} y={ry[i][j]} width={rw} height={rh} />
143
+ <rect x={rx[i][j]} y={ry[i][j]} width={rw} height={rh}/>
126
144
  {/each}
127
145
  {/if}
128
146
  </g>