dotted-map 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,23 +26,99 @@ npm i dotted-map
26
26
 
27
27
  ```js
28
28
  const fs = require('fs');
29
- const DottedMap = require('dotted-map');
29
+ const DottedMap = require('dotted-map').default;
30
+ // Or in the browser: import DottedMap from 'dotted-map';
30
31
 
31
32
  const map = new DottedMap({ height: 60, grid: 'diagonal' });
32
33
 
33
- map.addPin({ lat: 40.73061, lng: -73.935242, svgOptions: { color: '#d6ff79', radius: 0.4 } });
34
- map.addPin({ lat: 48.8534, lng: 2.3488, svgOptions: { color: '#fffcf2', radius: 0.4 } });
34
+ map.addPin({
35
+ lat: 40.73061,
36
+ lng: -73.935242,
37
+ svgOptions: { color: '#d6ff79', radius: 0.4 },
38
+ });
39
+ map.addPin({
40
+ lat: 48.8534,
41
+ lng: 2.3488,
42
+ svgOptions: { color: '#fffcf2', radius: 0.4 },
43
+ });
35
44
 
36
- const svgMap = map.getSVG({ radius: 0.22, color: '#423B38', shape: 'circle', backgroundColor: '#020300' });
45
+ const svgMap = map.getSVG({
46
+ radius: 0.22,
47
+ color: '#423B38',
48
+ shape: 'circle',
49
+ backgroundColor: '#020300',
50
+ });
37
51
 
38
52
  fs.writeFileSync('./map.svg', svgMap);
39
53
  ```
40
54
 
41
- Note: if you use a large number of points (height or width ≥ 100), it may take a bit of time to compute the map (from 1 to 30 seconds depending on your device and number of points). This is why the result grid is cached. If you don't change the parameters of `new DottedMap`, the next maps will be a lot faster to generate. You can however change the pins and the SVG options.
55
+ If you use a large number of points (height or width ≥ 100), it may take a bit of time to compute the map (from 1 to 30 seconds depending on your device and number of points). This is why the result grid is cached. If you dont change the parameters of `new DottedMap`, the next maps will be a lot faster to generate. You can however change the pins and the SVG options.
56
+
57
+ ### Precomputing the map
58
+
59
+ Because the previous operation can be expansive (especially if you want to use DottedMap in a browser or React Native app), it’s possible to precompute the grid. You will still be able to add pins on-the-fly, in real time. This also allows you to import a lighter version of the library. This is especially useful if you always use the same map parameters, but only change the pins.
60
+
61
+ ```js
62
+ // So you do this first step only once, when developing your app
63
+ const getMapJSON = require('dotted-map').getMapJSON;
64
+
65
+ // This function accepts the same arguments as DottedMap in the example above.
66
+ const mapJsonString = getMapJSON({ height: 60, grid: 'diagonal' });
67
+ console.log(mapJsonString);
68
+
69
+ // This string will contain everything about the grid. You will need to copy
70
+ // and include it in your front.
71
+ ```
72
+
73
+ ```js
74
+ // Now we are in your app, let’s imagine it’s a React app
75
+
76
+ // This import doesn’t include coordinates of countries: it’s lighter
77
+ // that 'dotted-map', so especially useful in fronts.
78
+ // However, you must give it a map you have pre-computed before.
79
+ import DottedMap from 'dotted-map/without-countries';
80
+
81
+ // Basically myMap.js contains something like:
82
+ //
83
+ // const MyMapString = 'the string mapJsonString that you got on the first step';
84
+ // export default MyMapString;
85
+ import MyMapString from './myMap';
86
+
87
+ const MyComponent = () => {
88
+ // It’s safe to re-create the map at each render, because of the
89
+ // pre-computation it’s super fast ⚡️
90
+ const map = new DottedMap({ map: JSON.parse(MyMapString) });
91
+
92
+ map.addPin({
93
+ lat: 40.73061,
94
+ lng: -73.935242,
95
+ svgOptions: { color: '#d6ff79', radius: 0.4 },
96
+ });
97
+
98
+ const svgMap = map.getSVG({
99
+ radius: 0.22,
100
+ color: '#423B38',
101
+ shape: 'circle',
102
+ backgroundColor: '#020300',
103
+ });
104
+
105
+ return (
106
+ <div>
107
+ <img src={`data:image/svg+xml;utf8,${encodeURIComponent(svgMap)}`} />
108
+ </div>
109
+ );
110
+ };
111
+
112
+ export default MyComponent;
113
+ ```
114
+
115
+ That’s how you can display a super stylish map in your React webapp, without impacting the size of your bundle nor the performance of your app (browsers are very fast at rendering SVGs).
42
116
 
43
117
  ## Specs
44
118
 
45
119
  ```js
120
+ import DottedMap from 'dotted-map';
121
+
46
122
  // Create the map
47
123
  const map = new DottedMap({
48
124
  height,
@@ -50,7 +126,7 @@ const map = new DottedMap({
50
126
  countries: ['FRA'] // look into `countries.geo.json` to see which keys to use. You can also omit this parameter and the whole world will be used
51
127
  region: { lat: { min, max }, lng: { min, max } }, // if not present, it will fit the countries (and if no country is specified, the whole world)
52
128
  grid: 'vertical' | 'diagonal', // how points should be aligned
53
- avoidOuterPins: false | true, // if it's true, prevent adding pins when they are outside of region/countries
129
+ avoidOuterPins: false | true, // if its true, prevent adding pins when they are outside of region/countries
54
130
  });
55
131
 
56
132
  // Add some points/change the color of existing points
package/index.d.ts CHANGED
@@ -38,6 +38,8 @@ namespace DottedMapLib {
38
38
  type Point = {
39
39
  x: number;
40
40
  y: number;
41
+ lat: number;
42
+ lng: number;
41
43
  data?: any;
42
44
  svgOptions?: SvgOptions;
43
45
  };
@@ -49,6 +51,7 @@ export default class DottedMap {
49
51
  constructor(settings: DottedMapLib.Settings);
50
52
 
51
53
  addPin(pin: DottedMapLib.Pin): DottedMapLib.Point;
54
+ getPin(pin: DottedMapLib.Pin): DottedMapLib.Point;
52
55
  getPoints(): DottedMapLib.Point[];
53
56
  getSVG(settings: DottedMapLib.SvgSettings): string;
54
57
  image: {