svelte-plots-basic 2.5.8 → 3.0.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
@@ -1,6 +1,9 @@
1
1
  # Svelte components for creating plots
2
2
 
3
- The library is under development and breaking changes may occur in the coming versions.
3
+ `svelte-plots-basic` is a [Svelte](https://svelte.dev) component library for making simple responsive 2D and 3D plots/charts. The plots are created by generating [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) inside HTML document.
4
+
5
+ One can think of this library as "Lego" bricks for plots. It has two groups of components: for 2D and for 3D plots. The 2D components are more developed while 3D components remain quite basic. In addition to `svelte`, the package has one direct dependence, [mdatools-js](https://github.com/svkucheryavski/mdatools-js) library, which is used for vector/matrix operations, statistics, and other manipulations with data values.
6
+
4
7
 
5
8
  ## Showcase
6
9
 
@@ -8,713 +11,793 @@ These websites and web-applications use `svelte-plots-basic` library:
8
11
 
9
12
  * [graasta.com](https://graasta.com) — interactive web-apps for learning statistics and beyond.
10
13
  * [mda.tools/ddsimca/](https://mda.tools/ddsimca/) — interactive web-app for one class classification using [DD-SIMCA](https://analyticalsciencejournals.onlinelibrary.wiley.com/doi/epdf/10.1002/cem.3556) method.
14
+ * [mda.tools/pca/](https://mda.tools/pca/) — interactive web-app for Principal Component Analysis method.
11
15
 
12
16
  ## News
13
17
 
14
- ### 2.5.0
15
-
16
- * improvements to axis ticks and tick labels estimation.
17
- * `<XAxis>` and `<YAxis>` got a new logical parameter `whole` to force using whole numbers as ticks.
18
-
19
-
20
- ### 2.4.0
21
-
22
- * added possibility to copy a plot to clipboard.
23
-
24
-
25
- ### 2.3.0
26
-
27
- * added possibility to save plot as SVG and PNG files (see details below).
28
- * improved handling of markers in `Points` component.
29
- * small improvements and bug fixes.
30
- * added more detailed description with code example both here and Svelte REPL (see below).
18
+ ### 3.0.0
31
19
 
32
- ### 2.2.0
20
+ 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.
33
21
 
34
- * Better handling of axes labels and plot title (now they are part of SVG object).
35
- * Other small improvements and bug fixes.
22
+ In addition to Svelte 5 syntax and functionality, this release also introduces a lot of improvements, such as better handling of axis ticks, new syntax for axis elements, etc. See examples below for inspiration. Here is a short list what has been changed from 2.x.x:
36
23
 
37
- ### 2.1.0
24
+ * **No more slots** — components `<Box>`, `<XAxis>`, `<YAxis>`, and `<ZAxis>` (for 3D) do not require attribute `slot` anymore. Moreover, you have to remove this attribute from all your old code in order to use the new version.
38
25
 
39
- * Added new elements, `Heatmap` and `ColormapLegend`.
40
- * Improvements to tick labels, in particular if values are too small (< 0.01) or too large (>99) the values are adjusted and a common factor is shown at the end of axis. This is applied only to automatic ticks, manually provided ticks and tick labels are shown as is.
41
- * Small improvements and bug fixes.
26
+ * **Axis labels** — in version 2.x.x. labels for x- and y-axis were a part of `<Axes>` component. From 3.x.x. they are part of corresponding axis components, for example:<br> `<XAxis label="x-axis label"/>`.
42
27
 
43
- ### 2.0.0
44
- New major release (v. 2.0.0) introduces many breaking changes as the library was completely re-written. If you use previous versions of `svelte-plots-basic` in your projects, and do not want to change anything, stick to the latest 1.x.x version (v. 1.1.4).
28
+ * **Support for subscripts and superscripts** — you can now use simple syntax for subscripts (`_`) and superscripts (`^`) in axis labels, tick labels, legend labels and plot title. For example, such labels as `'x^2'`, `x^-1` or `x_(34)` — will be correctly transformed to corresponding SVG text elements in order to visualize them correctly. You can also use HTML symbols, such as `&alpha;`.
45
29
 
46
- ## Description
30
+ * **Mouse events** — previosly, handling mouse events was done through a coplex system of manual events dispatched by `<Axes>` component. From 3.x.x this is much easier, almost every 2D series component (`<Rectangles>`, `<Bars>`, `<Points>`, `<Segments>`, `<Lines>`, `<Areas>`) can handle its own `onclick` event. You just need to provide a callback — a function that will be run if this event is fired. The callback should have one argument — id (position) of an element the click was made on. For example `<Points ... onclick={(id) => console.log(id)}>`. Because of this modification, the property `title` has been removed from all components. 2D component `<Axes>` also supports `onclick` event, it provides coordinates of the clicked point as arguments for the callback function. 3D plots do not support mouse events.
47
31
 
48
- `svelte-plots-basic` is a [Svelte](https://svelte.dev) component library for creating simple responsive 2D and 3D plots/charts. The plots are created by generating [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) inside HTML document.
32
+ * **Save and copy 3D plots** — from 3.x.x you can also save and copy to clipboard 3D plots (same way as for 2D).
49
33
 
50
- The library provides building blocks for creating plots, one can think of this library as "Lego" bricks for plots. It has two groups of components: for 2D and for 3D plots. In addition to `svelte` the library has one direct dependence, [mdatools-js](https://github.com/svkucheryavski/mdatools-js) library which is used for vector/matrix operations, statistics, and other manipulations with data values.
34
+ * **Doc strings** — every component has a corresponding doc string with description of its properties and a simple code example. It should be available when you move your mouse over the component tag in your editor/IDE if it supports this option (works in VSCode).
51
35
 
36
+ * **Properites** — from 3.x.x the naming of the properties is more consistent. For example, before the library used properties `borderColor` and `borderWidth` for areas, rectangels, bars and markers, while for lines, segments and multilines the similar properties were named as `lineColor` and `lineWidth`. Now they all have prefix `line*` if it is something about lines or segments and `face*` if it is somthing inside a closed contour. So no more `border*` properties.
52
37
 
53
38
  ## Installation
54
39
 
55
40
  The set up process is similar to any other Svelte component library. Just use:
56
41
 
57
42
  ```
58
- npm -i -D svelte-plots-basic
43
+ npm install svelte-plots-basic
59
44
  ```
60
45
 
61
46
  or, to install it with yarn:
62
47
 
63
48
  ```
64
- yarn add -D svelte-plots-basic
49
+ yarn add svelte-plots-basic
65
50
  ```
66
51
 
67
52
 
68
- ## Quick start (2D plots)
53
+ ## User tutorial (2D plots)
69
54
 
70
- Below you will find several simple examples which help you to start with. It is assumed that you already know the basics of Svelte.
71
55
 
72
- To make a plot, just create a new Svelte app following the [quick start guide](https://svelte.dev/blog/the-easiest-way-to-get-started). Then open `App.svelte` file, delete everything and write the following code, which creates a simple 2D bar chart:
56
+ ### Axes
57
+
58
+ Component `<Axes>` is a main component which does most of the job. It must be a parent element of all other components. It has many properties, full list with explanation is available at the end of this section, meanwhile we consider the three most important ones.
59
+
60
+ Properties `limX` and `limY` define limits of the coordinates system in *plot's coordinates*. Imagine you want to show a bar plot with developing of GDP per capita in Germany during five years — from 2020 to 2024. The values in US dollars are [47342, 52301, 49725, 53565, 55859]. So limits of the x-axis will be for example from 2019 to 2025 to have some space around: `limX={[2019, 2025]}` and the limits for y-axis will be from 0 to 60000: `limY={[0, 60000]}`. Here is a full example:
73
61
 
74
62
  ```svelte
75
63
  <script>
76
- import {Axes, XAxis, YAxis, Box, Bars} from 'svelte-plots-basic/2d';
77
-
78
- // test data for the plot
79
- const years = [2010, 2020, 2030, 2040, 2050];
80
- const amount = [100, 200, 150, 300, -100];
64
+ import { Axes } from 'svelte-plots-basic/2d';
81
65
  </script>
82
66
 
83
- <div class="plot-container">
84
- <Axes limX={[2005, 2055]} limY={[-150, 350]} margins={[1, 1, 0.5, 0.5]}
85
- xLabel="Years" yLabel="Income">
86
-
87
- <!-- series of bars for the defined data -->
88
- <Bars
89
- faceColor="#e0e0e0"
90
- edgeColor="#909090"
91
- xValues={years}
92
- yValues={amount}
93
- />
94
-
95
- <!-- x and y axis with automatic ticks and grid lines -->
96
- <XAxis slot="xaxis" />
97
- <YAxis slot="yaxis" />
98
-
99
- <!-- box around axes -->
100
- <Box slot="box" />
101
-
67
+ <div class="plot-wrapper">
68
+ <Axes limX={[2019, 2025]} limY={[0, 60000]}>
102
69
  </Axes>
103
70
  </div>
104
71
 
105
72
  <style>
106
- .plot-container {
73
+ .plot-wrapper {
107
74
  width: 100%;
108
75
  height: 100%;
109
- min-width: 200px;
110
- min-height: 200px;
111
76
  }
112
77
  </style>
113
78
  ```
114
79
 
115
- Then run `npm run dev` in terminal and open the URL provided by npm in browser. That is it.
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]`.
116
81
 
117
- [This example](https://svelte.dev/repl/ad0f5631137d4a16b3a9b0e9dff23169?version=4.2.18) in Svelte REPL.
82
+ 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.
118
83
 
119
- You can also use all capabilities of the `mdatools` package, e.g. generating random numbers:
84
+ The default values are `[1.0, 1.1, 0.6, 0.6]` and they will fit most of the cases, so it is not necessary to provide your own. These values are picked up assuming that there will be x- and y-axis with ticks and tick labels, thefore the bottom and left margins are bigger than the top and the right.
120
85
 
121
- ```svelte
122
- <script>
123
- import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
124
- import { Vector } from 'mdatools/arrays';
86
+ The real margins in pixels depend on size of the plot on the screen and vary from 30 (small plot) to 60 (extra large) pixels. For example if plot is small, then margin value of `0.6` will lead to `30 * 0.6 = 18` pixels of margin.
125
87
 
126
- // generate random values from normal distribution
127
- const x = Vector.randn(200, 0, 1);
128
- const y = Vector.randn(200, 0, 2);
129
- </script>
88
+ The `Axes` component works by mapping points from the plot coordinates to the screen coordinates. It is reactive, meaning if you change the plot size interactively (e.g. by resizing the browser window), the coordinates will be recomputed and all elements will be redrawn automatically. The figure below show schematically how it works (just remember that in SVG y-coordinates start from top, not from bottom, hence the difference between equations for x- and y-coordinates):
130
89
 
131
- <div class="plot-container">
132
- <Axes limX={[-6, 6]} limY={[-5, 6]} xLabel="x" yLabel="y">
133
- <Points xValues={x} yValues={y} />
134
- <XAxis slot="xaxis" />
135
- <YAxis slot="yaxis" />
136
- <Box slot="box" />
137
- </Axes>
138
- </div>
90
+ ![Coordinate transformation](./assets/coords.png)
139
91
 
140
- <style>
141
- .plot-container {
142
- width: 100%;
143
- height: 100%;
144
- min-width: 200px;
145
- min-height: 200px;
146
- }
147
- </style>
148
- ```
92
+ Finally you can also provide `title` of the plot which is shown on the top outside the main area (shown as gray rectangle in the figure above) thereby it does not affect margins, ticks, etc.
149
93
 
150
- [This example](https://svelte.dev/repl/4763a7b8d49d4e268e1c68e0828403a6?version=4.2.18) in Svelte REPL.
94
+ Here is a table with main properties of `Axes` component.
151
95
 
96
+ Property name | Default value | Description
97
+ --|--|--
98
+ `title` | `''` | title of the plot, shown on top
99
+ `limX` | `[0, 1]` | x-axis limits in plot coordinates
100
+ `limY` | `[0, 1]` | y-axis limits in plot coordinates
101
+ `margins` | `[1.0, 1.1, 0.6, 0.6]` | relative margins (bottom, left, top, right)
152
102
 
153
- Below is a brief description of available components for 2D plots.
154
103
 
104
+ The component also provides functionality for saving plots as SVG or PNG files and/or copying the plot to clipboard as PNG image. In order to activate it one has to set value for property `downloadLinks`. It can be one of the following: `'none'` (default value, hides download buttons), `'hover'` — the toolbar with buttons pops up when user hover mouse cursor over the plot area, and `'fixed'` — the toobar is always shown. By default the toolbar with buttons is shown in the right bottom corener of the plot. This can be changed by amending CSS styles of `.download-links` and `.download-links > button` elements.
155
105
 
156
- ### Axes
106
+ By default the size of the PNG image is 8 x 8 cm. If plot on the screen is not squared (e.g. width is larger than height), it will keep the largest size at 8 cm and adjust the other side automatically to keep the actual aspect ratio. The default resoulition of PNG image is 300 ppi (so the default pixel size is 2400 x 2400 pixels). All three parameters (physical width, height and resolution) can be adjusted by setting corresponding properties of the component.
157
107
 
158
- Main component of any plot, all other components must be located inside `Axes`. You can specify parameters defining the x- and y-axis limits, margins around axes pane (to make space for axis ticks and labels), plot title and axis labels, as well as several parameters for saving plot to graphical files.
108
+ One can also adjust width and height of image, which is copied to clipboard. In this case both parameters are set in pixels.
159
109
 
160
- Here is an example of using the component with all available parameters:
110
+ Here is a table with properties of the component related to saving plot to a file and copying it to clipboard.
161
111
 
162
- ```svelte
163
- <script>
164
- import { Axes } from 'svelte-plots-basic/2d';
165
- </script>
112
+ Property name | Default value | Description
113
+ --|--|--
114
+ `downloadLinks` | `'none'` | show toolbar with download buttons: `'none'`, `'hover'` or `'fixed'`
115
+ `fileName` | `'plot'` | filename for downloaded image file without extention
116
+ `pngWidth` | `8` | size of PNG image in cm
117
+ `pngHeight` | `8` | size of PNG image in cm
118
+ `pngRes` | `300` | resolution of PNG image (pixels per inch)
119
+ `clipboardWidth` | `1200` | width of image in clipboard in pixels
120
+ `clipboardHeight` | `800` | height of image in clipboard in pixels
166
121
 
167
- <Axes
168
- limX={[0, 10]}
169
- limY={[-100, 100]}
170
- title="My super plot"
171
- xLabel="X-axis label"
172
- yLabel="Y-axis label"
173
- margins={[1.0, 0.75, 0.5, 0.5]}
174
-
175
- downloadLinks="hover"
176
- fileName="plot"
177
- pngWidth={8}
178
- pngHeight={8}
179
- pngRes={300}
180
- >
181
- </Axes>
182
- ```
122
+ Please pay attention that most of the browsers enable clipboard functionality only if website is available via HTTPS.
183
123
 
184
- Parameter `margin` must contain four values, relative margins for bottom, left, top and right sides around main plotting area. The larger margin value is the more space available for axis ticks, labels, etc.
185
124
 
186
- The last five parameters are needed to save plot to a file. See corresponding section with more details below.
125
+ Finally, one can also provide callback functions for several mouse events (click, down, up and move). The event is triggered when someone clicks inside the plotting area but outside any other plotting element (e.g. marker, line, rectangle, etc).
187
126
 
188
- [This example](https://svelte.dev/repl/d818a241c85844249b34e75196ac308c?version=4.2.18) in Svelte REPL.
127
+ `<Axes>` is the only component that supports several events, other components support only `onclick`. For every event the component calls a provided callback function with two arguments — x- and y- coordinates of the mouse pointer in plot coordinates (not pixels).
189
128
 
129
+ Here is a table with properties for handling mouse events.
190
130
 
191
- ### Axis
131
+ Property name | Default value | Description
132
+ --|--|--
133
+ `onclick` | | callback for on mouse click event
134
+ `onmousemove` | | callback for on mouse move event
135
+ `onmousedown` | | callback for on mouse down
136
+ `onmouseup` | | callback for on mouse up
192
137
 
193
- Two components, `XAxis` and `YAxis`, add corresponding elements to the plot. Both have one mandatory argument, `slot`, which must have values `"xaxis"` and `"yaxis"` correspondingly. Other parameters let you define manual ticks and tick labels as well as turn on/off grid lines.
194
138
 
195
- Here is an example where component `XAxis` is shown with all available parameters:
139
+ ### Box
196
140
 
197
- ```svelte
198
- <script>
199
- import { Axes, XAxis, YAxis } from 'svelte-plots-basic/2d';
200
- </script>
141
+ By default `Axes` does not show any elements, it simply creates an empty SVG image inside the wrapper element and implements all necessary functionality, like transformation of coordinates, download and copy functionality, etc. In order to add elements you need to put corresponding components inside the `<Axes>...</Axes>`.
201
142
 
202
- <Axes limX={[-6, 6]} limY={[-5, 6]} margins={[1.5, 1.5, 0.5, 0.5]}>
203
- <XAxis
204
- slot="xaxis"
205
- ticks={[-4, 0, 4]}
206
- tickLabels={["before", "now", "after"]}
207
- showGrid={true}
208
- las={2}
209
- lineColor="#a0a0a0"
210
- gridColor="#e0e0e0"
211
- textColor="#ff6666"
212
- />
143
+ The simplest component is `<Box>` which creates a box/frame/rectangle around the plotting area. You can decide thickness and color of the box lines. Here is an example:
144
+
145
+ ```svelte
146
+ <Axes limX={[2019, 2025]} limY={[0, 60000]}>
147
+ <Box />
213
148
  </Axes>
214
149
  ```
215
150
 
216
- The `YAxis` has identical parameters, just remember to change the `slot` value. The parameter `las` can be equal to `1` or `2` and it defines orientation of tick labels (horizontal or vertical).
151
+ The element has only two properties:
217
152
 
218
- [This example](https://svelte.dev/repl/fbc54e9359a84cd39b5e1da0787b7274?version=4.2.18) in Svelte REPL.
153
+ Property name | Default value | Description
154
+ --|--|--
155
+ `lineWidth` | `1` | width/thickness of box line in pixels
156
+ `lineColor` | `'#606060'` | color of the line
219
157
 
220
158
 
221
159
 
222
- ### Box
160
+ ### Axis
223
161
 
224
- Simple component which adds a box (frame) around the main plotting area. Has only one parameter (it is mandatory), `slot`, which must always be `"box"`:
162
+ There are two components to add x- and y-axis: `XAxis` and `YAxis`. Both have identicall set of parameters. Each component adds corresponding axis elements, such as axis line, tick lines located at tick positions/values and tick labels. By default everything is computed automatically based on axis limits. Here is an example:
225
163
 
226
164
  ```svelte
227
- <script>
228
- import { Axes, Box } from 'svelte-plots-basic/2d';
229
- </script>
230
-
231
- <Axes limX={[-6, 6]} limY={[-5, 6]}>
232
- <Box slot="box" />
165
+ <Axes limX={[2019, 2025]} limY={[0, 60000]}>
166
+ <XAxis label="Year" />
167
+ <YAxis label="GDP per capita" />
233
168
  </Axes>
234
169
  ```
235
170
 
236
- [This example](https://svelte.dev/repl/567b716dbe844ba1a79c72f4beff8d3d?version=4.2.18) in Svelte REPL.
237
-
171
+ You can also define tick positions and corresponding labels manually. Tick positions must be provided as array of numeric values. They must be unique, and all values must be inside the axis limits. You can also provide manual tick labels, in this case it should be array of text values. The number of labels should match the number of ticks, which means if you want to provide manual tick labels, you should provide corresponding tick values as well. Here is an example:
238
172
 
239
- ### Points
173
+ ```svelte
174
+ <Axes limX={[2019, 2026]} limY={[0, 60000]}>
175
+ <Xaxis ticks={[2023, 2024, 2025]} tickLabels={['Past', 'Present', 'Future']} />
176
+ </Axes>
177
+ ```
240
178
 
241
- Adds a series of points to a plot. Requires at least two sequence of values, x- and y-coordinates of the points, which can be specified as Javascript array or as a `Vector` instance (class from `mdatools` package).
179
+ Tick and axis labels can include super and subscripts and specifal HTML symbols (e.g. `'&alpha;^2'` or `'&gamma;_(34)'`).
242
180
 
243
- Here is an example of using the component with all available parameters:
181
+ Here is a full set of properties for both components:
244
182
 
245
- ```svelte
246
- <script>
247
- import { Axes, Points } from 'svelte-plots-basic/2d';
248
- </script>
183
+ Property name | Default value | Description
184
+ --|--|--
185
+ `label` | `''` | axis label
186
+ `showGrid` | `false` | show or not grid lines at tick positions
187
+ `ticks` | | tick positions, by default will be computed automatically
188
+ `tickLabels` | | tick labels, by default will be based on tick position values
189
+ `las` | `1` | orientation of tick labels: `1` — for horizontal, `2` — for vertical
190
+ `whole` | `false` | if `true` the automatic tick labels will be shown as whole numbers
249
191
 
250
- <Axes limX={[-4, 4]} limY={[-1, 10]}>
251
- <Points
252
- xValues={[-3, -2, -1, 0, 1, 2, 3]}
253
- yValues={[9, 4, 1, 0, 1, 4, 9]}
254
- marker={1}
255
- faceColor="transparent"
256
- borderColor="#ff0000"
257
- borderWidth={2}
258
- markerSize={2}
259
- title="series1"
260
- />
261
- </Axes>
262
- ```
263
192
 
264
- Parameter `marker` must be a number between 1 and 8, which corresponds to the following marker symbols: `["●", "◼", "▲", "▼", "⬥", "+", "*", "✕"]`. First five markers may have different colors for the border (stroke) and the face (fill) as well as different border width. The last three markers are shown using same color and have fixed border width.
193
+ Check these Svelte REPL examples covering the use of all four components: [plots-axes-simple](https://svelte.dev/playground/d818a241c85844249b34e75196ac308c), [plots-axes-advanced](https://svelte.dev/playground/d06edc89a2d341faba03e0d03f43839f), [plots-axes-mouse](https://svelte.dev/playground/706b66ce008d40c9bcad1e4c89434e23). The last example also uses other elements which are described below.
265
194
 
266
- The size of markers is defined in `"em"` units.
267
195
 
268
- Parameter `title` is needed only if you want to handle click events, for example, to select a particular point. See specific section below with more details below.
269
196
 
270
- [This example](https://svelte.dev/repl/edc090cb1c184fee88aedebd8731a87e?version=4.2.18) in Svelte REPL.
271
197
 
198
+ ### Series
272
199
 
273
- ### Segments
200
+ Series are components that add several items of the same nature (hence series) on the plot. There are seven components in this group, below you will find their short description. Each component has a property `onclick` whose value should be a callback function that will be called when a user clicks on any item of the series. The callback function should have one argument — the index of the item.
274
201
 
275
- Use this component if you want to show a series of line segments. The component has four mandatory parameters — x- and y-coordinates of start and end points of the segments. The coordinates can be provided as Javascript array or as a `Vector` instance.
202
+ #### Points
276
203
 
277
- Here is an example of using the component with all available parameters:
204
+ This component shows a set of points using one of eight pre-defined markers. Also known as scatter series. Here is an example:
278
205
 
279
206
  ```svelte
280
207
  <script>
281
- import { Axes, Segments } from 'svelte-plots-basic/2d';
208
+ import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
209
+ const height = [1.68, 1.72, 1.88, 1.54, 1.79];
210
+ const weight = [70, 69, 90, 56, 74];
282
211
  </script>
283
212
 
284
- <Axes limX={[-4, 4]} limY={[0, 10]}>
285
- <Segments
286
- xStart={[-3, -2, -1, 0, 1, 2, 3]}
287
- yStart={[1, 2, 3, 4, 3, 2, 1]}
288
- xEnd={[-3, -2, -1, 0, 1, 2, 3]}
289
- yEnd={[9, 8, 7, 6, 7, 8, 9]}
290
- lineColor="#ff0000"
291
- lineType={3}
213
+ <div class="plot-wrapper">
214
+ <Axes limX={[1, 2]} limY={[50, 100]} title="People">
215
+
216
+ <Points xValues={height} yValues={weight}
217
+ marker={2}
218
+ lineColor="#3344ff"
292
219
  lineWidth={2}
220
+ faceColor="#3344ff80"
293
221
  />
222
+
223
+ <XAxis label="Height, m", showGrid={true} />
224
+ <YAxis label="Weight, kg" showGrid={true} />
225
+ <Box />
294
226
  </Axes>
227
+ </div>
228
+
229
+ <style>
230
+ .plot-wrapper {
231
+ width: 100%;
232
+ height: 100%;
233
+ }
234
+ </style>
295
235
  ```
296
236
 
297
- Parameter `lineType` can have the following values `1` - solid, `2` - dashed, `3` - dotted, `4` - dash dot lines.
237
+ Check more advanced example in Svelte REPL: [plots-points](https://svelte.dev/playground/edc090cb1c184fee88aedebd8731a87e).
298
238
 
299
- [This example](https://svelte.dev/repl/41285c86fe6e4e6c9abc50fb08faa7d9?version=4.2.18) in Svelte REPL.
300
239
 
240
+ Here is a table with all properties:
301
241
 
302
- ### Rectangles
242
+ Property name | Default value | Description
243
+ --|--|--
244
+ `xValues` | | array or vector with x-coordinates of the points
245
+ `yValues` | | array or vector with y-coordinates of the points
246
+ `marker` | `1` | value between 1 and 8 defininng markers: `●, ◼, ▲, ▼, ⬥, +, *, ✕`
247
+ `markerSize` | `1` | size of the marker symbol in em
248
+ `lineColor` | `'#2679B2'` | color of marker border
249
+ `lineWidth` | `1` | thickness/width of the border in pixels
250
+ `faceColor` | `'transparent'` | color of the face of the marker (only for the first five)
251
+ `onclick` | `null` | callback function for on mouse click event
303
252
 
304
- Use this component if you want to show a series of rectangles. The component has four mandatory parameters — coordinates of left-top corner, width and height of each rectangle. The coordinates and sizes can be provided as Javascript array or as a `Vector` instance.
305
253
 
306
- Here is an example of using the component with all available parameters:
254
+ Last three markers (`+, *, ✕`) do not have face, hence `faceColor` has no effect for them.
255
+
256
+ #### Text labels
257
+
258
+ This component is similar to `<Points>` but lets you put any text at the specified positions. Here is an example, based on the previous case but here we also use `<TextLabels>` to add labels for each point:
307
259
 
308
260
 
309
261
  ```svelte
310
262
  <script>
311
- import { Axes, Rectangles } from 'svelte-plots-basic/2d';
263
+ import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
264
+ const height = [1.68, 1.72, 1.88, 1.54, 1.79];
265
+ const weight = [70, 69, 90, 56, 74];
266
+ const labels = ['Bob', 'Eva', 'John', 'Leya', 'Peter'];
312
267
  </script>
313
268
 
314
- <Axes limX={[-4, 4]} limY={[0, 10]}>
315
- <Rectangles
316
- left={[-3, -2, -1, 0, 1, 2, 3]}
317
- top={[9, 8, 7, 6, 7, 8, 9]}
318
- width={[0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75]}
319
- height={[8, 6, 7, 5, 6, 7, 8]}
320
- faceColor="#ff000080"
321
- borderColor="#ff0000"
322
- lineWidth={2}
323
- />
269
+ <div class="plot-wrapper">
270
+ <Axes limX={[1, 2]} limY={[50, 100]} title="People">
271
+ <Points xValues={height} yValues={weight} />
272
+
273
+ <TextLabels xValues={height} yValues={weight} {labels} pos={3} />
274
+
275
+ <XAxis label="Height, m", showGrid={true} />
276
+ <YAxis label="Weight, kg" showGrid={true} />
277
+ <Box />
324
278
  </Axes>
325
- ```
279
+ </div>
326
280
 
327
- [This example](https://svelte.dev/repl/8ef24c0380474d2a9ff15ab66f2d0572?version=4.2.18) in Svelte REPL.
281
+ <style>
282
+ .plot-wrapper {
283
+ width: 100%;
284
+ height: 100%;
285
+ }
286
+ </style>
287
+ ```
328
288
 
329
- ### Area
289
+ Check this example in Svelte REPL: [plots-textlabels](https://svelte.dev/playground/ae58b8d4e92f4e748a679b60c4876347).
330
290
 
331
- Use this component if you want to show a filled area of arbitrary shape. The component has two mandatory parameters — x- and y-coordinates of the corner points of the area. The coordinates can be provided as Javascript array or as a `Vector` instance.
332
291
 
333
- Here is an example of using the component with all available parameters:
292
+ Here is a full set of properties:
334
293
 
335
- ```svelte
336
- <script>
337
- import { Axes, Area } from 'svelte-plots-basic/2d';
338
- </script>
294
+ Property name | Default value | Description
295
+ --|--|--
296
+ `xValues` | | array or vector with x-coordinates of the labels position
297
+ `yValues` | | array or vector with y-coordinates of the labels position
298
+ `labels` | | text labels (either array or single value for all positions)
299
+ `pos` | `0` | positions of labels related to coordinates (see details)
300
+ `lineColor` | `'transparent'` | color of border of the labels' symbols
301
+ `lineWidth` | `1` | thickness/width of the border in pixels
302
+ `faceColor` | `'#2679B2'` | color of the face of the symbols
303
+ `textSize` | `1` | size of the labels' symbols in em
304
+ `rotateAngle` | `0` | angle in degrees to rotate the labels
305
+ `onclick` | `null` | callback function for on mouse click event
339
306
 
340
- <Axes limX={[0, 6]} limY={[-7, 21]}>
341
- <Area
342
- xValues={[3, 2, 1, 4, 5]}
343
- yValues={[-5, 10, 20, 10, -2]}
344
- fillColor="#ffc00080"
345
- lineColor="#ff0000"
346
- lineWidth={2}
347
- lineType={3}
348
- />
349
- </Axes>
350
- ```
307
+ Both `labels` and `pos` properties can be provided as array or as a single value. The number of elements in array should match the number of coordinates. If single value is provided it will be
308
+ replicated for all coordinates.
351
309
 
352
- [This example](https://svelte.dev/repl/478b77501386469b93a1160e46809644?version=4.2.18) in Svelte REPL.
353
310
 
354
- ### Text labels
355
311
 
356
- This component is similar to `Points` but it let you show any text values on the plot instead of markers. You can specify the location of the values relative to the points, rotation angle and other settings.
312
+ #### Segments
357
313
 
358
- Here is an example of using the component with all available parameters:
314
+ This component shows a series of line segments connecting two points (start and end). Hence it has four mandatory arguments: x- and y-coordinates of start and end points.
359
315
 
360
316
  ```svelte
361
317
  <script>
362
- import { Axes, TextLabels } from 'svelte-plots-basic/2d';
318
+ import { Axes, XAxis, YAxis, Box, Segments } from 'svelte-plots-basic/2d';
319
+ const xStart = [1, 2, 3, 4, 5];
320
+ const yStart = [1, 2, 3, 2, 1];
321
+ const xEnd = [1, 2, 3, 4, 5];
322
+ const yEnd = [9, 7, 6, 7, 9];
363
323
  </script>
364
324
 
365
- <Axes limX={[-4, 4]} limY={[-1, 10]}>
366
- <TextLabels
367
- xValues={[-3, -2, -1, 0, 1, 2, 3]}
368
- yValues={[9, 4, 1, 0, 1, 4, 9]}
369
- labels={["😀", "$$$", "oo", "x", "oo", "$$$", "😀"]}
370
- pos={0}
371
- faceColor="#ffcc0080"
372
- borderColor="#aa0000"
373
- borderWidth={1}
374
- textSize={4}
375
- />
325
+ <div class="plot-wrapper">
326
+ <Axes limX={[0, 6]} limY={[0, 10]} >
327
+
328
+ <Segments {xStart} {yStart} {xEnd} {yEnd} lineWidth={2} lineColor="#ff4422" />
329
+
330
+ <XAxis showGrid={true} />
331
+ <YAxis showGrid={true} />
332
+ <Box />
376
333
  </Axes>
334
+ </div>
335
+
336
+ <style>
337
+ .plot-wrapper {
338
+ width: 100%;
339
+ height: 100%;
340
+ }
341
+ </style>
377
342
  ```
378
343
 
379
- Parameter `pos` defines position of the text label relative to the coordinate of corresponding point. It can be one of the following: `0` (on the point), `1` (under), `2` (on the left side), `3` (over), `4` (on the right side).
344
+ Check this example in Svelte REPL: [plots-segments](https://svelte.dev/playground/41285c86fe6e4e6c9abc50fb08faa7d9).
380
345
 
381
- It can be specified as a single value, like in the example above, or as array of values — individual for each label.
382
346
 
383
- Same about parameter `labels` — it can be a single value for all points or an array of individual values for each point like in the example above.
347
+ Here is a full set of properties:
384
348
 
385
- [This example](https://svelte.dev/repl/ae58b8d4e92f4e748a679b60c4876347?version=4.2.18) in Svelte REPL.
349
+ Property name | Default value | Description
350
+ --|--|--
351
+ `xStart` | | array or vector with x-coordinates of the start points
352
+ `yStart` | | array or vector with y-coordinates of the start points
353
+ `xEnd` | | array or vector with x-coordinates of the end points
354
+ `yEnd` | | array or vector with y-coordinates of the end points
355
+ `lineColor` | `'#2679B2'` | color of the lines
356
+ `lineWidth` | `1` | thickness/width of the lines in pixels
357
+ `lineType` | `1` | line type (`1`- solid, `2` - dashed, `3` - dotted, `4` - dashdot)
358
+ `onclick` | `null` | callback function for on mouse click event
386
359
 
387
360
 
388
- ### Bars
389
361
 
390
- Use this component to add bar series to the plot. You just need to specify x-coordinates of middle points of each bar and y-coordinate of its top.
362
+ #### Lines
363
+
364
+ Lines components is similar to `<Points>` but instead of showing markers, it connects the points with line segments. If the points are close to each other, the result looks like a smooth curve.
391
365
 
392
- Here is an example of using the component with all available parameters:
393
366
 
394
367
  ```svelte
395
368
  <script>
396
- import { Axes, Bars } from 'svelte-plots-basic/2d';
397
- </script>
369
+ import { Axes, XAxis, YAxis, Box, Lines } from 'svelte-plots-basic/2d';
398
370
 
399
- <Axes limX={[-4, 4]} limY={[-1, 10]}>
400
- <Bars
401
- xValues={[-3, -2, -1, 0, 1, 2, 3]}
402
- yValues={[9, 4, 1, 0.1, 1, 4, 9]}
403
- faceColor="#ffcc0080"
404
- borderColor="#ff0000"
405
- borderWidth={2}
406
- />
407
- </Axes>
408
- ```
371
+ const xValues = [-4, -3, -2, -1, 0, 1, 2, 3, 4];
372
+ const yValues = [16, 9, 4, 1, 0, 1, 4, 9, 16];
373
+ </script>
409
374
 
410
- [This example](https://svelte.dev/repl/3afb9d8acd824d64ba1c42226fb01fa9?version=4.2.18) in Svelte REPL.
375
+ <div class="plot-wrapper">
376
+ <Axes limX={[-10, 10]} limY={[-2, 20]} >
411
377
 
378
+ <Lines {xValues} {yValues} lineWidth={2} lineColor="#ff4422" />
412
379
 
413
- ### Lines
380
+ <XAxis showGrid={true} />
381
+ <YAxis showGrid={true} />
382
+ <Box />
383
+ </Axes>
384
+ </div>
414
385
 
415
- Use this component to show lines connected sequence of points (polyline), usually known as line series. Requires two sequence of values, x- and y-coordinates of the points, which can be specified as Javascript array or as a `Vector` instance (class from `mdatools` package).
386
+ <style>
387
+ .plot-wrapper {
388
+ width: 100%;
389
+ height: 100%;
390
+ }
391
+ </style>
392
+ ```
416
393
 
417
- Here is an example of using the component with all available parameters:
394
+ Check this example in Svelte REPL: [plots-lines](https://svelte.dev/playground/2006ca1441f845eb9ed40b3e583d0094).
418
395
 
419
- ```svelte
420
- <script>
421
- import { Axes, Lines } from 'svelte-plots-basic/2d';
422
- </script>
423
396
 
424
- <Axes limX={[-4, 4]} limY={[-1, 10]}>
425
- <Lines
426
- xValues={[-3, -2, -1, 0, 1, 2, 3]}
427
- yValues={[9, 4, 1, 0, 1, 4, 9]}
428
- lineColor="#ff0000"
429
- lineWidth={2}
430
- lineType={3}
431
- />
432
- </Axes>
433
- ```
397
+ Here is a full set of properties:
434
398
 
435
- The line parameters are similar to the ones used in `Segments` component.
399
+ Property name | Default value | Description
400
+ --|--|--
401
+ `xValues` | | array or vector with x-coordinates of the points
402
+ `yValues` | | array or vector with y-coordinates of the points
403
+ `lineColor` | `'#2679B2'` | color of the lines
404
+ `lineWidth` | `1` | thickness/width of the lines in pixels
405
+ `lineType` | `1` | line type similar to `Segments`
406
+ `onclick` | `null` | callback function for on mouse click event
436
407
 
437
- [This example](https://svelte.dev/repl/2006ca1441f845eb9ed40b3e583d0094?version=4.2.18) in Svelte REPL.
438
408
 
439
409
 
440
- ### Multilines
410
+ #### Multilines
441
411
 
442
- This component is similar to `Lines` but it lets you showing multiple lines whose x-coordinates are the same but each line has its own y-coordinates. The parameters are similar to `Lines` component except one — `yValues` must be provided as an instance of `Matrix` class (from `mdatools` package). Every column of this matrix contains y-coordinates of corresponding line.
412
+ This component is similar to `Lines` but it can show multiple lines whose x-coordinates are the same and each line has its own y-coordinates. Most of the properties are identical the properties of `Lines` except one — `yValues` must be provided as an instance of `Matrix` class (from `mdatools` package). Every column of this matrix contains y-coordinates of corresponding line.
443
413
 
444
414
  Here is an example of using the component with all available parameters:
445
415
 
446
416
  ```svelte
447
417
  <script>
448
- import { Axes, Multilines } from 'svelte-plots-basic/2d';
418
+ import { Axes, XAxis, YAxis, Box, Multilines } from 'svelte-plots-basic/2d';
449
419
  import { Vector, cbind } from 'mdatools/arrays';
450
420
 
451
421
  // create x-values
452
- const x = Vector.seq(0, 15, 0.1);
422
+ const xValues = Vector.seq(0, 15, 0.1);
453
423
 
454
424
  // compute y-values for four lines
455
- const y1 = x.apply(v => Math.sin(v));
425
+ const y1 = xValues.apply(v => Math.sin(v));
456
426
  const y2 = y1.add(0.2);
457
427
  const y3 = y2.add(0.2);
458
428
  const y4 = y3.add(0.2);
459
429
 
460
- // combine y-values into a matrix
461
- const Y = cbind(y1, y2, y3, y4);
430
+ // combine y-values so they form columns of matrix Y
431
+ const yValues = cbind(y1, y2, y3, y4);
462
432
  </script>
463
433
 
464
- <Axes limX={[0, 15]} limY={[-2, 2]}>
465
- <Multilines
466
- xValues={x}
467
- yValues={Y}
468
- lineColor="#ff0000"
469
- lineWidth={2}
470
- lineType={1}
471
- />
434
+ <div class="plot-wrapper">
435
+ <Axes limX={[-1, 16]} limY={[-2,2]} >
436
+
437
+ <Multilines {xValues} {yValues} lineWidth={2} lineColor="#ff4422" />
438
+
439
+ <XAxis showGrid={true} />
440
+ <YAxis showGrid={true} />
441
+ <Box />
472
442
  </Axes>
443
+ </div>
444
+
445
+ <style>
446
+ .plot-wrapper {
447
+ width: 100%;
448
+ height: 100%;
449
+ }
450
+ </style>
473
451
  ```
474
452
 
475
- [This example](https://svelte.dev/repl/d642a8fb78fd4a4f8381438d9bf427a2?version=4.2.18) in Svelte REPL.
453
+ Check this example in Svelte REPL: [plots-multilines](https://svelte.dev/playground/d642a8fb78fd4a4f8381438d9bf427a2).
476
454
 
477
455
 
478
- ### Legend
456
+ #### Rectangles
479
457
 
480
- This component makes sense to use if you show several series on the same plot. It has two arguments — a position of the legend and array with JSON objects specifying legend text and parameters of the corresponding series.
458
+ Use this component if you want to show a series of rectangles. The component has four mandatory parameters — coordinates of left-top corner, width and height of each rectangle.
481
459
 
482
- Here is an example:
483
460
 
484
461
  ```svelte
485
462
  <script>
486
- import { Axes, Lines, Points, Legend } from 'svelte-plots-basic/2d';
487
- import {Vector} from 'mdatools/arrays';
463
+ import { Axes, XAxis, YAxis, Box, Rectangles } from 'svelte-plots-basic/2d';
488
464
 
489
- // create x-values
490
- const x = Vector.seq(-5, 5, 0.1);
465
+ const left = [-3, -2, -1, 0, 1, 2, 3];
466
+ const top = [9, 8, 7, 6, 7, 8, 9];
467
+ const width = [0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75];
468
+ const height = [8, 6, 4, 2, 4, 6, 8];
491
469
 
492
- // compute y-values
493
- const y1 = x.apply(v => Math.pow(v, 2));
494
- const y2 = x.apply(v => Math.pow(v, 3));
495
- const y3 = x.apply(v => Math.pow(v, 4));
496
470
  </script>
497
471
 
498
- <Axes limX={[-5, 5]} limY={[-20, 20]}>
499
-
500
- <!-- series 1: dotted red line -->
501
- <Lines xValues={x} yValues={y1} lineColor="red" lineType={3} />
502
-
503
- <!-- series 2: solid blue line and circle markers -->
504
- <Lines xValues={x} yValues={y2} lineColor="blue" lineType={1} />
505
- <Points xValues={x} yValues={y2} borderColor="blue" faceColor="white"/>
506
-
507
- <!-- series 3: markers in form of diamonds with green stroke and yellow fill -->
508
- <Points xValues={x} yValues={y3} marker={5} faceColor="yellow" borderColor="green" />
472
+ <div class="plot-wrapper">
473
+ <Axes limX={[-4, 4]} limY={[0, 10]}>
509
474
 
510
- <!-- legend with one JSON for each series -->
511
- <Legend
512
- position="right"
513
- items = {[
514
- {"label": 'y=x^2', "lineType": 3, "lineColor": "red"},
515
- {"label": "y=x^3", "lineType": 1, "lineColor": "blue", "marker": 1, "faceColor": "white", "borderColor": "blue"},
516
- {"label": "y=x^4", "marker": 5, "faceColor": "yellow", "borderColor": "green"},
517
- ]}
475
+ <Rectangles {left} {top} {width} {height}
476
+ faceColor="#ff000080"
477
+ lineColor="#ff0000"
478
+ lineWidth={2}
518
479
  />
480
+
481
+ <XAxis showGrid={true} />
482
+ <YAxis showGrid={true} />
483
+ <Box />
519
484
  </Axes>
485
+ </div>
486
+
487
+ <style>
488
+ .plot-wrapper {
489
+ width: 100%;
490
+ height: 100%;
491
+ }
492
+ </style>
520
493
  ```
494
+ Check more advanced example in Svelte REPL: [plots-rectangles](https://svelte.dev/playground/8ef24c0380474d2a9ff15ab66f2d0572).
521
495
 
522
- The `position` parameter can be one of the follows: `"topleft"`, `"top"`, `"topright"`, `"right"`, `"bottomright"` and so on.
523
496
 
524
- [This example](https://svelte.dev/repl/1c5e21e2153a4229afb630809fe545bd?version=4.2.18) in Svelte REPL.
497
+ Here is a table with all properties:
525
498
 
526
- ### Heatmap
499
+ Property name | Default value | Description
500
+ --|--|--
501
+ `left` | | array or vector with coordinates of the left side of the rectangles
502
+ `top` | | array or vector with coordinates of the top side of the rectangles
503
+ `height` | | single value, array or vector with height values of the rectangle
504
+ `width` | | single value, array or vector with width values of the rectangle
505
+ `lineColor` | `'#2679B2'` | color of border line
506
+ `lineWidth` | `1` | thickness/width of the border in pixels
507
+ `faceColor` | `'transparent'` | color of the face of the marker (only for first five)
508
+ `onclick` | `null` | callback function for on mouse click event
527
509
 
528
- This component lets you visualizing values of a matrix (instance of class `Matrix`). Matrix with values is the only mandatory parameter for the component, the other two are optional.
510
+ The `left` and `top` properties must be provided as vector or array. But `height` or/and `width` can be provided as single values, in this case all rectangles will have the same height and width.
529
511
 
530
- ```svelte
531
- <script>
532
- import { Axes, Heatmap } from 'svelte-plots-basic/2d';
533
- import { matrix, vector } from 'mdatools/arrays';
534
512
 
535
- // create values for a matrix
536
- const values = matrix([0.9, 0.7, 0.5, 0.3, 0.9, 0.1, 0.1, 0.5, 0.9], 3, 3);
513
+ #### Bars
514
+
515
+ Adds series of bars. The properties are similar to `Rectangles` but you have to provide only x- and y-coordinates of the top (or bottom if value is negative) sides of the bar.
537
516
 
538
- // create vector of breaks
539
- const breaks = vector([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]);
540
517
 
541
- // colmap
542
- const colmap = ["blue", "green", "yellow", "orange", "red"]
518
+ ```svelte
519
+ <script>
520
+ import { Axes, XAxis, YAxis, Box, Bars } from 'svelte-plots-basic/2d';
521
+
522
+ const xValues = [2020, 2021, 2022, 2023, 2024];
523
+ const yValues = [47342, 52301, 49725, 53565, 55859];
543
524
  </script>
544
525
 
545
- <Axes limX={[0, 4]} limY={[0, 4]}>
546
- <Heatmap
547
- {values}
548
- {breaks}
549
- {colmap}
526
+ <div class="plot-wrapper">
527
+ <Axes limX={[2019, 2025]} limY={[0, 60000]} title="GDP per capita">
528
+
529
+ <Bars {xValues} {yValues}
530
+ faceColor="#0000ff80"
531
+ lineColor="#0000ff"
532
+ lineWidth={1}
550
533
  />
534
+
535
+ <XAxis label="Year" />
536
+ <YAxis label="GDP per capita, USD" />
537
+ <Box />
551
538
  </Axes>
539
+ </div>
540
+
541
+ <style>
542
+ .plot-wrapper {
543
+ width: 100%;
544
+ height: 100%;
545
+ }
546
+ </style>
552
547
  ```
548
+ Check more advanced example in Svelte REPL: [plots-bars](https://svelte.dev/playground/3afb9d8acd824d64ba1c42226fb01fa9).
553
549
 
554
- Parameter `breaks` defines vector (instance of `Vector` class) with interval breaks. If a value from the matrix `values` falls into one of the intervals defined by `breaks`, it will be shown using a corresponding color from the `colormap` parameter. For example if value is between 0.2 and 0.4 it will be shown as green rectangle in the example above.
550
+ Here is a table with all properties:
555
551
 
556
- Both `breaks` and `colmap` are defined automatically by default.
552
+ Property name | Default value | Description
553
+ --|--|--
554
+ `xValues` | | array or vector with coordinates of the middle point of the bars
555
+ `yValues` | | array or vector with coordinates of the top/bottom point of the bars
556
+ `lineColor` | `'#2679B2'` | color of border line
557
+ `lineWidth` | `1` | thickness/width of the border in pixels
558
+ `faceColor` | `'#2679B2'` | color of the face of the marker (only for first five)
559
+ `barWidth` | `0.8` | width of each par as per cent of maximum possible width
560
+ `onclick` | `null` | callback function for on mouse click event
557
561
 
558
- [This example](https://svelte.dev/repl/fde87fe5dad24c419314eeb7b9fa469f?version=4.2.18) in Svelte REPL.
559
562
 
563
+ ### Elements
560
564
 
561
- ### Colormap legend
565
+ Elements are components that add one single item to the plot. There are four of them, `<Area>` and `<Heatmap>` are used to add plotting elements based on data values, while `<Legend>` and `<ColormapLegend>` are service elements. Here is a short description of each.
562
566
 
563
- This component is useful when you have series of points or other primitives (including heatmap) shown using different colors. Especially if the colors are sequential.
567
+ #### Area
564
568
 
565
- Here is an example from above with added `ColormapLegend` component.
569
+ This component shows a polygon defined by a series of points. The points are connected by lines (including the connection between the first and the last points) and one can define the characteristics of the line (width, color, type), as well as color of the polygon face. The coordinates can be provided as an array or vector of values.
570
+
571
+ Here is an example:
566
572
 
567
573
  ```svelte
568
574
  <script>
569
- import { Axes, Heatmap, ColormapLegend } from 'svelte-plots-basic/2d';
570
- import { matrix, vector } from 'mdatools/arrays';
571
-
572
- // create values for a matrix
573
- const values = matrix([0.9, 0.7, 0.5, 0.3, 0.9, 0.1, 0.1, 0.5, 0.9], 3, 3);
575
+ import { Axes, Xaxis, YAxis, Box, Area } from 'svelte-plots-basic/2d';
574
576
 
575
- // create vector of breaks
576
- const breaks = vector([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]);
577
+ const xValues = [3, 2, 2.5, 4, 5.5, 6, 5];
578
+ const yValues = [1, 3, 4, 5, 4, 3, 1];
577
579
 
578
- // colmap
579
- const colmap = ["blue", "green", "yellow", "orange", "red"]
580
580
  </script>
581
581
 
582
- <Axes limX={[0, 4]} limY={[0, 4]}>
583
- <Heatmap
584
- {values}
585
- {breaks}
586
- {colmap}
587
- />
582
+ <div class="plot-wrapper">
583
+ <Axes limX={[0, 6]} limY={[0, 7]}>
584
+
585
+ <Area {xValues} {yValues} lineColor="blue" lineWidth={2} faceColor="#3344ff80" />
588
586
 
589
- <ColormapLegend {breaks} {colmap} />
587
+ <XAxis showGrid={true} />
588
+ <YAxis showGrid={true} />
589
+ <Box />
590
590
  </Axes>
591
+ </div>
592
+
593
+ <style>
594
+ .plot-wrapper {
595
+ width: 100%;
596
+ height: 100%;
597
+ }
598
+ </style>
591
599
  ```
592
600
 
593
- [This example](https://svelte.dev/repl/f91380c18b3346599b45baf06b9b8819?version=4.2.18) in Svelte REPL.
601
+ Check this example in Svelte REPL: [plots-area](https://svelte.dev/playground/478b77501386469b93a1160e46809644).
602
+
594
603
 
595
- In addition to two mandatory parameters, `breaks` and `colmap`, it has the following optional parameters:
604
+ Here is a table with parameters:
596
605
 
597
- * `decNum` — number of decimals to show the breaks with (by default 1).
598
- * `labels` — optional vector with labels for interval boundaries (instead of breaks values).
599
- * `labelColor` — color of the label values (by default `'#909090'`).
600
- * `fontSize`— font size for the labels in em (by default `0.85`).
606
+ Property name | Default value | Description
607
+ --|--|--
608
+ `xValues` | | array or vector with x-coordinates of points
609
+ `yValues` | | array or vector with y-coordinates of points
610
+ `lineColor` | `'#2679B2'` | color of line connecting the points
611
+ `lineWidth` | `2` | thickness/width of the line in pixels
612
+ `lineType` | `1` | number from 1 to 4 defining solid, dashed, dotted and dashdotted line
613
+ `faceColor` | `'transparent'` | color of the face inside the polygon
614
+ `opacity` | `1` | opacity value for both face and line color
615
+ `onclick` | `null` | callback function for on mouse click event
601
616
 
617
+ The opacity of separate line or face colors can be defined in the color value, e.g. `#ff000050`.
602
618
 
603
- ### Handle mouse events
604
619
 
605
- Elements of points and bar series as well as axes component. When user clicks on axes area outside any series elements, the `Axes` component dispatches event `'axesclick'`.
620
+ #### Heatmap
606
621
 
607
- When user clicks on element (marker) of `Points` component it dispatches `'markerclick'` event supplements with data value which correspond to the marker index (position of the values with marker coordinates).
622
+ Heatmap is used to visualze values of a matrix, hence the values for this component must be provided as object/instance of class `Matrix` from `mdatools` package.
608
623
 
609
- Here is an example which utilizes this functionality to select a point (marker) when user clicks on it and removes the selection if user clicks outside the markers (but inside the plotting area).
624
+ Optionally you can also provide property `breaks` — list of interval boundaries you want to bin the values into, and `colmap` — list of colors associated with each interval. If you do not provide breaks, they will be computed automatically based on the provided values (the component splits them into 12 intervals evenly distrubuted between the smallest and the largest values).
625
+
626
+ Then the component visualizes the values as a rectangular grid/table, where every cell has a color depending on which interval a value corresponding to this cell is fallen into.
627
+
628
+ Here is an example:
610
629
 
611
630
  ```svelte
612
631
  <script>
613
- import { Axes, XAxis, YAxis, Box, Points, Lines, TextLabels } from 'svelte-plots-basic/2d';
632
+ import { Axes, Xaxis, YAxis, Box, Heatmap } from 'svelte-plots-basic/2d';
633
+ import { Matrix } from 'mdatools/arrays';
614
634
 
615
- // values for series
616
- const x = [2000, 2001, 2002, 2003];
617
- const y = [179.7, 185.3, 189.0, 193.5]
635
+ // create 5 x 10 matrix filled with normally distributed random values
636
+ const values = Matrix.randn(5, 10);
618
637
 
619
- // currently selected bar
620
- let selected = -1;
638
+ // create list with boundaries for 6 intervals
639
+ const breaks = [-3, -2, -1, 0, 1, 2, 3];
621
640
 
622
- // handler click event on marker
623
- function selectMarker(e) {
624
- const id = parseInt(e.detail.elementID)
625
- if (id >= 0) {
626
- selected = Number.parseFloat(e.detail.elementID);
627
- }
628
- }
641
+ // create a list with colors for the intervals
642
+ const colmap = ['blue', 'cyan', 'green', 'yellow', 'orange', 'red'];
643
+ </script>
629
644
 
630
- // handler click event on axes
631
- function resetSelection(e) {
632
- selected = -1;
645
+ <div class="plot-wrapper">
646
+ <Axes limX={[0.5, 15.5]} limY={[0.5, 5.5]}>
647
+
648
+ <Heatmap {values} {breaks} {colmap} />
649
+
650
+ <XAxis whole={true} />
651
+ <YAxis whole={true} />
652
+ <Box />
653
+ </Axes>
654
+ </div>
655
+
656
+ <style>
657
+ .plot-wrapper {
658
+ width: 100%;
659
+ height: 100%;
633
660
  }
634
- </script>
661
+ </style>
662
+ ```
635
663
 
636
- <Axes limX={[1999, 2004]} limY={[0, 220]}
637
- xLabel="Years" yLabel="bn US$ PPP" title="GDP of Denmark"
638
- on:markerclick={selectMarker}
639
- on:axesclick={resetSelection}
640
- >
664
+ Check this example in Svelte REPL: [plots-heatmap](https://svelte.dev/playground/fde87fe5dad24c419314eeb7b9fa469f).
641
665
 
642
- <Lines xValues={x} yValues={y} />
643
- <Points xValues={x} yValues={y} markerSize="1.5" faceColor="#fff" borderWidth="2"/>
666
+ Here is a table with parameters:
644
667
 
645
- {#if selected > -1}
646
- <Points xValues={[x[selected]]} yValues={[y[selected]]} markerSize="1.6" faceColor="#ffcc00"
647
- borderColor="crimson" borderWidth="2"/>
648
- <TextLabels xValues={[x[selected]]} yValues={[y[selected]]} labels={[y[selected]]} pos={3} />
649
- {/if}
668
+ Property name | Default value | Description
669
+ --|--|--
670
+ `values` | | matrix with values (mandatory)
671
+ `breaks` | | optional, list or vector with interval boundaries
672
+ `colmap` | | optional, list of colors for each interval
650
673
 
651
- <XAxis slot="xaxis" ticks={x}/>
652
- <YAxis slot="yaxis" showGrid={true} />
653
- <Box slot="box" />
674
+ Naturally, number of breaks is by one larger than the number of intervals and hence number of colors.
654
675
 
655
- </Axes>
656
- ```
657
676
 
658
- Play with [this example](https://svelte.dev/repl/e4705e10f1604e4cabc21f5543376717?version=4.2.18) in Svelte REPL.
677
+ #### Colormap legend
678
+
679
+ Colormap legend can be used together with `<Heatmap>` or any other color grouping case, when you need to show a legend which match colors and corresponding values. Here is an example how it can be used with `<Heatmap>` (note that y-limits were adjusted to give place for the legend):
680
+
659
681
 
682
+ ```svelte
683
+ <script>
684
+ import { Axes, Xaxis, YAxis, Box, Heatmap, ColormapLegend } from 'svelte-plots-basic/2d';
685
+ import { Matrix } from 'mdatools/arrays';
686
+
687
+ // create 5 x 10 matrix filled with normally distributed random values
688
+ const values = Matrix.randn(5, 10);
689
+
690
+ // create list with boundaries for 6 intervals
691
+ const breaks = [-3, -2, -1, 0, 1, 2, 3];
692
+
693
+ // create a list with colors for the intervals
694
+ const colmap = ['blue', 'cyan', 'green', 'yellow', 'orange', 'red'];
695
+ </script>
660
696
 
661
- ### Save plot to a file
697
+ <div class="plot-wrapper">
698
+ <Axes limX={[0.5, 15.5]} limY={[0.5, 6.5]}>
699
+
700
+ <ColormapLegend {breaks} {colmap} />
701
+
702
+ <Heatmap {values} {breaks} {colmap} />
703
+ <XAxis whole={true} />
704
+ <YAxis whole={true} />
705
+ <Box />
706
+ </Axes>
707
+ </div>
708
+
709
+ <style>
710
+ .plot-wrapper {
711
+ width: 100%;
712
+ height: 100%;
713
+ }
714
+ </style>
715
+ ```
662
716
 
663
- From version *2.3.0* it is possible to save any plot as an SVG or PNG file. In order to use this you need to add additional parameter to `Axes` component, `downloadLinks`. This parameter may have the following values:
717
+ By default labels are set based on the interval boundaries (breaks) but you can also provide array with manual labels. If number of labels is the same as number of breaks (by one larger than number of colors), they will be shown between the colored rectangles. If the number is the same, then they will be shown in the middle.
664
718
 
665
- * `"none"` — turns this functionality off (default value).
666
- * `"hover"` — buttons with download options appear when user hovers mouse over the plot.
667
- * `"fixed"` — buttons with download options are always shown.
719
+ Check these two Svelte REPL apps for example: [plots-heatmap-colormap](https://svelte.dev/playground/52ba71622f554155a56755b987598ce5), [plots-colormap](https://svelte.dev/playground/637311032f584f9d9fc4b324db4f9f49)
668
720
 
669
- You can also specify parameter `fileName` which should be the desired filename for the plot without extension (e.g. `fileName="myplot"`).
670
721
 
671
- In case of SVG file, the plot is downloaded as is. In case of PNG the plot is being rasterized and you can define additional options, also as parameters of `Axes` component:
722
+ #### Legend
672
723
 
673
- * `pngWidth` — width of PNG image in cm (default is 8 cm).
674
- * `pngHeight` — height of PNG image in cm (default is 8 cm).
675
- * `pngRes` — resolution of the PNG image in pixels per inch (default is 300).
724
+ This component makes sense to use if you show several series on the same plot and want to annotate them. It has two main arguments — a position of the legend and array with JSON objects specifying legend text and parameters of the corresponding series.
676
725
 
677
- If the first two parameters do not match the aspect ratio of the plot they will be adjusted accordingly.
726
+ Every item should contain a mandatory field `label` — with text label for the legend item, and, optionally two additional fields: `point` — with properties of point and `line` — with properties of line. If you have other elements, e.g. segments, bars, areas, in the legend they can be still defined by a marker (`point`) or/and by a line (`line`).
678
727
 
679
- Here is a full example:
728
+ Here is an example:
680
729
 
681
730
  ```svelte
682
731
  <script>
683
- import { Axes, XAxis, YAxis, Box, BarSeries } from 'svelte-plots-basic/2d';
684
- import { Vector } from 'mdatools/arrays';
732
+ import { Axes, Points, XAxis, YAxis, Box, Lines, Legend } from 'svelte-plots-basic/2d';
733
+ import {Vector} from 'mdatools/arrays';
685
734
 
686
- // generate random values from normal distribution
687
- const x = Vector.randn(200, 0, 1);
688
- const y = Vector.randn(200, 0, 2);
735
+ // create x-values
736
+ const x = Vector.seq(-5, 5, 0.1);
737
+
738
+ // compute y-values
739
+ const y1 = x.apply(v => Math.pow(v, 2));
740
+ const y2 = x.apply(v => Math.pow(v, 3));
741
+ const y3 = x.apply(v => Math.pow(v, 4));
742
+
743
+ // define properties of each series
744
+ line1Props = {lineType: 3, lineColor: 'red'};
745
+ line2Props = {lineColor: 'blue', lineType: 1};
746
+ point2Props = {lineColor: 'blue', faceColor: 'white'};
747
+ point3Props = {marker: 5, lineColor: 'green', faceColor: 'yellow'};
748
+
749
+ // legend items
750
+ items = [
751
+ {label: 'y=x^2', line: lines1Props },
752
+ {label: "y=x^3", line: lines2Props, point: points2Props },
753
+ {label: "y=x^4", point: points2Props },
754
+ ];
689
755
  </script>
690
756
 
691
- <div class="plot-container">
692
- <Axes
693
- limX={[-6, 6]} limY={[-5, 6]}
694
- xLabel="x" yLabel="y"
695
- downloadLinks="hover"
696
- fileName="myplot"
697
- pngWidth={5}
698
- pngHeight={5}
699
- pngRes={300}
700
- >
701
-
702
- <ScatterSeries xValues={x} yValues={y} />
703
- <XAxis slot="xaxis" />
704
- <YAxis slot="yaxis" />
705
- <Box slot="box" />
706
- </Axes>
707
- </div>
757
+ <Axes limX={[-5, 5]} limY={[-20, 20]}>
758
+
759
+ <!-- series 1: dotted red line -->
760
+ <Lines xValues={x} yValues={y1} {...line1Props} />
761
+
762
+ <!-- series 2: solid blue line and circle markers -->
763
+ <Lines xValues={x} yValues={y2} {...line2Props} />
764
+ <Points xValues={x} yValues={y2} {...point2Props} />
765
+
766
+ <!-- series 3: markers in form of diamonds with green stroke and yellow fill -->
767
+ <Points xValues={x} yValues={y3} {...points3Props} />
768
+
769
+ <!-- legend with one JSON for each series -->
770
+ <Legend position="right" {items} />
771
+
772
+ <XAxis showGrid={true} />
773
+ <YAxis showGrid={true} />
774
+ <Box />
775
+ </Axes>
708
776
  ```
777
+ Check this example in Svelte REPL: [plots-legend](https://svelte.dev/playground/1c5e21e2153a4229afb630809fe545bd) in Svelte REPL.
709
778
 
710
- [This example](https://svelte.dev/repl/29a021768ca14496a1f659f186958a9a?version=4.2.18) in Svelte REPL.
779
+ The `position` parameter can be one of the follows: `'topleft'`, `'top'`, `'topright'`, `'right'`, `'bottomright'` and so on.
711
780
 
712
- ## Quick start (3D plots)
781
+ The component has also properties which changes it apearance, here is the full list:
782
+
783
+ Property name | Default value | Description
784
+ --|--|--
785
+ `items` | | array with JSON properties of legend items
786
+ `position` | `'topleft'` | position of the legend element inside plotting area
787
+ `lineColor` | `'#303030'` | color of the legend box line
788
+ `lineWidth` | `1` | width (thickness) of the legend box line
789
+ `faceColor` | `'#fff'` | background color of the legend box
790
+ `fontSize` | `0.85` | font size for labels in em
791
+
792
+
793
+ ## User tutorial (3D plots)
713
794
 
714
795
  3D plots can be created similar to 2D plots but its components must be imported from `svelte-plots-basic/3d`. Plus all elements must have three coordinates (x, y and z). Plot elements include axes pane, x-, y- and z-axis, as well as points, lines, segments and mesh series.
715
796
 
716
797
  The plots are also made as SVG elements, by using [isometric projection](https://en.wikipedia.org/wiki/Isometric_projection). The orientation of the projection plane is defined by parameters `phi` and `theta`. Parameter `zoom` defines the distance between the plane and the scene.
717
798
 
799
+ The 3D plots can also be downloaded or copied to clipboard, however mouse events are directly not supported yet. However you can add mouse and keyboard support extrentally.
800
+
718
801
  Here is an example of simple 3D scatter plot:
719
802
 
720
803
  ```svelte
@@ -735,14 +818,16 @@ Here is an example of simple 3D scatter plot:
735
818
 
736
819
  <Axes limX={[-10, 10]} limY={[-10, 10]} limZ={[-10, 10]} {zoom} {phi} {theta}>
737
820
  <Points {xValues} {yValues} {zValues} />
738
- <XAxis showGrid={true} title="X" slot="xaxis" />
739
- <YAxis showGrid={true} title="Y" slot="yaxis" />
740
- <ZAxis showGrid={true} title="Z" slot="zaxis" />
821
+
822
+ <XAxis showGrid={true} title="X" />
823
+ <YAxis showGrid={true} title="Y" />
824
+ <ZAxis showGrid={true} title="Z" />
741
825
  </Axes>
742
826
  ```
743
827
 
744
- [This example](https://svelte.dev/repl/2294eecba7d7477ab9a09c1734d32ac2?version=4.2.18) in Svelte REPL.
745
-
746
828
  You can also add `Lines`, `Segments` and `Mesh` series to 3D plots.
747
829
 
830
+ Check more advanced example in Svelte REPL with mouse and keyboard support, and more: [plots-3d]()
831
+
832
+
748
833