svelte-plots-basic 2.2.4 → 2.3.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
@@ -2,9 +2,22 @@
2
2
 
3
3
  The library is under development and breaking changes may occur in the coming versions.
4
4
 
5
+ ## Showcase
6
+
7
+ These websites and web-applications use `svelte-plots-basic` library:
8
+
9
+ * [graasta.com](https://graasta.com) — interactive web-apps for learning statistics and beyond.
10
+ * [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.
11
+
5
12
  ## News
6
13
 
7
- 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).
14
+
15
+ ### 2.3.0
16
+
17
+ * added possibility to save plot as SVG and PNG files (see details below).
18
+ * improved handling of markers in `Points` component.
19
+ * small improvements and bug fixes.
20
+ * added more detailed description with code example both here and Svelte REPL (see below).
8
21
 
9
22
  ### 2.2.0
10
23
 
@@ -17,9 +30,12 @@ New major release (v. 2.0.0) introduces many breaking changes as the library was
17
30
  * 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.
18
31
  * Small improvements and bug fixes.
19
32
 
33
+ ### 2.0.0
34
+ 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).
35
+
20
36
  ## Description
21
37
 
22
- `svelte-plots-basic` is a [Svelte](https://svelte.dev) component library for creating simple 2D and 3D plots/charts. The plots are created by generating [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) inside HTML document and are re-scalable.
38
+ `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.
23
39
 
24
40
  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.
25
41
 
@@ -41,13 +57,13 @@ yarn add -D svelte-plots-basic
41
57
 
42
58
  ## Quick start (2D plots)
43
59
 
44
- It is assumed that you already know the basics of Svelte.
60
+ Below you will find several simple examples which help you to start with. It is assumed that you already know the basics of Svelte.
45
61
 
46
- 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:
62
+ 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:
47
63
 
48
64
  ```svelte
49
65
  <script>
50
- import {Axes, XAxis, YAxis, Box, BarSeries} from 'svelte-plots-basic';
66
+ import {Axes, XAxis, YAxis, Box, Bars} from 'svelte-plots-basic/2d';
51
67
 
52
68
  // test data for the plot
53
69
  const years = [2010, 2020, 2030, 2040, 2050];
@@ -55,20 +71,22 @@ Just create a new Svelte app following the [quick start guide](https://svelte.de
55
71
  </script>
56
72
 
57
73
  <div class="plot-container">
58
- <Axes limX={[2005, 2055]} limY={[-150, 350]} margins={[1, 1, 0.5, 0.5]}xLabel="Years" yLabel="Income">
74
+ <Axes limX={[2005, 2055]} limY={[-150, 350]} margins={[1, 1, 0.5, 0.5]}
75
+ xLabel="Years" yLabel="Income">
59
76
 
60
- <BarSeries
77
+ <!-- series of bars for the defined data -->
78
+ <Bars
61
79
  faceColor="#e0e0e0"
62
80
  edgeColor="#909090"
63
81
  xValues={years}
64
82
  yValues={amount}
65
83
  />
66
84
 
67
- // x and y axis with automatic ticks and grid lines
85
+ <!-- x and y axis with automatic ticks and grid lines -->
68
86
  <XAxis slot="xaxis" />
69
87
  <YAxis slot="yaxis" />
70
88
 
71
- // box around the axes
89
+ <!-- box around axes -->
72
90
  <Box slot="box" />
73
91
 
74
92
  </Axes>
@@ -86,11 +104,13 @@ Just create a new Svelte app following the [quick start guide](https://svelte.de
86
104
 
87
105
  Then run `npm run dev` in terminal and open the URL provided by npm in browser. That is it.
88
106
 
89
- You can also use all capabilities of the `mdatools` package, e.g. generated random numbers:
107
+ [This example](https://svelte.dev/repl/ad0f5631137d4a16b3a9b0e9dff23169?version=4.2.18) in Svelte REPL.
108
+
109
+ You can also use all capabilities of the `mdatools` package, e.g. generating random numbers:
90
110
 
91
111
  ```svelte
92
112
  <script>
93
- import { Axes, BarSeries } from 'svelte-plots-basic';
113
+ import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
94
114
  import { Vector } from 'mdatools/arrays';
95
115
 
96
116
  // generate random values from normal distribution
@@ -100,26 +120,619 @@ You can also use all capabilities of the `mdatools` package, e.g. generated rand
100
120
 
101
121
  <div class="plot-container">
102
122
  <Axes limX={[-6, 6]} limY={[-5, 6]} xLabel="x" yLabel="y">
123
+ <Points xValues={x} yValues={y} />
124
+ <XAxis slot="xaxis" />
125
+ <YAxis slot="yaxis" />
126
+ <Box slot="box" />
127
+ </Axes>
128
+ </div>
103
129
 
104
- <ScatterSeries
105
- xValues={x}
106
- yValues={y}
107
- />
130
+ <style>
131
+ .plot-container {
132
+ width: 100%;
133
+ height: 100%;
134
+ min-width: 200px;
135
+ min-height: 200px;
136
+ }
137
+ </style>
138
+ ```
139
+
140
+ [This example](https://svelte.dev/repl/4763a7b8d49d4e268e1c68e0828403a6?version=4.2.18) in Svelte REPL.
141
+
142
+
143
+ Below is a brief description of available components for 2D plots.
144
+
145
+
146
+ ### Axes
147
+
148
+ 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.
149
+
150
+ Here is an example of using the component with all available parameters:
151
+
152
+ ```svelte
153
+ <script>
154
+ import { Axes } from 'svelte-plots-basic/2d';
155
+ </script>
156
+
157
+ <Axes
158
+ limX={[0, 10]}
159
+ limY={[-100, 100]}
160
+ title="My super plot"
161
+ xLabel="X-axis label"
162
+ yLabel="Y-axis label"
163
+ margins={[1.0, 0.75, 0.5, 0.5]}
164
+
165
+ downloadLinks="hover"
166
+ fileName="plot"
167
+ pngWidth={8}
168
+ pngHeight={8}
169
+ pngRes={300}
170
+ >
171
+ </Axes>
172
+ ```
173
+
174
+ 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.
175
+
176
+ The last five parameters are needed to save plot to a file. See corresponding section with more details below.
177
+
178
+ [This example](https://svelte.dev/repl/d818a241c85844249b34e75196ac308c?version=4.2.18) in Svelte REPL.
179
+
180
+
181
+ ### Axis
182
+
183
+ 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.
184
+
185
+ Here is an example where component `XAxis` is shown with all available parameters:
186
+
187
+ ```svelte
188
+ <script>
189
+ import { Axes, XAxis, YAxis } from 'svelte-plots-basic/2d';
190
+ </script>
191
+
192
+ <Axes limX={[-6, 6]} limY={[-5, 6]} margins={[1.5, 1.5, 0.5, 0.5]}>
193
+ <XAxis
194
+ slot="xaxis"
195
+ ticks={[-4, 0, 4]}
196
+ tickLabels={["before", "now", "after"]}
197
+ showGrid={true}
198
+ las={2}
199
+ lineColor="#a0a0a0"
200
+ gridColor="#e0e0e0"
201
+ textColor="#ff6666"
202
+ />
203
+ </Axes>
204
+ ```
205
+
206
+ 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).
207
+
208
+ [This example](https://svelte.dev/repl/fbc54e9359a84cd39b5e1da0787b7274?version=4.2.18) in Svelte REPL.
209
+
210
+
211
+
212
+ ### Box
213
+
214
+ 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"`:
215
+
216
+ ```svelte
217
+ <script>
218
+ import { Axes, Box } from 'svelte-plots-basic/2d';
219
+ </script>
220
+
221
+ <Axes limX={[-6, 6]} limY={[-5, 6]}>
222
+ <Box slot="box" />
223
+ </Axes>
224
+ ```
225
+
226
+ [This example](https://svelte.dev/repl/567b716dbe844ba1a79c72f4beff8d3d?version=4.2.18) in Svelte REPL.
227
+
228
+
229
+ ### Points
230
+
231
+ 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).
232
+
233
+ Here is an example of using the component with all available parameters:
234
+
235
+ ```svelte
236
+ <script>
237
+ import { Axes, Points } from 'svelte-plots-basic/2d';
238
+ </script>
239
+
240
+ <Axes limX={[-4, 4]} limY={[-1, 10]}>
241
+ <Points
242
+ xValues={[-3, -2, -1, 0, 1, 2, 3]}
243
+ yValues={[9, 4, 1, 0, 1, 4, 9]}
244
+ marker={1}
245
+ faceColor="transparent"
246
+ borderColor="#ff0000"
247
+ borderWidth={2}
248
+ markerSize={2}
249
+ title="series1"
250
+ />
251
+ </Axes>
252
+ ```
253
+
254
+ 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.
255
+
256
+ The size of markers is defined in `"em"` units.
257
+
258
+ 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.
259
+
260
+ [This example](https://svelte.dev/repl/edc090cb1c184fee88aedebd8731a87e?version=4.2.18) in Svelte REPL.
261
+
262
+
263
+ ### Segments
264
+
265
+ 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.
266
+
267
+ Here is an example of using the component with all available parameters:
268
+
269
+ ```svelte
270
+ <script>
271
+ import { Axes, Segments } from 'svelte-plots-basic/2d';
272
+ </script>
273
+
274
+ <Axes limX={[-4, 4]} limY={[0, 10]}>
275
+ <Segments
276
+ xStart={[-3, -2, -1, 0, 1, 2, 3]}
277
+ yStart={[1, 2, 3, 4, 3, 2, 1]}
278
+ xEnd={[-3, -2, -1, 0, 1, 2, 3]}
279
+ yEnd={[9, 8, 7, 6, 7, 8, 9]}
280
+ lineColor="#ff0000"
281
+ lineType={3}
282
+ lineWidth={2}
283
+ />
284
+ </Axes>
285
+ ```
286
+
287
+ Parameter `lineType` can have the following values `1` - solid, `2` - dashed, `3` - dotted, `4` - dash dot lines.
288
+
289
+ [This example](https://svelte.dev/repl/41285c86fe6e4e6c9abc50fb08faa7d9?version=4.2.18) in Svelte REPL.
290
+
291
+
292
+ ### Rectangles
293
+
294
+ 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.
295
+
296
+ Here is an example of using the component with all available parameters:
297
+
298
+
299
+ ```svelte
300
+ <script>
301
+ import { Axes, Rectangles } from 'svelte-plots-basic/2d';
302
+ </script>
303
+
304
+ <Axes limX={[-4, 4]} limY={[0, 10]}>
305
+ <Rectangles
306
+ left={[-3, -2, -1, 0, 1, 2, 3]}
307
+ top={[9, 8, 7, 6, 7, 8, 9]}
308
+ width={[0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75]}
309
+ height={[8, 6, 7, 5, 6, 7, 8]}
310
+ faceColor="#ff000080"
311
+ borderColor="#ff0000"
312
+ lineWidth={2}
313
+ />
314
+ </Axes>
315
+ ```
316
+
317
+ [This example](https://svelte.dev/repl/8ef24c0380474d2a9ff15ab66f2d0572?version=4.2.18) in Svelte REPL.
318
+
319
+ ### Area
320
+
321
+ 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.
322
+
323
+ Here is an example of using the component with all available parameters:
324
+
325
+ ```svelte
326
+ <script>
327
+ import { Axes, Area } from 'svelte-plots-basic/2d';
328
+ </script>
329
+
330
+ <Axes limX={[0, 6]} limY={[-7, 21]}>
331
+ <Area
332
+ xValues={[3, 2, 1, 4, 5]}
333
+ yValues={[-5, 10, 20, 10, -2]}
334
+ fillColor="#ffc00080"
335
+ lineColor="#ff0000"
336
+ lineWidth={2}
337
+ lineType={3}
338
+ />
339
+ </Axes>
340
+ ```
341
+
342
+ [This example](https://svelte.dev/repl/478b77501386469b93a1160e46809644?version=4.2.18) in Svelte REPL.
343
+
344
+ ### Text labels
345
+
346
+ 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.
347
+
348
+ Here is an example of using the component with all available parameters:
349
+
350
+ ```svelte
351
+ <script>
352
+ import { Axes, TextLabels } from 'svelte-plots-basic/2d';
353
+ </script>
354
+
355
+ <Axes limX={[-4, 4]} limY={[-1, 10]}>
356
+ <TextLabels
357
+ xValues={[-3, -2, -1, 0, 1, 2, 3]}
358
+ yValues={[9, 4, 1, 0, 1, 4, 9]}
359
+ labels={["😀", "$$$", "oo", "x", "oo", "$$$", "😀"]}
360
+ pos={0}
361
+ faceColor="#ffcc0080"
362
+ borderColor="#aa0000"
363
+ borderWidth={1}
364
+ textSize={4}
365
+ />
366
+ </Axes>
367
+ ```
368
+
369
+ 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).
370
+
371
+ It can be specified as a single value, like in the example above, or as array of values — individual for each label.
372
+
373
+ 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.
374
+
375
+ [This example](https://svelte.dev/repl/ae58b8d4e92f4e748a679b60c4876347?version=4.2.18) in Svelte REPL.
376
+
377
+
378
+ ### Bars
379
+
380
+ 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.
381
+
382
+ Here is an example of using the component with all available parameters:
383
+
384
+ ```svelte
385
+ <script>
386
+ import { Axes, Bars } from 'svelte-plots-basic/2d';
387
+ </script>
108
388
 
109
- // x and y axis with automatic ticks and grid lines
389
+ <Axes limX={[-4, 4]} limY={[-1, 10]}>
390
+ <Bars
391
+ xValues={[-3, -2, -1, 0, 1, 2, 3]}
392
+ yValues={[9, 4, 1, 0.1, 1, 4, 9]}
393
+ faceColor="#ffcc0080"
394
+ borderColor="#ff0000"
395
+ borderWidth={2}
396
+ />
397
+ </Axes>
398
+ ```
399
+
400
+ [This example](https://svelte.dev/repl/3afb9d8acd824d64ba1c42226fb01fa9?version=4.2.18) in Svelte REPL.
401
+
402
+
403
+ ### Lines
404
+
405
+ 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).
406
+
407
+ Here is an example of using the component with all available parameters:
408
+
409
+ ```svelte
410
+ <script>
411
+ import { Axes, Lines } from 'svelte-plots-basic/2d';
412
+ </script>
413
+
414
+ <Axes limX={[-4, 4]} limY={[-1, 10]}>
415
+ <Lines
416
+ xValues={[-3, -2, -1, 0, 1, 2, 3]}
417
+ yValues={[9, 4, 1, 0, 1, 4, 9]}
418
+ lineColor="#ff0000"
419
+ lineWidth={2}
420
+ lineType={3}
421
+ />
422
+ </Axes>
423
+ ```
424
+
425
+ The line parameters are similar to the ones used in `Segments` component.
426
+
427
+ [This example](https://svelte.dev/repl/2006ca1441f845eb9ed40b3e583d0094?version=4.2.18) in Svelte REPL.
428
+
429
+
430
+ ### Multilines
431
+
432
+ 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.
433
+
434
+ Here is an example of using the component with all available parameters:
435
+
436
+ ```svelte
437
+ <script>
438
+ import { Axes, Multilines } from 'svelte-plots-basic/2d';
439
+ import { Vector, cbind } from 'mdatools/arrays';
440
+
441
+ // create x-values
442
+ const x = Vector.seq(0, 15, 0.1);
443
+
444
+ // compute y-values for four lines
445
+ const y1 = x.apply(v => Math.sin(v));
446
+ const y2 = y1.add(0.2);
447
+ const y3 = y2.add(0.2);
448
+ const y4 = y3.add(0.2);
449
+
450
+ // combine y-values into a matrix
451
+ const Y = cbind(y1, y2, y3, y4);
452
+ </script>
453
+
454
+ <Axes limX={[0, 15]} limY={[-2, 2]}>
455
+ <Multilines
456
+ xValues={x}
457
+ yValues={Y}
458
+ lineColor="#ff0000"
459
+ lineWidth={2}
460
+ lineType={1}
461
+ />
462
+ </Axes>
463
+ ```
464
+
465
+ [This example](https://svelte.dev/repl/d642a8fb78fd4a4f8381438d9bf427a2?version=4.2.18) in Svelte REPL.
466
+
467
+
468
+ ### Legend
469
+
470
+ 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.
471
+
472
+ Here is an example:
473
+
474
+ ```svelte
475
+ <script>
476
+ import { Axes, Lines, Points, Legend } from 'svelte-plots-basic/2d';
477
+ import {Vector} from 'mdatools/arrays';
478
+
479
+ // create x-values
480
+ const x = Vector.seq(-5, 5, 0.1);
481
+
482
+ // compute y-values
483
+ const y1 = x.apply(v => Math.pow(v, 2));
484
+ const y2 = x.apply(v => Math.pow(v, 3));
485
+ const y3 = x.apply(v => Math.pow(v, 4));
486
+ </script>
487
+
488
+ <Axes limX={[-5, 5]} limY={[-20, 20]}>
489
+
490
+ <!-- series 1: dotted red line -->
491
+ <Lines xValues={x} yValues={y1} lineColor="red" lineType={3} />
492
+
493
+ <!-- series 2: solid blue line and circle markers -->
494
+ <Lines xValues={x} yValues={y2} lineColor="blue" lineType={1} />
495
+ <Points xValues={x} yValues={y2} borderColor="blue" faceColor="white"/>
496
+
497
+ <!-- series 3: markers in form of diamonds with green stroke and yellow fill -->
498
+ <Points xValues={x} yValues={y3} marker={5} faceColor="yellow" borderColor="green" />
499
+
500
+ <!-- legend with one JSON for each series -->
501
+ <Legend
502
+ position="right"
503
+ items = {[
504
+ {"label": 'y=x^2', "lineType": 3, "lineColor": "red"},
505
+ {"label": "y=x^3", "lineType": 1, "lineColor": "blue", "marker": 1, "faceColor": "white", "borderColor": "blue"},
506
+ {"label": "y=x^4", "marker": 5, "faceColor": "yellow", "borderColor": "green"},
507
+ ]}
508
+ />
509
+ </Axes>
510
+ ```
511
+
512
+ The `position` parameter can be one of the follows: `"topleft"`, `"top"`, `"topright"`, `"right"`, `"bottomright"` and so on.
513
+
514
+ [This example](https://svelte.dev/repl/1c5e21e2153a4229afb630809fe545bd?version=4.2.18) in Svelte REPL.
515
+
516
+ ### Heatmap
517
+
518
+ 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.
519
+
520
+ ```svelte
521
+ <script>
522
+ import { Axes, Heatmap } from 'svelte-plots-basic/2d';
523
+ import { matrix, vector } from 'mdatools/arrays';
524
+
525
+ // create values for a matrix
526
+ const values = matrix([0.9, 0.7, 0.5, 0.3, 0.9, 0.1, 0.1, 0.5, 0.9], 3, 3);
527
+
528
+ // create vector of breaks
529
+ const breaks = vector([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]);
530
+
531
+ // colmap
532
+ const colmap = ["blue", "green", "yellow", "orange", "red"]
533
+ </script>
534
+
535
+ <Axes limX={[0, 4]} limY={[0, 4]}>
536
+ <Heatmap
537
+ {values}
538
+ {breaks}
539
+ {colmap}
540
+ />
541
+ </Axes>
542
+ ```
543
+
544
+ 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.
545
+
546
+ Both `breaks` and `colmap` are defined automatically by default.
547
+
548
+ [This example](https://svelte.dev/repl/fde87fe5dad24c419314eeb7b9fa469f?version=4.2.18) in Svelte REPL.
549
+
550
+
551
+ ### Colormap legend
552
+
553
+ 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.
554
+
555
+ Here is an example from above with added `ColormapLegend` component.
556
+
557
+ ```svelte
558
+ <script>
559
+ import { Axes, Heatmap, ColormapLegend } from 'svelte-plots-basic/2d';
560
+ import { matrix, vector } from 'mdatools/arrays';
561
+
562
+ // create values for a matrix
563
+ const values = matrix([0.9, 0.7, 0.5, 0.3, 0.9, 0.1, 0.1, 0.5, 0.9], 3, 3);
564
+
565
+ // create vector of breaks
566
+ const breaks = vector([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]);
567
+
568
+ // colmap
569
+ const colmap = ["blue", "green", "yellow", "orange", "red"]
570
+ </script>
571
+
572
+ <Axes limX={[0, 4]} limY={[0, 4]}>
573
+ <Heatmap
574
+ {values}
575
+ {breaks}
576
+ {colmap}
577
+ />
578
+
579
+ <ColormapLegend {breaks} {colmap} />
580
+ </Axes>
581
+ ```
582
+
583
+ [This example](https://svelte.dev/repl/f91380c18b3346599b45baf06b9b8819?version=4.2.18) in Svelte REPL.
584
+
585
+ In addition to two mandatory parameters, `breaks` and `colmap`, it has the following optional parameters:
586
+
587
+ * `decNum` — number of decimals to show the breaks with (by default 1).
588
+ * `labels` — optional vector with labels for interval boundaries (instead of breaks values).
589
+ * `labelColor` — color of the label values (by default `'#909090'`).
590
+ * `fontSize`— font size for the labels in em (by default `0.85`).
591
+
592
+
593
+ ### Handle mouse events
594
+
595
+ 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'`.
596
+
597
+ 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).
598
+
599
+ 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).
600
+
601
+ ```svelte
602
+ <script>
603
+ import { Axes, XAxis, YAxis, Box, Points, Lines, TextLabels } from 'svelte-plots-basic/2d';
604
+
605
+ // values for series
606
+ const x = [2000, 2001, 2002, 2003];
607
+ const y = [179.7, 185.3, 189.0, 193.5]
608
+
609
+ // currently selected bar
610
+ let selected = -1;
611
+
612
+ // handler click event on marker
613
+ function selectMarker(e) {
614
+ const id = parseInt(e.detail.elementID)
615
+ if (id >= 0) {
616
+ selected = Number.parseFloat(e.detail.elementID);
617
+ }
618
+ }
619
+
620
+ // handler click event on axes
621
+ function resetSelection(e) {
622
+ selected = -1;
623
+ }
624
+ </script>
625
+
626
+ <Axes limX={[1999, 2004]} limY={[0, 220]}
627
+ xLabel="Years" yLabel="bn US$ PPP" title="GDP of Denmark"
628
+ on:markerclick={selectMarker}
629
+ on:axesclick={resetSelection}
630
+ >
631
+
632
+ <Lines xValues={x} yValues={y} />
633
+ <Points xValues={x} yValues={y} markerSize="1.5" faceColor="#fff" borderWidth="2"/>
634
+
635
+ {#if selected > -1}
636
+ <Points xValues={[x[selected]]} yValues={[y[selected]]} markerSize="1.6" faceColor="#ffcc00"
637
+ borderColor="crimson" borderWidth="2"/>
638
+ <TextLabels xValues={[x[selected]]} yValues={[y[selected]]} labels={[y[selected]]} pos={3} />
639
+ {/if}
640
+
641
+ <XAxis slot="xaxis" ticks={x}/>
642
+ <YAxis slot="yaxis" showGrid={true} />
643
+ <Box slot="box" />
644
+
645
+ </Axes>
646
+ ```
647
+
648
+ Play with [this example](https://svelte.dev/repl/e4705e10f1604e4cabc21f5543376717?version=4.2.18) in Svelte REPL.
649
+
650
+
651
+ ### Save plot to a file
652
+
653
+ 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:
654
+
655
+ * `"none"` — turns this functionality off (default value).
656
+ * `"hover"` — buttons with download options appear when user hovers mouse over the plot.
657
+ * `"fixed"` — buttons with download options are always shown.
658
+
659
+ You can also specify parameter `fileName` which should be the desired filename for the plot without extension (e.g. `fileName="myplot"`).
660
+
661
+ 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:
662
+
663
+ * `pngWidth` — width of PNG image in cm (default is 8 cm).
664
+ * `pngHeight` — height of PNG image in cm (default is 8 cm).
665
+ * `pngRes` — resolution of the PNG image in pixels per inch (default is 300).
666
+
667
+ If the first two parameters do not match the aspect ratio of the plot they will be adjusted accordingly.
668
+
669
+ Here is a full example:
670
+
671
+ ```svelte
672
+ <script>
673
+ import { Axes, XAxis, YAxis, Box, BarSeries } from 'svelte-plots-basic/2d';
674
+ import { Vector } from 'mdatools/arrays';
675
+
676
+ // generate random values from normal distribution
677
+ const x = Vector.randn(200, 0, 1);
678
+ const y = Vector.randn(200, 0, 2);
679
+ </script>
680
+
681
+ <div class="plot-container">
682
+ <Axes
683
+ limX={[-6, 6]} limY={[-5, 6]}
684
+ xLabel="x" yLabel="y"
685
+ downloadLinks="hover"
686
+ fileName="myplot"
687
+ pngWidth={5}
688
+ pngHeight={5}
689
+ pngRes={300}
690
+ >
691
+
692
+ <ScatterSeries xValues={x} yValues={y} />
110
693
  <XAxis slot="xaxis" />
111
694
  <YAxis slot="yaxis" />
112
-
113
- // box around the axes
114
695
  <Box slot="box" />
115
-
116
696
  </Axes>
117
697
  </div>
118
698
  ```
119
699
 
120
- See demo for more details.
700
+ [This example](https://svelte.dev/repl/29a021768ca14496a1f659f186958a9a?version=4.2.18) in Svelte REPL.
121
701
 
122
702
  ## Quick start (3D plots)
123
703
 
704
+ 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.
705
+
706
+ 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.
707
+
708
+ Here is an example of simple 3D scatter plot:
709
+
710
+ ```svelte
711
+ <script>
712
+ import { Mesh, Points, Axes, XAxis, YAxis, ZAxis } from 'svelte-plots-basic/3d';
713
+ import { Matrix, Vector } from 'mdatools/arrays';
714
+
715
+ // generate coordinates for scatter plot
716
+ const xValues = Vector.rand(100, -9, 9);
717
+ const zValues = Vector.rand(100, -9, 9);
718
+ const yValues = xValues.add(zValues).divide(2).apply(v => 2 - v);
719
+
720
+ // orientation and zoom for scene
721
+ let phi = -25.264 / 180 * Math.PI
722
+ let theta = 215 / 180 * Math.PI;
723
+ let zoom = 0.5;
724
+ </script>
725
+
726
+ <Axes limX={[-10, 10]} limY={[-10, 10]} limZ={[-10, 10]} {zoom} {phi} {theta}>
727
+ <Points {xValues} {yValues} {zValues} />
728
+ <XAxis showGrid={true} title="X" slot="xaxis" />
729
+ <YAxis showGrid={true} title="Y" slot="yaxis" />
730
+ <ZAxis showGrid={true} title="Z" slot="zaxis" />
731
+ </Axes>
732
+ ```
733
+
734
+ [This example](https://svelte.dev/repl/2294eecba7d7477ab9a09c1734d32ac2?version=4.2.18) in Svelte REPL.
735
+
736
+ You can also add `Lines`, `Segments` and `Mesh` series to 3D plots.
124
737
 
125
738