-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
108 lines (95 loc) · 3.31 KB
/
script.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Define as dimensões e margens do gráfico
const margin = { top: 30, right: 10, bottom: 10, left: 10 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Anexe o objeto SVG ao corpo da página
const svg = d3.select("body").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 + ")");
// Carregar os dados do arquivo JSON
d3.json("dados.json").then(function(data) {
// Extrair a lista de dimensões que queremos plotar
const dimensions = d3.keys(data[0]);
// Crie uma escala para cada dimensão
const y = {};
for (let i in dimensions) {
let name = dimensions[i];
y[name] = d3.scaleLinear()
.domain(d3.extent(data, d => +d[name]))
.range([height, 0]);
}
// Crie uma escala x
const x = d3.scalePoint()
.range([0, width])
.padding(1)
.domain(dimensions);
// Função para desenhar o caminho
function path(d) {
return d3.line()(dimensions.map(p => [x(p), y[p](d[p])]));
}
// Adicionar linhas de fundo cinzas para contexto
svg.append("g")
.attr("class", "background")
.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", path);
// Adicionar as linhas de primeiro plano
const color = d3.scaleOrdinal(d3.schemeCategory10);
svg.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", path)
.style("stroke", d => color(d.products_int));
// Adicionar um elemento de grupo para cada dimensão
const g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", d => "translate(" + x(d) + ")");
// Adicionar um eixo e título
g.append("g")
.attr("class", "axis")
.each(function(d) {
d3.select(this).call(d3.axisLeft(y[d]));
})
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(d => d);
// Permitir brushing
g.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(d3.brushY()
.extent([[-10,0], [10,height]])
.on("start brush", brush)
);
});
// Destaque as dimensões selecionadas
function brush() {
const actives = [];
svg.selectAll(".brush")
.filter(function(d) {
y[d].brushSelectionValue = d3.brushSelection(this);
return d3.brushSelection(this);
})
.each(function(d) {
actives.push({
dimension: d,
extent: d3.brushSelection(this)
});
});
// Defina a exibição das linhas de primeiro plano
svg.selectAll(".foreground path").style("display", function(d) {
return actives.every(function(active) {
const dim = active.dimension;
return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
}) ? null : "none";
});
}
});