1.0.0 • Published 2 years ago

@antonio-goncalves/react-regional-data-display v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Regional Data Display

React components based on mapbox used to display quantitative data of geographical regions with support for multiples data sets and dates.

Install

npm install @antonio-goncalves/react-regional-data-display

Usage

Usage example using data provided by Eurostats.

import * as React from "react";
import {ReactRegionalDataDisplay} from "@antonio-goncalves/react-regional-data-display";
import '@antonio-goncalves/react-regional-data-display/dist/main.css'

import useSWR from 'swr'
import axios from 'axios'


const center = {
    lng: 13.9612,
    lat: 57.1004,
    zoom: 2.36
}

const fetcher =( url:string )=> axios.get(url).then(res => res.data)

const MAPBOX_TOKEN="your-token";

export function DataDisplayWithData(){
    const { data:dataSets, error:dataSetsError } = useSWR('https://preview.antonio-goncalves.com/api/dataSets?ids=gdp,populationDensity', fetcher)
    const { data:regions, error:regionsError } = useSWR('https://preview.antonio-goncalves.com/api/regions/eurostatCountries10m?ids=BE,BG,CZ,DK,DE,EE,IE,EL,ES,FR,HR,IT,CY,LV,LT,LU,HU,MT,NL,AT,PL,PT,RO,SI,SK,FI,SE,IS,LI,NO,CH,UK,ME,MK,AL,RS,TR', fetcher)

    if(dataSetsError || regionsError){
        const errors = []
        if(dataSetsError){
            errors.push(<p style={{color:"red"}}>Error when fetching data sets: {dataSetsError.message}</p>)
        }
        if(regionsError){
            errors.push(<p style={{color:"red"}}>Error when fetching regions: {regionsError.message}</p>)
        }
        return <>
            {errors.map((el,i)=>(
                el
            ))}
        </>
    }

    return (
        <ReactRegionalDataDisplay
            dataSets={dataSets}
            regions={regions}
            mapBoxToken={MAPBOX_TOKEN}
            center={center}
        />
    )


}

Components

React component provided by this library.

ReactRegionalDataDisplay

Main component providing the map visualizer, data info, data set and date selection.

Properties

NameTypeIs OptionalDefault ValueDescription
dataSetsDataSet[]noData sets available on the component.
regionsRegion[]noGeoJSON data of regions available on the map.
centerCenteryes{   zoom:1,    lng:0,    lat:0}Center position and zoom.
mapBoxTokenstringnoAccess token required to use mapbox.
mapHeightstring or numberyes500Height of the map, use a number of "px" or a sring for a css value.
mapBoxOptionsobjectyesExtra options passed to the mapbox constructor.
onDataSetChange(dataSetId:string)=>voidyesFired when a new data set is selected.
classNamestringyesCustom class for the container.
sliderClassNamestringyesCustom class for the slider element.
dropdownClassNamestringyesCustom class for the dropdown element.
placeholderClassNamestringyesCustom class for the placeholder text.
mapContainerClassNamestringyesCustom class for map container.
styleobjectyesCustom style for the container.
sliderStyleobjectyesCustom style for the slider element.
dropdownStyleobjectyesCustom style for the dropdown element.
placeholderStyleobjectyesCustom style for the placeholder text.
mapContainerStyleobjectyesCustom style for map container.

RegionsMap

Map component used on this project, can be used as a standalone component if date and data set selection is not required.

Properties

NameTypeIs OptionalDefault ValueDescription
regionsDataRegionData[] or nullnoData value for each region.
regionsRegion[]noGeoJSON data of regions available on the map.
centerCenteryes{   zoom:1,    lng:0,    lat:0}Center position and zoom.
mapBoxTokenstringnoAccess token required to use mapbox.
mapHeightstring or numberyes500Height of the map, use a number of "px" or a sring for a css value.
mapBoxOptionsobjectyesExtra options passed to the mapbox constructor.
scaleScalenoScale used by the map.
footnotesFootnote[]yesFootnotes texts available to be used by data points.
unitUnitnoUnit of the data showed on the map.
dropdownClassNamestringyesCustom class for the dropdown element.
mapContainerClassNamestringyesCustom class for the map container.
mapContainerStyleobjectyesCustom style for the map container.

Scale

Coloured scale used on the map

Properties

NameTypeIs OptionalDefault ValueDescription
valueDataScalenoNumeric intervals and colors.
unitUnitnoUnit showed on the label.
onMouseOver(value?: ScaleInterval)=>voidnoMouse over event for each scale area.

Slider

Animated slider used for date selection.

Properties

NameTypeIs OptionalDefault ValueDescription
valuenumbernoCurrent selection of the scale.
valuesSliderValue[]noAvailable values.
timeBetweenJumpsnumberyes1000Time in ms between between selection when in animated mode is enabled.
onChange(value: number)=>voidnoEvent when fired when the value changes
classNamestringyesCustom class for the slider.
styleobjectyesCustom style for the slider.

Structures

Data structures used on this project.

DataSet

Object describing a data set.

NameTypeIs OptionalDefault ValueDescription
idstringnoId of the data set.
namestringnoName of the data set.
descriptionstringnoDescription of the data set.
centerCenternoPosition and zoom of the map (overrides "center" property of the map).
sourceDataSetSourcenoSource of the data.
datesDataSetDatesnoDates of the data set.
unitUnitnoUnit of the data set.
scaleDataScalenoScale info for the data set.
dataRegionData[] or nullnoData values for each region.
footnotesFootnote[]yesFootnotes texts available to be used by data points.

Center

Object describing the center position and zoom on the map.

NameTypeIs OptionalDefault ValueDescription
latnumbernoLatitude value.
lngnumbernoLongitude value.
zoomnumbernoZoom level.

DataSetSource

Object describing the source of the data

NameTypeIs OptionalDefault ValueDescription
namestringnoName of the source.
urlstringnoUrl of the source.

DataSetDates

Object describing the dates available on the data set

NameTypeIs OptionalDefault ValueDescription
formatstringnoFormat on how to display the dates.
timestampsnumber[]noValues of the timestamps in ms.

Unit

Object describing the unit

NameTypeIs OptionalDefault ValueDescription
namestringnoName of the unit.
symbolstringnoSymbol of the unit.

DataScale

Object describing the scale used on the map

NameTypeIs OptionalDefault ValueDescription
startValuenumbernoFirst value of the scale.
stepsnumber[]noValues for each step.
colorsstring[]noColor of each step.

RegionData

Object describing the data for each region

NameTypeIs OptionalDefault ValueDescription
dateTimestampnumbernoDate of the data.
idstringnoId of the region.
valuenumbernoNumeric value of the data.
footnoteIdsstring[]yesIds of the footnotes.

Footnote

Object describing footnotes available

NameTypeIs OptionalDefault ValueDescription
idstringnoId of the footnote.
valuestringnoText value of the footnote.

Region

Object containing the contours of a region

NameTypeIs OptionalDefault ValueDescription
idstringnoId of the region.
namestringnoname of the region.
geoJSONGeoJSONnoContours of the region, GeoJSON (type="Feature").

SliderValue

NameTypeIs OptionalDefault ValueDescription
labelstringnoLabel of the value.
valuenumbernoNumeric value.

ScaleInterval

NameTypeIs OptionalDefault ValueDescription
minValuenumbernoMinimum value.
maxValuenumbernoMaximum value.
colorstringnoColor of the area.