svelte-plots-basic 1.1.4 → 2.1.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 +59 -67
- package/package.json +10 -4
- package/src/2d/Area.svelte +61 -0
- package/src/2d/Axes.svelte +500 -0
- package/src/2d/Axis.svelte +67 -0
- package/src/2d/AxisLines.svelte +54 -0
- package/src/2d/AxisTickLabels.svelte +82 -0
- package/src/2d/Bars.svelte +65 -0
- package/src/2d/Box.svelte +50 -0
- package/src/2d/ColorLegend.svelte +36 -0
- package/src/2d/ColormapLegend.svelte +100 -0
- package/src/2d/Heatmap.svelte +109 -0
- package/src/2d/Legend.svelte +148 -0
- package/src/2d/Lines.svelte +49 -0
- package/src/2d/Multilines.svelte +67 -0
- package/src/2d/Points.svelte +79 -0
- package/src/2d/Rectangles.svelte +74 -0
- package/src/2d/Segments.svelte +65 -0
- package/src/2d/TextLabels.svelte +107 -0
- package/src/2d/TextLegend.svelte +53 -0
- package/src/2d/XAxis.svelte +109 -0
- package/src/2d/YAxis.svelte +109 -0
- package/src/2d/index.js +21 -0
- package/src/3d/Axes.svelte +445 -0
- package/src/3d/Axis.svelte +64 -0
- package/src/3d/AxisLines.svelte +50 -0
- package/src/3d/AxisTickLabels.svelte +54 -0
- package/src/3d/Lines.svelte +49 -0
- package/src/3d/Mesh.svelte +185 -0
- package/src/3d/Points.svelte +61 -0
- package/src/3d/Segments.svelte +69 -0
- package/src/3d/TextLabels.svelte +79 -0
- package/src/3d/XAxis.svelte +123 -0
- package/src/3d/YAxis.svelte +123 -0
- package/src/3d/ZAxis.svelte +123 -0
- package/src/3d/index.js +13 -0
- package/src/Colors.js +36 -1
- package/src/Utils.js +243 -0
- package/src/AreaSeries.svelte +0 -53
- package/src/Axes.svelte +0 -550
- package/src/BarSeries.svelte +0 -68
- package/src/Box.svelte +0 -35
- package/src/LineSeries.svelte +0 -50
- package/src/Rectangles.svelte +0 -64
- package/src/ScatterSeries.svelte +0 -69
- package/src/Segments.svelte +0 -52
- package/src/TextLabels.svelte +0 -88
- package/src/TextLegend.svelte +0 -38
- package/src/XAxis.svelte +0 -69
- package/src/YAxis.svelte +0 -72
- package/src/index.js +0 -16
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/****************************************************
|
|
3
|
+
* Z-Axis for 3D plots *
|
|
4
|
+
* -------------------- *
|
|
5
|
+
* shows z-axis and its elements (ticks, grid, etc.) *
|
|
6
|
+
*****************************************************/
|
|
7
|
+
|
|
8
|
+
import { cbind, matrix, Vector } from 'mdatools/arrays';
|
|
9
|
+
import { getContext } from 'svelte';
|
|
10
|
+
import { Colors } from '../Colors';
|
|
11
|
+
import { getAxisTicks } from '../Utils.js';
|
|
12
|
+
import Axis from './Axis.svelte';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/*****************************************/
|
|
16
|
+
/* Input parameters */
|
|
17
|
+
/*****************************************/
|
|
18
|
+
|
|
19
|
+
export let slot = 'zaxis'; // slot the component must be placed in
|
|
20
|
+
export let ticks = undefined; // vector with numeric tick positions in plot units
|
|
21
|
+
export let tickLabels = ticks; // vector with labels for each tick
|
|
22
|
+
export let showGrid = false; // logical, show or not grid lines
|
|
23
|
+
export let title = ''; // axis title
|
|
24
|
+
|
|
25
|
+
export let lineColor = Colors.DARKGRAY;
|
|
26
|
+
export let gridColor = Colors.MIDDLEGRAY;
|
|
27
|
+
export let textColor = Colors.DARKGRAY;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/*****************************************/
|
|
31
|
+
/* Component code */
|
|
32
|
+
/*****************************************/
|
|
33
|
+
|
|
34
|
+
if (slot !== 'zaxis') {
|
|
35
|
+
throw('Component ZAxis must have "slot=\'zaxis\'" attribute.');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// get axes context
|
|
39
|
+
const axes = getContext('axes');
|
|
40
|
+
|
|
41
|
+
// get reactive variables needed to compute coordinates
|
|
42
|
+
const xLim = axes.xLim;
|
|
43
|
+
const yLim = axes.yLim;
|
|
44
|
+
const zLim = axes.zLim;
|
|
45
|
+
const scale = axes.scale;
|
|
46
|
+
const isOk = axes.isOk;
|
|
47
|
+
|
|
48
|
+
// prepare variables for coordinates
|
|
49
|
+
let titleCoords = [];
|
|
50
|
+
let grid1 = [];
|
|
51
|
+
let grid2 = [];
|
|
52
|
+
let axisLine = [];
|
|
53
|
+
let tickCoords = [];
|
|
54
|
+
|
|
55
|
+
// compute tick x-coordinates
|
|
56
|
+
if ($isOk) {
|
|
57
|
+
const dZ = ($zLim[1] - $zLim[0]) / 100; // 1% of axis size
|
|
58
|
+
const ticksZ = getAxisTicks(ticks, $zLim, axes.TICK_NUM[$scale], true);
|
|
59
|
+
const tickNum = ticksZ.length;
|
|
60
|
+
|
|
61
|
+
// compute tick x-coordinates (middle, up and bottom)
|
|
62
|
+
const dX = ($xLim[1] - $xLim[0]) / 100; // 1% of axis size
|
|
63
|
+
const ticksX = Vector.fill($xLim[0], tickNum);
|
|
64
|
+
const ticksX1 = ticksX.subtract(dX * 1.5);
|
|
65
|
+
const ticksX2 = ticksX.add(dX * 1.5);
|
|
66
|
+
const ticksX3 = ticksX.subtract(dX * 1);
|
|
67
|
+
|
|
68
|
+
// tick y-coordinates
|
|
69
|
+
const dY = ($yLim[1] - $yLim[0]) / 100; // 1% of axis size
|
|
70
|
+
const ticksY = Vector.fill($yLim[0], tickNum);
|
|
71
|
+
const ticksY1 = ticksY.subtract(dY * 1.5);
|
|
72
|
+
const ticksY2 = ticksY.add(dY * 1.5);
|
|
73
|
+
const ticksY3 = ticksY.subtract(dY * 5);
|
|
74
|
+
|
|
75
|
+
// coordinates for the ends of grid
|
|
76
|
+
const gridXEnd = Vector.fill($xLim[1], tickNum);
|
|
77
|
+
const gridYEnd = Vector.fill($yLim[1], tickNum);
|
|
78
|
+
|
|
79
|
+
// tick labels
|
|
80
|
+
tickLabels = (ticks === undefined || tickLabels === undefined) ? ticksZ.v : tickLabels;
|
|
81
|
+
if (tickLabels.length !== ticksZ.length) {
|
|
82
|
+
throw Error('ZAxis: "tickLabels" must be a array of the same size as ticks.');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// combine all coordinates together
|
|
86
|
+
grid1 = [
|
|
87
|
+
cbind(ticksX, ticksY, ticksZ),
|
|
88
|
+
cbind(gridXEnd, ticksY, ticksZ)
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
grid2 = [
|
|
92
|
+
cbind(ticksX, ticksY, ticksZ),
|
|
93
|
+
cbind(ticksX, gridYEnd, ticksZ)
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
axisLine = [
|
|
97
|
+
matrix([$xLim[0], $yLim[0], $zLim[0]], 1, 3),
|
|
98
|
+
matrix([$xLim[0], $yLim[0], $zLim[1]], 1, 3)
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
tickCoords = [
|
|
102
|
+
cbind(ticksX, ticksY, ticksZ), // middle point
|
|
103
|
+
cbind(ticksX2, ticksY, ticksZ), // middle point with positive shift along X
|
|
104
|
+
cbind(ticksX, ticksY2, ticksZ), // middle point with positive shift along Y
|
|
105
|
+
cbind(ticksX1, ticksY1, ticksZ), // middle point with negative shift along X and Y
|
|
106
|
+
cbind(ticksX3, ticksY3, ticksZ) // middle point with negative shift along X and Y for ticks
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
// here we do not need to make a matrix as the three values will be used as vectors
|
|
110
|
+
titleCoords = [
|
|
111
|
+
[$xLim[0] - 2 * dX], [$yLim[0] - 2 * dY], [$zLim[1] + 3 * dZ]
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
</script>
|
|
115
|
+
|
|
116
|
+
{#if $isOk && axisLine.length > 0}
|
|
117
|
+
<Axis
|
|
118
|
+
className="mdaplot__zaxis"
|
|
119
|
+
{title} {lineColor} {gridColor} {textColor} {titleCoords}
|
|
120
|
+
{showGrid} {grid1} {grid2} {axisLine} {tickCoords} {tickLabels}
|
|
121
|
+
/>
|
|
122
|
+
{/if}
|
|
123
|
+
|
package/src/3d/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { default as Axes } from './Axes.svelte';
|
|
2
|
+
|
|
3
|
+
export { default as XAxis } from './XAxis.svelte';
|
|
4
|
+
export { default as YAxis } from './YAxis.svelte';
|
|
5
|
+
export { default as ZAxis } from './ZAxis.svelte';
|
|
6
|
+
|
|
7
|
+
export { default as TextLabels } from './TextLabels.svelte';
|
|
8
|
+
export { default as Segments } from './Segments.svelte';
|
|
9
|
+
export { default as Lines } from './Lines.svelte';
|
|
10
|
+
export { default as Mesh } from './Mesh.svelte';
|
|
11
|
+
export { default as Points } from './Points.svelte';
|
|
12
|
+
|
|
13
|
+
export { getcolmap, Colors } from '../Colors.js';
|
package/src/Colors.js
CHANGED
|
@@ -7,9 +7,44 @@ export const Colors = {
|
|
|
7
7
|
"MIDDLEGRAY": "#dadada",
|
|
8
8
|
"LIGHTGRAY": "#f0f0f0",
|
|
9
9
|
"DARKGRAY": "#606060",
|
|
10
|
+
"LEGEND": "#303030",
|
|
10
11
|
|
|
11
|
-
"PRIMARY": "#
|
|
12
|
+
"PRIMARY": "#2679B2",
|
|
12
13
|
"PRIMARY_TEXT": "#333",
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Returns an array with 'n' colors.
|
|
19
|
+
*
|
|
20
|
+
* @param {number} n - number of colors to return.
|
|
21
|
+
*
|
|
22
|
+
* @returns {Array} array with colors as strings (hexadecimal).
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
export function getcolmap(n) {
|
|
26
|
+
if (n == 1) return ["#2679B2"];
|
|
27
|
+
if (n == 2) return ["#2679B2", "#D22C2F"];
|
|
28
|
+
if (n == 3) return ["#2679B2", "#92B42A", "#D22C2F"];
|
|
29
|
+
if (n == 4) return ["#2679B2", "#2E9658", "#F2B825", "#D22C2F"];
|
|
30
|
+
if (n == 5) return ["#2679B2", "#22988A", "#92B42A", "#F79426", "#D22C2F"];
|
|
31
|
+
if (n == 6) return ["#2679B2", "#1C9AA8", "#379531", "#EED524", "#FB7F28", "#D22C2F"];
|
|
32
|
+
if (n == 7) return ["#2679B2", "#1D94A9", "#2E9658", "#92B42A", "#F2B825", "#F47129", "#D22C2F"];
|
|
33
|
+
if (n == 8) return ["#2679B2", "#1E90AA", "#279775", "#519E2F", "#D3CB25", "#F5A326", "#EF672A", "#D22C2F"];
|
|
34
|
+
if (n == 9) return ["#2679B2", "#1F8DAB", "#22988A", "#33953F", "#92B42A", "#EFCA24", "#F79426", "#EB5F2A", "#D22C2F"];
|
|
35
|
+
if (n == 10) return ["#2679B2", "#208BAC", "#1E999A", "#2E9658", "#5FA32E", "#C5C626", "#F2B825", "#F98827", "#E85A2B", "#D22C2F"];
|
|
36
|
+
if (n == 11) return ["#2679B2", "#2089AD", "#1C9AA8", "#29976C", "#379531", "#92B42A", "#EED524", "#F4AA26", "#FB7F28",
|
|
37
|
+
"#E6552B", "#D22C2F"];
|
|
38
|
+
if (n == 12) return ["#2679B2", "#2188AD", "#1C97A8", "#25987C", "#329546", "#68A62D", "#BCC327", "#F0C524", "#F69E26",
|
|
39
|
+
"#F77728", "#E4512B", "#D22C2F"];
|
|
40
|
+
if (n == 13) return ["#2679B2", "#2186AD", "#1D94A9", "#22988A", "#2E9658", "#469A2F", "#92B42A", "#DECF25", "#F2B825",
|
|
41
|
+
"#F79426", "#F47129", "#E34E2C", "#D22C2F"];
|
|
42
|
+
if (n == 14) return ["#2679B2", "#2285AE", "#1E92AA", "#209995", "#2A9767", "#34953A", "#6FA82D", "#B5C128", "#EFCE24", "#F3AD25",
|
|
43
|
+
"#F98C27", "#F16B29", "#E14B2C", "#D22C2F"];
|
|
44
|
+
if (n == 15) return ["#2679B2", "#2284AE", "#1E90AA", "#1D999F", "#279775", "#31964A", "#519E2F", "#92B42A", "#D3CB25", "#F0C224",
|
|
45
|
+
"#F5A326", "#FA8527", "#EF672A", "#E0492C", "#D22C2F"];
|
|
46
|
+
|
|
47
|
+
// if n > 15 return 16 colors
|
|
48
|
+
return ["#2679B2", "#2283AE", "#1F8FAB", "#1C9AA8", "#249880", "#2E9658", "#379531", "#73AA2C", "#B0BF28", "#EDD424", "#F2B825",
|
|
49
|
+
"#F69B26", "#FB7F28", "#ED632A", "#DF472C", "#D22C2F"];
|
|
50
|
+
}
|
package/src/Utils.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { cbind, vector, isvector, Vector } from 'mdatools/arrays';
|
|
2
|
+
import { min, max, diff } from 'mdatools/stat';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate vector with axis tick labels and tick factor based on numeric tick values.
|
|
7
|
+
*
|
|
8
|
+
* @param {Vector} ticks - vector with numeric tick values.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Array} an array with tick factor and array with tick labels.
|
|
11
|
+
*/
|
|
12
|
+
export function getTickLabels(ticks) {
|
|
13
|
+
|
|
14
|
+
if (ticks === undefined) return undefined;
|
|
15
|
+
|
|
16
|
+
const step = Math.abs(min(diff(ticks)));
|
|
17
|
+
let tickFactor = 0;
|
|
18
|
+
if (step < 1 && Math.abs(max(ticks)) < 1) {
|
|
19
|
+
// values between 0 and 1 (absolute)
|
|
20
|
+
let decNum = Math.ceil(-Math.log10(step));
|
|
21
|
+
if (decNum <= 2) {
|
|
22
|
+
return [0, Array.from(ticks).map(v => v.toFixed(decNum).toString())];
|
|
23
|
+
}
|
|
24
|
+
tickFactor = decNum - 2;
|
|
25
|
+
decNum = 2;
|
|
26
|
+
return [-tickFactor, Array.from(ticks).map(v => (v * Math.pow(10, tickFactor)).toFixed(decNum).toString())];
|
|
27
|
+
} else {
|
|
28
|
+
let decNum = Math.ceil(Math.log10(step));
|
|
29
|
+
if (decNum <= 2) {
|
|
30
|
+
return [0, Array.from(ticks).map(v => v.toString())];
|
|
31
|
+
}
|
|
32
|
+
tickFactor = decNum - 2;
|
|
33
|
+
decNum = 2;
|
|
34
|
+
return [tickFactor, Array.from(ticks).map(v => (v / Math.pow(10, tickFactor)).toString())];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Check coordinates and convert them to vector if necessary.
|
|
41
|
+
*
|
|
42
|
+
* @param {Array|Vector} x - vector or array with coordinates.
|
|
43
|
+
* @param {string} source - name of component which called the method (needed for error message).
|
|
44
|
+
*
|
|
45
|
+
* @returns {Vector}
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export function checkCoords(x, source) {
|
|
49
|
+
|
|
50
|
+
if (Array.isArray(x)) {
|
|
51
|
+
x = vector(x);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!isvector(x)) {
|
|
55
|
+
throw Error(source + ": coordinates must be provided as array or instance of Vector class.");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (x.length < 1) {
|
|
59
|
+
throw Error(source + ": vector with coordinates is empty.");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return x;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create string with coordinates of SVG polygon in 3D.
|
|
68
|
+
*
|
|
69
|
+
* @param {Array|Vector} x - vector with x-coordinates of polygon points.
|
|
70
|
+
* @param {Array|Vector} y - vector with y-coordinates of polygon points.
|
|
71
|
+
* @param {Array|Vector} z - vector with y-coordinates of polygon points.
|
|
72
|
+
* @param {Array} tM - transformation matrix for 3D->2D (from 'Axes').
|
|
73
|
+
* @param {Object} axes - JSON with Axes context.
|
|
74
|
+
*
|
|
75
|
+
* @returns {string} string with coordinates.
|
|
76
|
+
*
|
|
77
|
+
*/
|
|
78
|
+
export function val2p3d(x, y, z, tM, axes) {
|
|
79
|
+
|
|
80
|
+
if (x === undefined || y === undefined) return undefined;
|
|
81
|
+
|
|
82
|
+
const [px, py] = axes.transform(cbind(checkCoords(x), checkCoords(y), checkCoords(z)), tM);
|
|
83
|
+
|
|
84
|
+
if (px.length !== py.length) {
|
|
85
|
+
throw Error('PlotSeries: parameters "xValues", "yValues" and "zValues" must be numeric vectors of the same length.');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let p = "";
|
|
89
|
+
for (let i = 0; i < px.length; i++) {
|
|
90
|
+
p += px[i] + "," + py[i] + " ";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return p;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Create string with coordinates of SVG polygon.
|
|
99
|
+
*
|
|
100
|
+
* @param {Array|Vector} x - vector with x-coordinates of polygon points.
|
|
101
|
+
* @param {Array|Vector} y - vector with y-coordinates of polygon points.
|
|
102
|
+
* @param {Array} tX - transformation parameters for x-coordinates (from 'Axes').
|
|
103
|
+
* @param {Array} tY - transformation parameters for y-coordinates (from 'Axes').
|
|
104
|
+
* @param {Object} axes - JSON with Axes context.
|
|
105
|
+
*
|
|
106
|
+
* @returns {string} string with coordinates.
|
|
107
|
+
*
|
|
108
|
+
*/
|
|
109
|
+
export function val2p(x, y, tX, tY, axes) {
|
|
110
|
+
|
|
111
|
+
if (x === undefined || y === undefined) return undefined;
|
|
112
|
+
|
|
113
|
+
const px = axes.transform(checkCoords(x), tX.coords);
|
|
114
|
+
const py = axes.transform(checkCoords(y), tY.coords);
|
|
115
|
+
|
|
116
|
+
if (px.length != py.length) {
|
|
117
|
+
throw Error('PlotSeries: parameters "xValues" and "yValues" must be numeric vectors of the same length.');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let p = "";
|
|
121
|
+
for (let i = 0; i < px.length; i++) {
|
|
122
|
+
p += px[i] + "," + py[i] + " ";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return p;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Computes nice tick values for axis.
|
|
130
|
+
*
|
|
131
|
+
* @param {Array} ticks - vector with ticks if alredy available (if not, new will be computed).
|
|
132
|
+
* @param {Array} lim - vector with axis limits tickets must be computed for.
|
|
133
|
+
* @param {number} maxTickNum - maximum number of ticks to compute.
|
|
134
|
+
* @param {boolean} round - round or not the fractions when computing nice numbers for the ticks.
|
|
135
|
+
*
|
|
136
|
+
* @returns {Array} an array with computed tick positions.
|
|
137
|
+
*
|
|
138
|
+
*/
|
|
139
|
+
export function getAxisTicks(ticks, lim, maxTickNum, round = true) {
|
|
140
|
+
|
|
141
|
+
// if ticks are already provided do not recompute them
|
|
142
|
+
if (ticks !== undefined) {
|
|
143
|
+
if (Array.isArray(ticks)) {
|
|
144
|
+
ticks = vector(ticks);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!isvector(ticks)) {
|
|
148
|
+
throw Error('getAxisTicks: axis ticks must be provided as an array or as a vector.');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return ticks.filter(x => x >= lim[0] & x <= lim[1]);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// check if limits are ok
|
|
155
|
+
if (typeof(lim) !== "object" || lim[0] === undefined || lim[1] === undefined) return undefined;
|
|
156
|
+
|
|
157
|
+
// get range as a nice number and compute min, max and steps for the tick sequence
|
|
158
|
+
const delta = (lim[1] - lim[0]) / 50;
|
|
159
|
+
const range = niceNum(lim[1] - lim[0] - 1 * delta, round);
|
|
160
|
+
const tickSpacing = niceNum(range / (maxTickNum - 1), round);
|
|
161
|
+
const tickMin = Math.ceil((lim[0] + delta) / tickSpacing) * tickSpacing;
|
|
162
|
+
const tickMax = Math.floor((lim[1] - delta) / tickSpacing) * tickSpacing;
|
|
163
|
+
|
|
164
|
+
// recompute maxTickNum
|
|
165
|
+
maxTickNum = Math.round((tickMax - tickMin + 1) / tickSpacing) + 1;
|
|
166
|
+
|
|
167
|
+
// create a sequence of ticks
|
|
168
|
+
ticks = Vector.seq(tickMin, tickMax, tickSpacing);
|
|
169
|
+
|
|
170
|
+
// if step is smaller than 1 round values to remove small decimals accidentially added by JS
|
|
171
|
+
if (Math.abs(tickSpacing) < 1) {
|
|
172
|
+
const decNum = Math.round(-Math.log10(tickSpacing));
|
|
173
|
+
const r = Math.pow(10, 1 + Math.round(-Math.log10(tickSpacing)));
|
|
174
|
+
ticks = ticks.apply(v => (Math.round((v + Number.EPSILON) * r) / r));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// make sure the ticks are not aligned with axes limits
|
|
178
|
+
return ticks.filter(x => x >= lim[0] & x <= lim[1]);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Computes a nice spacing value for a given range.
|
|
184
|
+
*
|
|
185
|
+
* @param {Number} localRange - a range (max - min).
|
|
186
|
+
* @param {boolean} round - round or not the fractions when computing the number.
|
|
187
|
+
*
|
|
188
|
+
* @returns {Number} the computed spacing value.
|
|
189
|
+
*
|
|
190
|
+
*/
|
|
191
|
+
export function niceNum( localRange, round) {
|
|
192
|
+
|
|
193
|
+
const exponent = Math.floor(Math.log10(localRange));
|
|
194
|
+
const fraction = localRange / Math.pow(10, exponent);
|
|
195
|
+
let niceFraction;
|
|
196
|
+
|
|
197
|
+
if (round) {
|
|
198
|
+
if (fraction < 1.5)
|
|
199
|
+
niceFraction = 1;
|
|
200
|
+
else if (fraction < 3)
|
|
201
|
+
niceFraction = 2;
|
|
202
|
+
else if (fraction < 7)
|
|
203
|
+
niceFraction = 5;
|
|
204
|
+
else
|
|
205
|
+
niceFraction = 10;
|
|
206
|
+
} else {
|
|
207
|
+
if (fraction <= 1)
|
|
208
|
+
niceFraction = 1;
|
|
209
|
+
else if (fraction <= 2)
|
|
210
|
+
niceFraction = 2;
|
|
211
|
+
else if (fraction <= 5)
|
|
212
|
+
niceFraction = 5;
|
|
213
|
+
else
|
|
214
|
+
niceFraction = 10;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return niceFraction * Math.pow(10, exponent);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Rounds coordinate values to single decimal
|
|
223
|
+
*/
|
|
224
|
+
export function roundCoords(x) {
|
|
225
|
+
return Math.round(x * 10) / 10
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Computes a scale level.
|
|
231
|
+
*
|
|
232
|
+
* @param {numeric} width - width of plotting area in pixels.
|
|
233
|
+
* @param {numeric} height - height of plotting area in pixels.
|
|
234
|
+
*
|
|
235
|
+
* @returns {text} the scale level ("small", "medium" or "large").
|
|
236
|
+
*
|
|
237
|
+
*/
|
|
238
|
+
export function getScale(width, height) {
|
|
239
|
+
if (height < 300.2 || width < 300.2) return "small";
|
|
240
|
+
if (height < 600.2 || width < 600.2) return "medium";
|
|
241
|
+
if (height < 850.2 || width < 850.2) return "large";
|
|
242
|
+
return "xlarge";
|
|
243
|
+
}
|
package/src/AreaSeries.svelte
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
import { getContext } from 'svelte';
|
|
4
|
-
import { mrange } from 'mdatools/stat';
|
|
5
|
-
import { Colors } from './Colors';
|
|
6
|
-
|
|
7
|
-
// input parameters
|
|
8
|
-
export let xValues;
|
|
9
|
-
export let yValues;
|
|
10
|
-
export let title = "";
|
|
11
|
-
export let lineWidth = 1;
|
|
12
|
-
export let lineColor = Colors.PRIMARY;
|
|
13
|
-
export let fillColor = Colors.PRIMARY;
|
|
14
|
-
export let opacity = 1;
|
|
15
|
-
export let lineType = 1;
|
|
16
|
-
|
|
17
|
-
/* sanity check of input parameters */
|
|
18
|
-
if (!Array.isArray(xValues) || !Array.isArray(yValues) || xValues.length != yValues.length) {
|
|
19
|
-
throw("AreaSeries: parameters 'xValues' and 'yValues' must be numeric vectors of the same length.");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// compute ranges for x and y values
|
|
23
|
-
const xValuesRange = mrange(xValues, 0.05);
|
|
24
|
-
const yValuesRange = mrange(yValues, 0.05);
|
|
25
|
-
|
|
26
|
-
// get axes context and adjust axes limits
|
|
27
|
-
const axes = getContext('axes');
|
|
28
|
-
axes.adjustXAxisLimits(xValuesRange);
|
|
29
|
-
axes.adjustYAxisLimits(yValuesRange);
|
|
30
|
-
|
|
31
|
-
// get reactive variables needed to compute coordinates
|
|
32
|
-
const xLim = axes.xLim;
|
|
33
|
-
const yLim = axes.yLim;
|
|
34
|
-
const axesWidth = axes.width;
|
|
35
|
-
const axesHeight = axes.height;
|
|
36
|
-
const scale = axes.scale;
|
|
37
|
-
|
|
38
|
-
// reactive variables for coordinates of data points in pixels
|
|
39
|
-
$: y0 = axes.scaleY([0], $yLim, $axesHeight);
|
|
40
|
-
$: x = axes.scaleX(xValues, $xLim, $axesWidth);
|
|
41
|
-
$: y = axes.scaleY(yValues, $yLim, $axesHeight);
|
|
42
|
-
$: p = x !== undefined && y !== undefined ? x.map((v, i) => `${v},${y[i]}`).join(' ') : undefined;
|
|
43
|
-
$: areaStyleStr = `opacity:${opacity};fill:${fillColor};stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${axes.LINE_STYLES[$scale][lineType - 1]}`;
|
|
44
|
-
</script>
|
|
45
|
-
|
|
46
|
-
{#if p !== undefined}
|
|
47
|
-
<g class="series lineseries" style="{areaStyleStr}" title="{title}">
|
|
48
|
-
<polygon points="{x[0] + "," + y0 + " " + p + " " + x[x.length - 1] + "," + y0[0]}"/>
|
|
49
|
-
</g>
|
|
50
|
-
{/if}
|
|
51
|
-
|
|
52
|
-
<style>
|
|
53
|
-
</style>
|