First Protovis, now D3. Both are works from Mike Bostock. Both are data visualization JavaScript libraries.
This tutorial we’re going to create a basic dot graph with scalable y-axis. It is adapted from one of the examples in D3, dot.html.
Step 1: Default html
First of all, we create a default html that contains a div for input button and another for the graph. We also link the d3 JavaScript library. The rest of tutorial will be concentrate of JavaScript code that will be written after the comment, "Scale graph code here."
Step 2: Random data
We need to generate some random data using Math.random(). We create randomData function for returning a list of ten x and y values. For example, [{“x”: 0, “y”: 0.3}, {“x”: 0.1, “y”: 0.4}, ...]
Step 3: Maximum value of y-axis
For this tutorial, only y axis values are random. We need to get maximum value of the y axis using d3.max, in order to scale the y axis based on the maximum value of the data.
Step 4: Ceil value
Sometime, the maximum value doesn’t suitable for domain range in scale axis. For example, 0.33333. Mike suggested a method to increase the size of domain to next highest rounded value.
Step 5: SVG container
Now, we determine the size of the graph, which are w and h for width and height. We need a svg container for the graph. We will append it inside #mainGraph div with width and height attributes. This is optional, which offset the graph with modify coordinate using translate in transform in a group. The element is used to group SVG shapes together. For example, ` will translate coordinate ["x": 50, "y": 50]** as zero in the that group.
Step 6: Rulers
Now, we add both x and y axises line grids and labels. We also notice that d3 supports css style. We make #ccc colour stroke style for both x and y line grids.
Step 7: Dots
This is the last step for the dots graph. All the dots are paths. What kind of symbols of the path is determined by d attribute. We also add title attribute in the path, so that when mouse is hovering the dots, tool tip is shown. Color of the dots fill is changed too. After you completed this steps, we will see a basic dot graph.
Step 8: Update button
This step is explaining what will happen after update button is clicked. Before that, we need to add onclick attribute in button type input with the update function name. For example, . In the update function, we will get a new data list from randomData function. Then get the maximum ceil value, which is similar in initData function. Since, we are only want to rescale the y axis, we will select g.y. Make sure to select their parent, #mainGraph svg g, first before selecting them, in order to append the extra line grid in the right parent. For updating the line grids and labels, we are going to use update, enter and exit selections. Since we are append new line grid and label, we need to set all the attributes that are needed. On the other hand, we only update attributes that need to be changes in update and exit selections. Lastly, don't forget about updating the dots data too.
Step 9: Summary
This tutorial is written based on my personal understanding on D3. I am still learning. Any feedback are welcome.
Comments
Comments powered by Disqus