We have the following data of cars
We can use functional programming to make it look nicer.
The code below use a functional paradigm.
By making a reduce
function we can easily format json formatted data.
This is done by mapping over the data with a custom function that formats each object (i.e. each car description). Look at components/car.js file for the formatting function.
We can also filter by a specific price. In the following example we filter by car prices less than $2000.
We can write scalable vector graphics (SVG) using the svg tag. Learn the follwoing:
We can see that the above can be replicated with d3.js.
Will demostrate how to make a face and will see it below. We can also do animation with d3. For a demo click the "Start Animation" button.
Explination to be done.
Got the data from here: UN Data
Here we will draw a scatterplot
Here is another scatter plot where both x and y are numeric.
Another scatter plot here with temperature.
Let us do line plots now
Here are some options you can play with:
Lets draw a function to see how it turns out.
Multiple plots for a line
Let us do area plot now
Change the size of the strok when show.
The baseline is an important property of an area plot. In the above lineplot example the temperature baseline was 14 but that is not a correct implementation of an area plot.
Below let us plot a proper area graph.
This section will demonstrate how the enter, update, and remove work with a bowl of fruit example.
If the bowl is empty then add 5 apples. If you eat one remove it from the bowl.
You can add only one element (like the bowl) by using the
selection.selectAll('rect').data([null]).enter().append('rect')
.
Who knew you could interaction with data. Here we will see that.
Lets make a map.
Here are some references used here:
Loaded topo json format and d3 requires geo json format. Thus use topojson library for conversion. Got the topojson library from here: TopoJSON Library.
Use the topojson.feature(data)
function for conversion.
If the path type is sphere then you will get the thick borders around the map.
export const createMapBeg = () => {
const svg = d3.select("#id");
// type of projection
const projection = d3.geoMercator();
// path generator ie the d=""
const pathGenerator = d3.geoPath().projection(projection);
d3.json("https://unpkg.com/world-atlas@1.1.4/world/110m.json")
.then(data => {
const countries = topojson
.feature(data, data.objects.countries);
const paths = svg.selectAll("path").data(countries.features);
paths.enter() .append("path") .attr("d", d => pathGenerator(d));
});
};
You will find the types of projections here: Projections, another link.
Here is a list of some projections
d3.geoOrthographic();
This is used below.
d3.geoStereographic();
d3.geoEqualEarth();
d3.geoGnomonic();
Need to figure out how to resize maps properly, and also how to rotate them.
Who knew we could interact with maps. Try to hover over them countries in the following map.
We will create a tree. Uses the d3.hierarchy library. It is there by default.
You can also have vertical trees with the following code:
You could use treeLayout = d3.cluster()
if the
leaft nodes are unlevel to have them appear on the same level.
In this module we will make a legend component.
Lets also make a size scale next to the legend. Size scale is proportional to area and we are going to use circle to represent size. Therefore, r = sqrt(A/pi)
Lets add a legend to the maps.