|
1 | 1 | <script lang="ts"> |
| 2 | + import { |
| 3 | + geoAlbersUsa, |
| 4 | + geoAlbers, |
| 5 | + geoEqualEarth, |
| 6 | + geoEquirectangular, |
| 7 | + geoMercator, |
| 8 | + geoNaturalEarth1, |
| 9 | + geoOrthographic, |
| 10 | + } from 'd3-geo'; |
| 11 | + import { feature } from 'topojson-client'; |
| 12 | +
|
| 13 | + import { RangeField, SelectField } from 'svelte-ux'; |
| 14 | +
|
2 | 15 | import Chart, { Svg } from '$lib/components/Chart.svelte'; |
| 16 | + import GeoCircle from '$lib/components/GeoCircle.svelte'; |
| 17 | + import GeoPath from '$lib/components/GeoPath.svelte'; |
| 18 | + import Graticule from '$lib/components/Graticule.svelte'; |
3 | 19 | import Preview from '$lib/docs/Preview.svelte'; |
| 20 | +
|
| 21 | + export let data; |
| 22 | +
|
| 23 | + let latitude = 0; |
| 24 | + let longitude = 0; |
| 25 | + let radius = 600; |
| 26 | + let precision = 6; |
| 27 | +
|
| 28 | + let projection = geoNaturalEarth1; |
| 29 | + const projections = [ |
| 30 | + { label: 'Albers', value: geoAlbers }, |
| 31 | + { label: 'Albers USA', value: geoAlbersUsa }, |
| 32 | + { label: 'Equal Earth', value: geoEqualEarth }, |
| 33 | + { label: 'Equirectangular', value: geoEquirectangular }, |
| 34 | + { label: 'Mercator', value: geoMercator }, |
| 35 | + { label: 'Natural Earth', value: geoNaturalEarth1 }, |
| 36 | + { label: 'Orthographic', value: geoOrthographic }, |
| 37 | + ]; |
| 38 | +
|
| 39 | + $: geojson = feature(data.geojson, data.geojson.objects.countries); |
| 40 | + $: features = |
| 41 | + projection === geoAlbersUsa |
| 42 | + ? geojson.features.filter((f) => f.properties.name === 'United States of America') |
| 43 | + : geojson.features; |
4 | 44 | </script> |
| 45 | + |
| 46 | +<div class="grid grid-cols-2 gap-2 my-2"> |
| 47 | + <SelectField |
| 48 | + label="Projections" |
| 49 | + options={projections} |
| 50 | + bind:value={projection} |
| 51 | + clearable={false} |
| 52 | + toggleIcon={null} |
| 53 | + stepper |
| 54 | + class="col-span-2" |
| 55 | + /> |
| 56 | + <RangeField label="Latitude" bind:value={latitude} min={-90} max={90} /> |
| 57 | + <RangeField label="Longitude" bind:value={longitude} min={-180} max={180} /> |
| 58 | + <RangeField label="Radius (km)" bind:value={radius} max={6371} /> |
| 59 | + <RangeField label="Precision" bind:value={precision} max={90} /> |
| 60 | +</div> |
| 61 | + |
| 62 | +<h1>Examples</h1> |
| 63 | + |
| 64 | +<h2>SVG</h2> |
| 65 | + |
| 66 | +<Preview data={geojson}> |
| 67 | + <div class="h-[600px] overflow-hidden"> |
| 68 | + <Chart |
| 69 | + geo={{ |
| 70 | + projection, |
| 71 | + fitGeojson: geojson, |
| 72 | + }} |
| 73 | + padding={{ left: 100, right: 100 }} |
| 74 | + > |
| 75 | + <Svg> |
| 76 | + <GeoPath geojson={{ type: 'Sphere' }} class="stroke-surface-content/30" id="globe" /> |
| 77 | + <Graticule class="stroke-surface-content/20" /> |
| 78 | + |
| 79 | + <GeoPath {geojson} id="clip" /> |
| 80 | + |
| 81 | + {#each features as feature} |
| 82 | + <GeoPath |
| 83 | + geojson={feature} |
| 84 | + class="stroke-gray-900/10 fill-gray-900/20 pointer-events-none" |
| 85 | + /> |
| 86 | + {/each} |
| 87 | + |
| 88 | + <GeoCircle |
| 89 | + center={[longitude, latitude]} |
| 90 | + radius={(radius / (6371 * Math.PI * 2)) * 360} |
| 91 | + {precision} |
| 92 | + class="fill-danger stroke-none" |
| 93 | + /> |
| 94 | + </Svg> |
| 95 | + </Chart> |
| 96 | + </div> |
| 97 | +</Preview> |
0 commit comments