-
-
Notifications
You must be signed in to change notification settings - Fork 30
docs: add canvas examples for arrow + vector mark, update api docs #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43dec09
docs: add canvas examples for arrow + vector mark, update api docs
gka 7ef6a1b
document canvas rendering in mark docs
gka 7051e9c
docs: fix image urls for presidents
gka c04d684
format
gka 50b9f38
actually use canvas
gka 06f0620
actually use canvas
gka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
docs: add canvas examples for arrow + vector mark, update api docs
- Loading branch information
commit 43dec090fb69858176dedb3a10d04c63d0e5a767
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <script module> | ||
| export const title = 'Canvas arrows'; | ||
| export const description = `Rising inequality (and population) in various U.S. cities from 1980 to 2015. Each arrow represents two observations of a city: the city’s population (x) and inequality (y) in 1980, and the same in 2015. Based on an example from <a href="https://observablehq.com/@observablehq/plot-arrow-variation-chart">Observable Plot</a>.`; | ||
| export const data = { metros: '/data/metros.csv' }; | ||
| export const sortKey = 20; | ||
| </script> | ||
|
|
||
| <script lang="ts"> | ||
| import { | ||
| Plot, | ||
| Arrow, | ||
| Text, | ||
| setPlotDefaults | ||
| } from 'svelteplot'; | ||
| import type { MetrosRow } from '../types'; | ||
| let { metros }: { metros: MetrosRow[] } = $props(); | ||
|
|
||
| setPlotDefaults({ | ||
| arrow: { | ||
| headAngle: 45 | ||
| } | ||
| }); | ||
|
|
||
| let hl = $state<MetrosRow | null>(null); | ||
| </script> | ||
|
|
||
| <Plot | ||
| grid | ||
| marginRight={20} | ||
| inset={10} | ||
| height={450} | ||
| x={{ type: 'log', label: 'Population' }} | ||
| y={{ label: 'Inequality' }} | ||
| color={{ | ||
| label: 'Change in inequality from 1980 to 2015', | ||
| legend: true, | ||
| tickFormat: { | ||
| minimumFractionDigits: 0, | ||
| maximumFractionDigits: 1 | ||
| } | ||
| }}> | ||
| <Arrow | ||
| data={metros} | ||
| x1="POP_1980" | ||
| y1="R90_10_1980" | ||
| x2="POP_2015" | ||
| y2="R90_10_2015" | ||
| bend | ||
| canvas | ||
| style="transition: opacity 0.2s ease-in" | ||
| opacity={{ | ||
| scale: null, | ||
| value: (d) => | ||
| !hl || hl.Metro === d.Metro ? 1 : 0.1 | ||
| }} | ||
| stroke={(d) => d.R90_10_2015 - d.R90_10_1980} /> | ||
| <Text | ||
| data={metros} | ||
| x="POP_2015" | ||
| y="R90_10_2015" | ||
| filter={(d) => | ||
| hl | ||
| ? hl.Metro === d.Metro | ||
| : Boolean(d.highlight)} | ||
| text="nyt_display" | ||
| fill="currentColor" | ||
| stroke="var(--svelteplot-bg)" | ||
| strokeWidth={4} | ||
| lineAnchor="bottom" | ||
| dy={-6} /> | ||
| </Plot> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <script module lang="ts"> | ||
| export const title = 'Shift map (canvas)'; | ||
| export const description = | ||
| 'Uses projected county centroids and vector arrows to show the direction and magnitude of 2020 election shifts.'; | ||
| export const data = { | ||
| us: '/data/us-counties-10m.json', | ||
| election: '/data/election.csv' | ||
| }; | ||
| export const sortKey = 11; | ||
| </script> | ||
|
|
||
| <script lang="ts"> | ||
| import { | ||
| Plot, | ||
| Geo, | ||
| Vector, | ||
| geoCentroid | ||
| } from 'svelteplot'; | ||
| import * as topojson from 'topojson-client'; | ||
|
|
||
| type ElectionDatum = { | ||
| fips: number; | ||
| margin2020?: number; | ||
| votes?: number; | ||
| }; | ||
|
|
||
| type CountyFeature = { | ||
| id?: string | number; | ||
| properties?: Record<string, unknown>; | ||
| }; | ||
|
|
||
| const { us, election } = $props(); | ||
|
|
||
| const nation = $derived( | ||
| topojson.feature(us, us.objects.nation) | ||
| ); | ||
| const stateMesh = $derived( | ||
| topojson.mesh(us, us.objects.states) | ||
| ); | ||
|
|
||
| const electionByFips = $derived( | ||
| new Map( | ||
| election.map((d: ElectionDatum) => [d.fips, d]) | ||
| ) | ||
| ); | ||
|
|
||
| const counties = $derived( | ||
| ( | ||
| topojson.feature(us, us.objects.counties) as any | ||
| ).features.map((feat: CountyFeature) => { | ||
| return { | ||
| ...feat, | ||
| properties: { | ||
| ...feat.properties, | ||
| // oxlint-disable-next-line unicorn/no-useless-fallback-in-spread -- TS requires ?? {} for Map.get() | ||
| ...(electionByFips.get( | ||
| Number(feat.id) | ||
| ) as object) | ||
| } | ||
| }; | ||
| }) | ||
| ); | ||
|
|
||
| const centroids = $derived( | ||
| geoCentroid({ data: counties }) as any | ||
| ); | ||
| </script> | ||
|
|
||
| <Plot | ||
| projection="albers-usa" | ||
| length={{ type: 'sqrt', range: [3, 40] }}> | ||
| <Geo | ||
| data={[nation]} | ||
| fill="var(--svelteplot-bg)" | ||
| stroke="currentColor" /> | ||
| <Geo | ||
| data={[stateMesh]} | ||
| stroke="currentColor" | ||
| strokeWidth={0.5} /> | ||
| <Vector | ||
| {...centroids} | ||
| length={(d: any) => | ||
| Math.abs( | ||
| (d.properties?.margin2020 ?? 0) * | ||
| (d.properties?.votes ?? 0) | ||
| )} | ||
| shape="arrow-filled" | ||
| strokeLinecap="round" | ||
| fill={(d: any) => | ||
| d.properties?.margin2020 > 0 | ||
| ? 'var(--svp-red)' | ||
| : 'var(--svp-blue)'} | ||
| rotate={(d: any) => | ||
| d.properties?.margin2020 > 0 ? 60 : -60} /> | ||
| </Plot> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.