From ea5b5ae04e90276865cd226f00125290f5604a5b Mon Sep 17 00:00:00 2001 From: Holtz Yan Date: Mon, 24 Jul 2023 10:19:58 +0200 Subject: [PATCH] initialize the cartogram section --- pages/cartogram.tsx | 189 ++++++++++++++++++++++++++++++++++++ util/sectionDescriptions.ts | 2 +- 2 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 pages/cartogram.tsx diff --git a/pages/cartogram.tsx b/pages/cartogram.tsx new file mode 100644 index 00000000..ad30ddd9 --- /dev/null +++ b/pages/cartogram.tsx @@ -0,0 +1,189 @@ +import React from 'react'; +import { Layout } from '../component/Layout'; +import TitleAndDescription from '../component/TitleAndDescription'; +import ChartFamilySection from '../component/ChartFamilySection'; +import { CodeBlock } from '../component/UI/CodeBlock'; +import { ChartOrSandbox } from '../component/ChartOrSandbox'; +import DatavizInspirationParallaxLink from '../component/DatavizInspirationParallaxLink'; +import { ResponsiveExplanationSection } from '../component/ResponsiveExplanationSection'; +import { GraphLinkImage } from '../component/UI/GraphLinkImage'; +import { ImageGrid } from '../component/UI/ImageGrid'; +import Link from 'next/link'; +import { BackgroundMapBasicDemo } from 'viz/BackgroundMapBasic/BackgroundMapBasicDemo'; +import { LinkAsButton } from 'component/LinkAsButton'; +import { CodeSandbox } from 'component/CodeSandbox'; +import { BackgroundMapProjectionDemo } from 'viz/BackgroundMapProjection/BackgroundMapProjectionDemo'; +import { ToDoSection } from 'component/UI/ToDoSection'; +import { BackgroundMapCanvasDemo } from 'viz/BackgroundMapCanvas/BackgroundMapCanvasDemo'; + +const graphDescription = ( + <> +

+ A cartogram{' '} + is a map in which the geometry of regions is distorted in order to + convey the information of an alternate variable. +

+

+ It is possible to build a Cartogram react component thanks to a js library + called{' '} + + topogram + + . This page provides step-by-step explanations on how to use the library + based on a geoJson file with the help of d3.js for + manipulating such a data source. +

+ +); + +export default function Home() { + return ( + + + {/* + // + // Data + // + */} +

The Data

+

+ Probably uses the same as for a{' '} + choropleth map or for a bubble map. +

+ {/* + // + // Topogram library + // + */} +

The Topogram library

+

+ As far as I can tell the best way to create a cartogram in JS is the{' '} + topogram library. +

+

+ However it looks like there is no easy way to install it using{' '} + npm. The easiest way is probably to clone the repo and + create the build, or to copy the content of the{' '} + + cartogram.js + {' '} + file. +

+ + +
+ +
+ + ); +} + +const snippetData = ` +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [102.0, 0.5] + }, + "properties": { + "prop0": "value0" + } + }, + { + "type": "Feature", + "geometry": { + "type": "LineString", + "coordinates": [ + [102.0, 0.0], + [103.0, 1.0], + [104.0, 0.0], + [105.0, 1.0] + ] + }, + "properties": { + "prop0": "value0", + "prop1": 0.0 + } + }, + ... + ] +}`.trim(); + +const snippetInstall = ` +npm install d3-geo +`.trim(); + +const snippetSkeleton = ` +import * as d3 from "d3"; // we will need d3.js + +type MapProps = { + width: number; + height: number; + data: GeoJsonData; +}; + +export const Map = ({ width, height, data }: MapProps) => { + + // read the data + // create a geoPath generator with the proper projection + // build the paths + + return ( +
+ + // render all the s + +
+ ); +}; +`.trim(); + +const snippetGeoPath = ` +const geoPathGenerator = d3.geoPath().projection(projection); +`.trim(); + +const snippetPath = ` +const allSvgPaths = data.features + .map((shape) => { + return ( + + ); +}); +`.trim(); + +const snippetInstallTypes = ` +npm install --save @types/geojson +`.trim(); + +const snippetDataType = ` +type MapProps = { + width: number; + height: number; + data: FeatureCollection; +};`.trim(); + +const snippetProjection = ` +const projection = d3 + .geoMercator() // name of the projection + .scale(width / 2 / Math.PI) // scale: bigger value = more zoom + .center([2.34, 48.86]) // coordinate of the center of the map. e.g. 2 and 48 for Paris + ...other options if needed +`.trim(); diff --git a/util/sectionDescriptions.ts b/util/sectionDescriptions.ts index dba38d18..185dcd44 100644 --- a/util/sectionDescriptions.ts +++ b/util/sectionDescriptions.ts @@ -456,7 +456,7 @@ export const chartTypesInfo: ChartTypesInfo[] = [ d3URL: 'https://www.d3-graph-gallery.com/cartogram', reactURL: 'https://react-graph-gallery.com/cartogram', label: 'Cartogram', - isAvailable: false, + isAvailable: true, }, { id: 'connection',