-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraficoLinhas.js
171 lines (146 loc) · 4.57 KB
/
GraficoLinhas.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, { Component } from "react";
import "./../../App.css";
import { select } from "d3-selection";
import { max, min } from "d3-array";
import { axisBottom } from "d3-axis";
import * as d3 from "d3";
import axios from "axios";
const margin = { top: 40, right: 10, bottom: 40, left: 10 };
// const margin = {top: 20, right: 80, bottom: 30, left: 100};
const width = 600 - margin.left - margin.right;
const height = 600 - margin.top - margin.bottom;
const API_DADOS = "http://naoaoutra.herokuapp.com/historico";
class LineChart extends Component {
constructor(props) {
super(props);
this.createLineChart = this.createLineChart.bind(this);
this.state = { isLoading: false, dados: [] };
}
componentDidMount() {
this.setState({ isLoading: true });
axios
.get(API_DADOS)
.then(res => res.data)
.then(data => this.setState({ dados: data }))
.catch(err => console.log(err));
this.createLineChart();
}
componentDidUpdate() {
this.createLineChart();
}
createLineChart() {
const node = this.node;
const chart = select(node)
.attr("viewBox", "0 0 " + (width + margin.left) + " " + (height + margin.top + margin.bottom))
.attr("width", "90%");
const x = d3.scaleLinear().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);
const valueline = d3
.line()
.x(function(d) {
return x(d.ano_eleicao);
})
.y(function(d) {
return y(d.total_candidate_fem);
});
const valueline2 = d3
.line()
.x(function(d) {
return x(d.ano_eleicao);
})
.y(function(d) {
return y(d.total_ghosts_fem);
});
function draw(mData) {
var data = mData;
// format the data
data.forEach(function(d) {
d.ano_eleicao = +d.ano_eleicao;
d.total_candidate_fem = +d.total_candidate_fem;
d.total_ghosts_fem = +d.total_ghosts_fem;
});
// sort years ascending
data.sort(function(a, b) {
return a["Date"] - b["Date"];
});
// Scale the range of the data
x.domain(
d3.extent(data, function(d) {
return d.ano_eleicao;
})
);
y.domain([
0,
d3.max(data, function(d) {
return Math.max(d.total_candidate_fem, d.total_ghosts_fem);
})
]);
const dataIn2009 = data.filter(function(elem) {
return elem.ano_eleicao === 2009;
});
var dataMax = max(dataIn2009, function(d) {
return Math.max(d.total_candidate_fem, d.total_ghosts_fem);
});
var dataMin = min(dataIn2009, function(d) {
return Math.min(d.total_candidate_fem, d.total_ghosts_fem);
});
const g = chart
.append("g")
.attr("transform", "translate(" + 30 + ", " + 30 + ")");
// Add the valueline path.
g.append("path")
.data([data])
.attr("class", "line-linechart")
.attr("d", valueline);
// Add the valueline path.
g.append("path")
.data([data])
.attr("class", "line-linechart-gaxis")
.attr("d", valueline2);
g.append("line")
.attr("x1", x(2009))
.attr("y1", y(0))
.attr("x2", x(2009))
.attr("y2", y(dataMax))
.style("stroke-dasharray", "3, 3")
.style("stroke-width", 2)
.style("stroke", "#fff43c")
.style("fill", "none");
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0, " + height + ")")
.call(axisBottom(x).ticks(6));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10).tickFormat(function(data) {return parseInt(data / 1000) + "k";}))
.attr("fill", "#fff")
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.attr("text-anchor", "end")
.attr("font-size", "23px")
.attr("fill", "white")
.text("Candidatas)");
g.append("g")
.attr("class", "axis axis--y gaxis")
.attr("transform", "translate( " + width + ", 0 )")
.call(d3.axisRight(y).tickSize(0).tickFormat(""))
.attr("fill", "#FF9B27")
.append("text")
.attr("transform", "rotate(270)")
.attr("y", 10)
.attr("x", -height + 105)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Candidatas fanstamas");
}
draw(this.props.data);
}
render() {
return (
<svg ref={node => (this.node = node)} width={width} height={height} />
);
}
}
export default LineChart;