svelte-plots-basic 3.0.7 → 3.1.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.
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.1.0
19
+
20
+ * Added possibility to reverse the axis, just swap the limit values.
21
+
22
+
18
23
  ### 3.0.0
19
24
 
20
25
  New major release (v. 3.0.0, released 20/01/2025) introduces many breaking changes as the library has been re-written using Svelte 5. If you use previous versions of `svelte-plots-basic` in your projects, and do not want to change anything, stick to the latest 2.x.x version.
@@ -77,7 +82,7 @@ Properties `limX` and `limY` define limits of the coordinates system in *plot's
77
82
  </style>
78
83
  ```
79
84
 
80
- These two properties must always be provided as an array with two values, and the first value must be smaller than the second. Both properties have default value of `[0, 1]`.
85
+ These two properties must always be provided as an array with two values (left and right). If the first value is larger than the second one, the corresponding axis will be reversed. Both properties have default value of `[0, 1]`.
81
86
 
82
87
  Second important property is `margins`, it defines margins around the plotting area — the one which will be used for positioning of actual plot elements. Margins should be provided as an array with four values: relative margins for bottom, left, top and right parts of the plot.
83
88
 
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.0.7",
4
+ "version": "3.1.1",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -130,8 +130,6 @@
130
130
  const isOk = $derived(
131
131
  checkArray(limX, 2) &&
132
132
  checkArray(limY, 2) &&
133
- limX[0] < limX[1] &&
134
- limY[0] < limY[1] &&
135
133
  width > (pxMargins[1] + pxMargins[3]) &&
136
134
  height > (pxMargins[0] + pxMargins[2])
137
135
  );
package/src/methods.js CHANGED
@@ -154,6 +154,7 @@ export function getTickLabels(ticks) {
154
154
  if (decNum <= 2) {
155
155
  return [0, Array.from(ticks).map(v => v.toFixed(decNum))];
156
156
  }
157
+
157
158
  tickFactor = decNum - 2;
158
159
  decNum = 2;
159
160
 
@@ -284,12 +285,15 @@ export function validateTicks(ticks, lim) {
284
285
  return [null, 'ticks values must be unique.'];
285
286
  }
286
287
 
288
+ // swap limits if necessary
289
+ const left = lim[0] < lim[1] ? lim[0] : lim[1];
290
+ const right = lim[0] < lim[1] ? lim[1] : lim[0];
291
+
287
292
  // check if provided ticks values are outside the axis limit range continue with automatic ticks
288
- const newTicks = ticks.filter(x => x >= lim[0] & x <= lim[1]);
293
+ const newTicks = ticks.filter(x => x >= left & x <= right);
289
294
  if (newTicks.length !== ticks.length) {
290
295
  return [null, 'some of ticks values are outside axis limits.'];
291
296
  }
292
-
293
297
  return [newTicks, ''];
294
298
  }
295
299
 
@@ -307,7 +311,7 @@ export function validateTicks(ticks, lim) {
307
311
  * @returns {Array} an array with computed tick positions.
308
312
  *
309
313
  */
310
- export function getAxisTicks(ticks, lim, maxTickNum, round, whole, deltaFactor) {
314
+ export function getAxisTicks(ticks, limIn, maxTickNum, round, whole, deltaFactor) {
311
315
 
312
316
  if (round === undefined || round === null) {
313
317
  round = true;
@@ -321,6 +325,9 @@ export function getAxisTicks(ticks, lim, maxTickNum, round, whole, deltaFactor)
321
325
  deltaFactor = 0.0001;
322
326
  }
323
327
 
328
+ // correct limits internally if they are reverse
329
+ const lim = limIn[0] < limIn[1] ? limIn : [limIn[1], limIn[0]];
330
+
324
331
  // if ticks are already provided do not recompute them
325
332
  // the validation is done in corresponding component, so no need to do it again
326
333
  if (ticks) return ticks;
@@ -935,11 +942,14 @@ export function getXAxisParams(limX, limY, scales, tY, axis) {
935
942
  const tfLabel = getTickFactorLabel(tickFactor);
936
943
 
937
944
  // hide last label if it is too close to tick factor
938
- const dtl = Math.abs(limX[1] - ticksX.v[ticksX.length - 1]);
939
- const dl = limX[1] - limX[0];
945
+ const n = tickLabels.length;
946
+ const dtl = Math.abs(limX[1] > limX[0] ? limX[1] - ticksX.v[n - 1] : limX[1] - ticksX.v[0]);
947
+ const dl = Math.abs(limX[1] - limX[0]);
940
948
  const dt = Math.abs(ticksX.v[1] - ticksX.v[0]);
941
- if (tfLabel.length > 0 && dtl < dl * 0.10 && dtl < dt * 0.5) {
942
- tickLabels[tickLabels.length - 1] = ' ';
949
+
950
+ if (tfLabel.length > 0 && dtl < dl * 0.10 && dtl < dt * 0.5) {
951
+ const pos = limX[1] > limX[0] ? n - 1 : 0
952
+ tickLabels[pos] = ' ';
943
953
  }
944
954
 
945
955
  return {grid, axisLine, tickCoords, tfCoords, ticks, tickLabels, tickFactor, tfLabel};
@@ -1014,10 +1024,21 @@ export function getYAxisParams(limX, limY, scales, tX, axis) {
1014
1024
  const tfLabel = getTickFactorLabel(tickFactor);
1015
1025
 
1016
1026
  // hide last label if it is too close to tick factor
1017
- if (tfLabel.length > 0 && (limY[1] - ticksY.v[ticksY.length - 1]) < ((limY[1] - limY[0]) * 0.05)) {
1018
- tickLabels[tickLabels.length - 1] = ' ';
1027
+ const n = tickLabels.length;
1028
+ const dtl = Math.abs(limY[1] > limY[0] ? limY[1] - ticksY.v[n - 1] : limY[1] - ticksY.v[0]);
1029
+ const dl = Math.abs(limY[1] - limY[0]);
1030
+ const dt = Math.abs(ticksY.v[1] - ticksY.v[0]);
1031
+
1032
+ if (tfLabel.length > 0 && dtl < dl * 0.10 && dtl < dt * 0.5) {
1033
+ const pos = limY[1] > limY[0] ? n - 1 : 0
1034
+ tickLabels[pos] = ' ';
1019
1035
  }
1020
1036
 
1037
+ // old version of the code above - remove after testing
1038
+ // if (tfLabel.length > 0 && (limY[1] - ticksY.v[ticksY.length - 1]) < ((limY[1] - limY[0]) * 0.05)) {
1039
+ // tickLabels[tickLabels.length - 1] = ' ';
1040
+ // }
1041
+
1021
1042
  return {grid, axisLine, tickCoords, tfCoords, ticks, tickLabels, tickFactor, tfLabel};
1022
1043
  }
1023
1044