All you need is a dataset. Installing with Bower is easy:
bower install simple-map-d3
Include dependencies (Topojson if you are using that format).
<script src="bower_components/d3/d3.min.js" charset="utf-8"></script>
<script src="bower_components/topojson/topojson.js" charset="utf-8"></script>
Include library:
<link rel="stylesheet" href="bower_components/simple-map-d3/dist/simple-map-d3.css">
<script src="bower_components/simple-map-d3/dist/simple-map-d3.js" charset="utf-8"></script>
Make a container:
<div id="simple-map-d3-example"></div>
Make a map:
var map = new SimpleMapD3({
container: '#simple-map-d3-example',
datasource: 'mn-county-2010.geo.json'
});
Minnesota census data per county, colored by population.
var mnPopMap = SimpleMapD3({
container: '.simple-map-d3-mn-pop-map',
datasource: 'example-data/mn-county-2010.geo.json',
colorOn: true,
colorProperty: 'POPULATION',
legendFormatter: d3.format(',f0')
});
A simple map of European countries and their population density using a different projection, a slight rotation, custom color set, ability to pan, and a custom tooltip output.
var europePopMap = SimpleMapD3({
container: '.simple-map-d3-europe-pop-map',
datasource: 'example-data/europe-population-density-geocommons.geo.json',
colorSet: 'Spectral',
colorOn: true,
colorProperty: 'population',
colorReverse: true,
projection: 'azimuthalEqualArea',
rotation: [0, 0, -20],
canvasDragOn: true,
tooltipContent: function(d) {
var p = d.properties;
return '<h5>' + p.country + '</h5>' +
p.population + ' population per square kilometer';
}
});
A simple map of US counties using a Topojson data source with some custom styling.
var usMap = SimpleMapD3({
container: '.simple-map-d3-us-map',
datasource: 'example-data/us-counties.topo.json',
tooltipOn: false,
styles: {
stroke: '#EDEDED',
fill: '#232323'
}
});
A simple map of 2005 world population with graticule and globe turned on and started "manually".
var worldMap = SimpleMapD3({
container: '.simple-map-d3-world-map',
datasource: 'example-data/world-population.geo.json',
projection: 'equirectangular',
colorOn: true,
colorProperty: 'POP2005',
colorSet: 'Paired',
colorScale: 'quantize',
tooltipOn: true,
graticuleOn: true,
globeOn: true,
legendOn: false,
startManually: true
}).start();
Simple Map supports any latitude and longitude (EPSG:4326) based GeoJSON or TopoJSON file.
The following is a list of supported options when using Simple Map.
container
: The DOM selector for the DOM element that will hold the map. This is required. datasource
: String location of json file to use. This is required, unless you use the data
property. data
: Instead of using a JSON file source with the datasource
property, you can directly use a dataset. colorOn
: Whether to process fill colors. Use with colorSet
and colorProperty
. colorSet
: An array of valid color strings or a string referring to a ColorBrewer.org color set; see below for keys to use. colorProperty
: The property that will be used to determine the fill color. This is a property found in the properties
object of each feature in the GeoJSON file. This will depend on your data. colorReverse
: Reverses the color set; mostly useful if using an existing ColorBrewer set. colorScale
: The scale function to use, either a custom function, or a valid D3 scale such as quantile or linear. This will affect how colors are matched to values. tooltipOn
: Whether to add a tooltip. tooltipContent
: Function that takes the data item as the argument, where d.properties
are the GeoJSON properties of the feature, and returns HTML to fill in the tooltip. canvasDragOn
: Enables dragging and panning for the map from the whole canvas. this is a useful interaction for users. This will interfere with the other dragging options. mapDragOn
: Enables dragging and panning of just the map. Useful for placement of the map through an interface. legendDragOn
: Enables dragging and panning of just the legend. Useful for placement of the legend through an interface. styles
: Styles for each map feature. The fill will be overrided if color is on.stylesHover
: Styles to apply to hover state of map feature.stylesBackground
: Styles for a background container.stylesLegendContainer
: Styles for legend container.stylesLegendTitleText
: Styles for legend title text.stylesLegendText
: Styles for each legend item text.stylesLegendSwatch
: Styles for each legend item color swatch.stylesGraticule
: Styles for the graticule.stylesGlobe
: Styles for the globe.projection
: The name of the projection function provided by D3. Projections describe how to display geographical information and will drastically change how the map looks. rotation
: The rotation array to use with the projection. See the D3 documentation for how it works. For example, if you just want to rotate around the center of the map 30 degrees, pass [0, 0, 30]
. legendOn
: Turns legend on, though not all scales support a legend at the moment. legendTitle
: The title for the legend box. legendFormatter
: Function for formatting numbers in legend. legendWidth
: Width of legend box. legendScale
: Scale of legend box. graticuleOn
: Adds a simple graticule grid. globeOn
: Adds a container around the globe that could be used for an outline. startManually
: Whether to automatically start the data and rendering process. If false, the configuration will be loaded, but data will not be loaded or the map will not be rendered. This can be helpful in order register events properly. mapOffset
: An x, y array that offsets the map from the top left. legendOffset
: An x, y array that offsets the legend from the top left. Key | Colors |
---|
There are a couple custom events that are fired through the map rendering process that may be helpful. These are created with the D3.dispatch system, and internally under .events
object.
dataLoaded
: This event is trigger once the data is loaded, if you are not using a data source, this will be very instant. Use it like so:
var europePopMap = SimpleMapD3({ ... });
europePopMap.events.on('dataLoaded.YOUR_NAMESPACE', function(smd) {
// Do your thing
});
rendered
: This event is triggered after everything has been rendered and is a good place to do any modifications to the map:
var europePopMap = SimpleMapD3({ ... });
europePopMap.events.on('rendered.YOUR_NAMESPACE', function(smd) {
// Do your thing
});