Skip to content

Experiments

RobinFrugte97 edited this page Nov 15, 2019 · 9 revisions

Datacleaning

I started cleaning the survey data from CMD. I tried loading in a csv file with d3.csv but I ran into some issues.

d3.csv("https://gist.github.com/RobinFrugte97/a5e17cb2420bb1a671ff3912fb1d8cda")
    .then((data) => console.log(data))

First I tried loading in the data from my csv hosted on a gist on Github. I got an error related to gist, so I put the csv file locally.


SPARQL data

I am fetching my SPARQL query data with an asynchoronous function;

async function runQuery(queryUrl, query){
    let getData = await fetch(queryUrl+"?query="+ encodeURIComponent(query) +"&format=json")
    let responseData = await getData.json()
    let data = responseData.results.bindings
    console.log(data)
    return data
}

I await the fetch results before I put it in a variable and turn it into JSON.


D3 experiment

 <svg viewbox="0 0 1000 1000">
        <defs>
            <radialGradient id="myGradient">
                <stop offset="10%" stop-color="gold" />
                <stop offset="95%" stop-color="red" />
            </radialGradient>
        </defs>
        
        <!-- using my linear gradient -->
        <g>
            <circle cx="500" cy="250" r="120" fill="url('#myGradient')">
                <animate attributeName="r" values="150;175;150" dur="3s" repeatCount="indefinite" />
            </circle>
            <circle cx="450" cy="200" r="20" fill="black"></circle>
            <circle cx="550" cy="200" r="20" fill="black"></circle>
            <ellipse cx="500" cy="300" rx="40" ry="20" fill="black"></ellipse>
            <rect width="20" height="20" x="510" y="250" fill="white">
            </rect>
        </g>
    </svg>

For my first experiment with D3 I played around with different kinds of D3 supported elements. The first thing I did was create an SVG tag to render. After that I created a circle around the middle of my screen to create a smiley with. I added a circular gradient to the circle to play around with. I defined the gradient inside a defs tag, which allows me to call to the gradient inside it with an id. I then made the eyes by creating two circles and tweaking the offset. The mouth is a horizontally dominant ellipse. There is a rect tag for a tooth inside the mouth.

Above is a screenshot. You can't see it, but the SVG also has an animation on the circle. The animation is on the r radius, which makes it so it changes is size. It does this over a period of 3 seconds.


Fetching data

export async function fetchData(queryUrl, query) {
    const res = await fetch(queryUrl + "?query=" + encodeURIComponent(query) + "&format=json")
    const jsonRes = await res.json()
    let data = jsonRes.results.bindings
    return data = data.map(cleanData)
}

function cleanData(row) {
    //Object function Laurens
    let result = []
    Object.entries(row)
        .forEach(([key, propValue]) => {
            result[key] = propValue.value
        })
    result.forEach(entry => {
            entry.lat = parseFloat(entry.lat)
            entry.long = parseFloat(entry.long)
    })
    return result
}

I fetch my data in a seperate module. This module fetches the data and then cleans it before returning the data to the main function. The data cleaning function removes the nested objects and changes the string coordinates to numbers with parseFloat() to be able to plot them on a chart.


First D3 map

My first step in building my app was to implement the map of Japan. I downloaded a JSON file with the svg coordinates to plot on my svg field. I had to adjust the scale to get the map to be the right size. Position it with the translate.

function drawChart(topo) {
	console.log("Drawing map of Japan...");
	
	let svg = d3.select("svg")
		.attr("width", width)
		.attr("height", height)
	//Control size and position of Japan within the screen.
	projection
		.scale(2000)
		.translate([-3900, 1800])
	svg.selectAll("path")
		.data(topo.features).enter()
		.append("path")
		.attr("class", "feature")
		.attr("d", path)
		.style("fill", "lightgreen")
}

Clone this wiki locally