Spatial Visualization with Animated Graphs Using GIS Cloud Map Portal and D3.js

October 29, 2020
9 min read
Author: Marko Polovina, Frontend Developer at GIS Cloud

GIS Cloud API is, and always was, flexible and open for interaction with other frameworks and libraries. D3.js (Data-Driven Documents JS library) is not an exception. Using D3.js we are able to create interactive graphs and consequently better understand the data we are consuming. 
With GIS Cloud API and D3.js integration, we created a Map Portal (custom GIS app) that connects best from both of the frameworks. 
The result is information-rich water pipelines map integrated with two graph types; histogram and donut chart.

How to start building a GIS app with interactive graphs

First, you need to set up your Map Portal app. To be able to do so, you will need to upload data and create a map in GIS Cloud Map Editor, go to the App builder application and build your map portal using created maps.
If you need additional instructions on how to create your own Map Portal take a look at this example or contact us directly.
When your Map Portal is up, your map is populated with data and you are done with the initial portal customization and branding, it’s time to start coding.
Add D3.js library to your Map Portal together with (for now empty) app.js and app.css files.
Go to GIS Cloud Manager and open the Apps tab from the Dashboard. Find your Map Portal from the Apps list, click on Edit, open the Advanced tab, scroll down and Open app folder. Upload your D3.js library to the Source Browser.

