Advertisement
  1. Code
  2. Python
Code

Data Visualization App Using GAE Python, D3.js and Google BigQuery: Part 2

by
Difficulty:IntermediateLength:LongLanguages:
This post is part of a series called Data Visualization App Using GAE Python, D3.js and Google BigQuery.
Data Visualization App Using GAE Python, D3.js and Google BigQuery
Data Visualization App Using GAE Python, D3.js and Google BigQuery: Part 3

In the first part of this series, we created a Python application and deployed it to Google App Engine (GAE). From the application we connected it to a Google BigQuery dataset and fetched the data into our application. In this tutorial, we'll see how to visualize the data using the JavaScript library D3.js.

1. Getting Started With D3.js

D3.js is a JavaScript library to create data-driven documents. It uses the capabilities of HTML5, SVG and CSS3 to create interactive visualizations based on arbitrary data. It also makes use of different transformations to make the display visually more appealing.

From the D3.js official site:

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.

To get started with D3.js, download and include D3.js, or you can directly link to the latest release.

Now we are all set for creating our graph using D3.js.

2. Creating the X and Y Axes

First, clone the previous tutorial source code from GitHub.

We'll create a new page to display our chart. Let's name it displayChart.html. So navigate to PythonD3jsMashup_Part1/Templates/ and create a new page called displayChart.html. Add the following HTML code to it:

Navigate to the PythonD3jsMashup_Part1 directory and open up app.py. Now we need to include a route for the newly-added page displayChart.html. Below is a part of the app.py where existing routes are declared for the app.

Similarly, we'll include one more route called /displayChart and set it to the DisplayChart class that we declare next. Here is the newly added route:

Now, let's create a class handler for /displayChart to render the template page. We'll use the template.render function to display the template page. Here is the newly-added class handler to display displayChart.html.

Save everything and restart the server.

Point your browser to http://localhost:8080/displayChart and you should see the newly-added page template.

Newly added page template

Scalable Vector Graphics (SVG) is an XML-based image format for drawing 2D graphics with support for interactivity and animation. We'll be using an SVG element, onto which we'll draw our graph. So, let's first create an SVG element.

Now, let's create a JavaScript function called InitChart to initialize the chart. Include the script at the end of the page.

Before getting started, let's assume a certain set of data as shown. Later, we'll replace the sample data with the data from the Google BigQuery data set.

First, we'll define a few constants related to the graph, such as its width, height, and margin from left, right, top and bottom, which we'll use while creating the graph. So, let's select the SVG element using d3.select and define our constants.

In order to plot our data, we'll need to define scales across the X axis and Y axis such that when data comes dynamically, the scales adjust accordingly. D3.js provides a number of APIs to make things easier. One such API is d3.scale.linear, which we'll use to create our scale based on the maximum and minimum data values.

That being said, we have two new terms called Range and Domain. Range represents the area we have to display the visualization and Domain represents the maximum and minimum values of data that we'll visualize on the svg area.

Now, we'll create our scales using d3.scale.linear and define the range and domain for the created scales.  

As you can see, we have defined the range from 20 to 980 so that it doesn't collide with the graph edges. In the domain, we have set the minimum and maximum values as per the sample data.

Similarly, we'll define the scale for the Y axis too.

Since the scales are ready, we'll move towards creating the axes. D3.js provides an API called d3.svg.axis to do this. So, we'll use the API to create the axes and set the above created scales to them.

With our scales and axes created, we are all set to draw them on the svg. For that we need a container, so let's create one. 

Now, we'll set the xAxis that we created above to the svg container as shown below:

Similarly, for the yAxis:

Here is the InitChart function that we just created. 

Save all the changes and call the function on page load. Restart the server and point your browser to http://localhost:8080/displayChart, and you should be able to see the below screen.

Chart with some data added

If you take a look at the above screen, it looks like a mess. Our two axes are there, but they seem to be overlapping each other. Let's get that corrected first.

In order to separate both the overlapping axes, what we'll do is move the X axis downwards. We'll use a property called transform to move the X axis down. There are different types of transforms that are available. We'll use the translate specific transformation to move it downwards. Using translate transform we can move the axes based on coordinates. While using translate, we need to specify the X and Y coordinates to which the axis needs to be moved. Since we want the X axis to move downwards, we specify only the Y coordinate and leave the X coordinate at 0. Here is how it looks like after adding the transform attribute. 

Save the changes and restart the server. Point your browser to http://localhost:8080/displayChart and you should see the below screen.

Chart with X axis created

Next, we need to change the orientation of the Y axis. We can specify the axis orientation using orient. So modify the yAxis as shown below to change its orientation.

Now if you refresh the page you won't be able to see the Y Axis. It's because the orientation of the Y axis has been changed and it has gone outside the view to the extreme left. To change that we'll apply transform to the Y axis as shown.

As you can see from the above code, we simply moved the Y axis along the X coordinate (20 from the left) to bring it into the picture. Save the changes and refresh the page and you should see the below screen.

Chart with both axes added

So here we have our X axis and Y axis, although they look a bit ugly. Let's apply some styles and make them look good. Include the following CSS styles:

Add the following CSS styles to the axes using the CSS attribute property.

Refresh the page and you should see the below screen:

Chart with CSS styles added

3. Scaling the X and Y Axes Dynamically

Until now, we hard-coded the minimum and maximum values for the domain, which makes it static and useless when data gets dynamic. So, we need to change it and make it dynamic so that the graph scales dynamically.

D3.js provides functions called d3.min and d3.max to get the minimum and maximum values from an array. We can use these functions to get the maximum and minimum values for our domain, based on the sample dataset. Suppose we have an array called data. Using d3.min we can get the minimum value.

Similarly, to get the maximum value:

Now we modify the xScale and yScale to make the domain values dynamic.

Wrapping It Up

In this tutorial, we saw how to get started with the JavaScript visualization library D3.js to make our visualization app. We created the axes for our graph and also made it dynamic so that it scales with the data accordingly. 

In the next part of this series, we'll use the actual data fetched from Google BigQuery to scale the graph axes and also get started with plotting the data on the graph.

Code from this tutorial is available on GitHub.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.