Repository navigation
For this project I'm using D3. 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. d3js.org.
I'm using D3 to render a chart on my screen and to plot images on the chart.
export function drawVis(chartLocation, data) {
d3.json(chartLocation).then(topo => {
d3.select("body").append("svg")
drawChart(topo)
drawObjects(topo, data)
})
} This function is where I load in my chart data with D3 in json format, using the d3.json() function. In my case the parameter is a url to where the chart SVG code is located. When the data is loaded in D3 in json format, I create an SVG element within my html body to later use as a container for my visualisation. I then call the functions to render the chart and plot the images on the chart.
function drawChart(topo) {
console.log("Drawing map of Japan...")
let chartContainer = d3.select("svg").append("g")
chartContainer.selectAll("path")
.data(topo.features).enter()
.append("path")
.attr("class", "feature")
.attr("d", setElementPosition(topo)[0])
}The chart SVG data consists of different paths for each region within the country that is displayed. For all these paths I create a group element g to keep it clean. I then start to render the paths that make up the country. With .data() I let D3 know what data to use when rendering the paths. .enter() allows me to create exactly the amount of elements that are present in the data. For each data entry I create a path element with .append(path). I then set the element attributes that I need. .attr() allows me to do that. In the attributes I add the chart positioning and a class for styling purposes.
function drawObjects(topo, data) {
let projection = setElementPosition(topo)[1]
data.then(data => {
console.log("Drawing objects on the map..")
d3.select("svg").selectAll("image")
.data(data).enter()
.append("image")
.attr("xlink:href", d => d.objectImage)
.attr("x", function (d) { return projection([d.long, d.lat])[0] })
.attr("y", function (d) { return projection([d.long, d.lat])[1] })
})
}I set the project to the same projection of the chart itself. This controls the scale, position and view of the visualisation. This needs to be the same as the chart so that the images plot on the correct place.
The data I'm using to plot the points is still a pending promise that I need to resolve. I do this with .then(). The same way I create the chart and create a path element for every entry in the data, I create an image element for every entry in the data. This is the equevalent of the img element for SVG. I set the x and y coordinates to the longitude and latitude from the data entry, inside the defined projection (control scale, position and view). I place the image source inside a xlink:href attribute with a callback function to retrieve the source from the data.
Some attributes accept a value or variable, some attributes only accept a callback function. An example is .attr("xlink:href", d => d.objectImage). Before I realised I needed a callback function to give the source to the attribute, I tried plainly giving it a variable, but that did not work because it only accepts callback functions.