In the app.json file (which is created automatically once you have opened your portal in the App Builder and saved the changes), we can load all JavaScript and CSS files. For the JS code, set your own namespace. Then, add the first function in app.js that will show a modal window with two graphs.

   "responsive": true,
   "scripts": {
       "namespace": "gcd3",
       "init": ["d3/d3.js"],
       "app": ["app.js"]
   "styles": [
   "sections": {
       "top": {
           "logo": {},
           "menu": {
               "items": [{
                   "label_i18n": "Show graph",
                   "href": "javascript:;",
                   "onclick": ";"


Understanding The Data

Don’t forget that there is no universal way to visualise the data, on the map or on the graph. First, try to understand the data. Sometimes it can be harder to decide what to do with the data then the rest of the job. 
For example, you can visualise your data on the map with different colours, and choose colours that are similar to the project theme. In our example, we decided to go with the blue shades for the types of water pipelines. We used the data for the town of Wellington, New Zealand.
Now we will combine the map and graph visualization to better understand the amount of water pipelines types in the different areas. At the end, we will integrate graphs and GIS Cloud map to achieve the interactive data visualization.
Response for features request in GIS Cloud:


Histogram Graph and Animations

We decided to start with one of the most popular graph types – Histogram. We will display that graph in the GIS Cloud modal window after the map is loaded. 
For this purpose, we are using jQuery Deferred object in the background. You can use then to wait until GIS Cloud UI is ready.

const loadGraph = () => {
   // show graph in modal window
   const modalWindow = giscloud.ui.modal({
       content: "
", title: "Pipelines", overlay: false, draggable: true, extraClass: "gc-histogram-modal" }); }; = () => { giscloud.ui.ready.then(() => { const viewer =; // if histogram is visible, don't try to create a new one if ($("#gc-histogram").length > 0) { return; } // load graph if viewer is ready (for menu click) if (viewer.mapId) { return loadGraph(); } // load graph when viewer is ready viewer.bind("ready", loadGraph); }); };;

After that, inside loadGraph function, load the feature data for the “Pipelines” layer. 

To achieve this, we will use the d3.json method. Prepare feature data for the graph.

d3.json("/rest/1/layers/3432375/features.json", features => {
   const fields = [];
   const graphData = []; => {
       if (
           fields.filter(field => {
               return field ===;
           }).length === 0
       ) {
           // add field
           // add field to graph data list
               counter: 1,
       } else {
           // add new record to a field in graph data list
           graphData[fields.indexOf(].counter =
               graphData[fields.indexOf(].counter + 1;
   histogram(fields, graphData);

Now, the graph data is ready and we can create a histogram graph. Change graph margins and set the one that will suit you best. 

For the “y” axis we are using 2000 as a top number. You can change that to a pipeline type maximum or any other number. At the end of the code, you will find a simple animation.

const histogram = (fields, graphData) => {
   // set the dimensions and margins of the graph
   const margin = {
           top: 10,
           right: 20,
           bottom: 90,
           left: 60,
       width = 550 - margin.left - margin.right,
       height = 400 - - margin.bottom;
   // append the svg object to the body of the page
   const svg = d3
       .attr("width", width + margin.left + margin.right)
       .attr("height", height + + margin.bottom)
       .attr("transform", "translate(" + margin.left + "," + + ")");
   // X axis: scale and draw:
   const x = d3
       .range([0, width])
       .attr("transform", "translate(0," + height + ")")
       .attr("transform", "translate(-10,0)rotate(-45)")
       .style("text-anchor", "end");
   // Y axis: scale and draw:
   const y = d3
       .range([height, 0])
       .domain([0, 2000]);
   // append the bar rectangles to the svg element
       .attr("x", d => x(d.field))
       .attr("y", d => y(0))
       .attr("width", x.bandwidth())
       .attr("height", d => height - y(0))
       .style("fill", "rgb(0, 151, 198)");
   // text label for the x axis
           "translate(" + width / 2 + " ," + (height + + 70) + ")",
       .style("text-anchor", "middle")
       .text("Pipeline type");
   // text label for the y axis
       .attr("transform", "rotate(-90)")
       .attr("y", 0 - margin.left)
       .attr("x", 0 - height / 2)
       .attr("dy", "1em")
       .style("text-anchor", "middle")
       .text("Number of pipelines");
   // Animation
       .attr("y", d => y(d.counter))
       .attr("height", d => height - y(d.counter))
       .delay((d, i) => i * 100);


GIS Cloud and D3.js Integration

Now that we have created a graph with an animation, it’s time to do a simple integration with GIS Cloud. Our map can catch a lot of events, you can find the extensive event list here
In our example, we want to achieve that after a user clicks on a random feature on a map, a graph column becomes selected based on the pipeline type. 
For that action, use the featureClick event. 
Compare feature type and graph columns to decide what type of pipeline is active. 
Put your new code in the histogram function.

const viewer =;
viewer.bind("featureClick", onFeatureClick);
const onFeatureClick = evt => {
   giscloud.features.byId(evt.layerId, evt.featureId).done(feature => {
       svg.selectAll("rect").each((d, i, elements) => {
           if (d.field === {
               elements[i].style.fill = "rgba(0, 151, 198, 0.4)";
           } else {
               elements[i].style.fill = "rgb(0, 151, 198)";

At this point, we want to do a similar thing in the other direction. When a user clicks on a graph column, the map will zoom into the feature bounds view, based on the pipeline type.
Let’s add a d3.js events to the svg variable. 

.on("click", onClick)
.on("mouseover", onMouseOver)
.on("mouseleave", onMouseLeave);

MouseOver and MouseLeave events will change the graph column color and cursor type.

const onMouseOver = (d, i, elements) => {
   elements[i].style.fill = "rgba(0, 151, 198, 0.4)";
   elements[i].style.cursor = "pointer";
const onMouseLeave = (d, i, elements) => {
   elements[i].style.fill = "rgb(0, 151, 198)";

The onClick event used for a graph column will zoom the map into the feature bounds.

const onClick = (d, i, elements) => {
const zoomToFeatures = value => {
   const viewer =;
   // get bounds for filtered features
       .featureBounds(3432375, {
           where: "water_type" + "='" + value + "'",
           srid: viewer.instance.epsg,
       .done(response => {
           if (response.bounds.left == 0 || response.bounds.right == 0) {
           // apply bounds
               new giscloud.Bounds(


Donut Graph

We will display the donut graph in the GIS Cloud modal window, just as we did for the histogram graph. But now, we will do something different.
This type of graph can display “cleaner” data, if we include a new pipeline type – OTHER
If there are less than 100 pipelines for a certain pipeline type, we need to put that pipeline type in the OTHER category. Then we have to print the number of pipelines on the MouseOver event and our donut graph is ready.

graphData = graphData.reduce((obj, item) => {
   if (item.counter 


d3.selectAll("path").on("mousemove", d => {"left", d3.event.pageX + 10 + "px");"top", d3.event.pageY - 25 + "px");"display", "inline-block");
   div.html("Number of pipelines" + "
" +; }); d3.selectAll("path").on("mouseout", d => {"display", "none"); });



In this project, we wanted to show you how easily you can integrate GIS Cloud API and apps with other frameworks. Using D3.js, you can build interactive graphs and connect them with GIS Cloud maps and spatial data. 

Click here to open the Map Portal we created in this project and try out the graph animations!
If you want to build your own Map Portal app and learn more about possible integrations, Sign Up and explore GIS Cloud apps and API!
Also, don’t hesitate to contact us if you have any questions, comments or maybe ideas for a new development example. 
Happy coding!

Follow GIS Cloud 

Terms of use & Privacy policy  © GIS Cloud Ltd