-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise.js
More file actions
63 lines (49 loc) · 2.24 KB
/
Copy pathExercise.js
File metadata and controls
63 lines (49 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Creating the main SVG.
let margin = {left: 0 , right: 10, bottom:10 , top:30};
let height = 300 - margin.top - margin.bottom;
let width = 400 - margin.left - margin.right;
let svg = d3.select("#chart").append("svg")
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
d3.json("Exercise_dataset.json").then(data=>{
//Creating the Axes.
let dimensions = Object.keys(data[0])
.filter(item => {return item != "class"});
let yScalers = {}
dimensions.forEach(dim=>{
let yScaler = d3.scaleLinear().
domain(d3.extent(data, d=>{return +d[dim]}))
.range([height,0])
yScalers[dim] = yScaler;
});
let xScaler = d3.scalePoint().domain(dimensions)
.padding(1).range([0,width])
svg.selectAll("axis").data(dimensions).enter().append('g')
.attr("transform", d=>{return "translate("+xScaler(d) +")"})
.each(function(d)
{
d3.select(this)
.call(d3.axisLeft().scale(yScalers[d]))
})
.append("text")
.style("text-anchor", "middle")
.attr("y", -8)
.text(d=>d)
.style("fill","black")
//Adding the data to the chart.
function createPath(d){
return d3.line()(dimensions.map(dim=>{return[xScaler(dim), yScalers[dim](d[dim])]}))
}
let species = data.map(d=>d['class'])
let speciesUnique = Array.from(new Set(species))
let getColor = d3.scaleOrdinal().domain(speciesUnique).range(d3.schemeCategory10)
svg.selectAll("myPath").data(data).enter().append("path")
.attr("d",createPath)
.attr("opacity",0.5)
.attr("stroke",d=>{ return getColor(d["class"])})
.attr("fill","none")
});
//Simple parallel coordinates plot using d3.js
//Json file data set containing of the information about different kind of cars labeled into different kinds of classes.