-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarchart.html
155 lines (130 loc) · 4.14 KB
/
barchart.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<style>
.chart div {
font: 10px;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
.title {
font-size: 20px;
font-weight: 500;
}
.axis path {
display: none;
}
.axis line {
stroke: #777;
stroke-dasharray: 2,2;
shape-rendering: crispEdges;
}
</style>
<div class="chart"></div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script>
$.ajax({
url: "http://test-compass.openocean.fr/api/point-stat/CUR-theta-PD_Point4/get_visu_data/?format=json",
type: "GET",
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function (json) {
renderPD(json);
console.log(json.visualization.driver.method);
}
});
function renderPD(json) {
// Widths and heights
var width = 1000;
var wLabelsy = 20;
var wTitley = 30;
var height = 600;
var hTitle = 50;
var hLabelsx = 20;
var hTitlex = 20;
var barPadding = 1;
var wChart = width - wLabelsy - wTitley;
var hChart = height - hTitle - hLabelsx - hTitlex;
var xdata = json[json.visualization.kwargs.x_name];
var ydata = json[json.visualization.kwargs.y_name];
var metadata = json.visualization;
var title = metadata.web["data-legend-0"];
var xlabel = xdata.attributes.long_name + " [" + xdata.attributes.units + "]";
var ylabel = ydata.attributes.long_name + " [" + ydata.attributes.units + "]";
var x = d3.scale.linear().domain([0, d3.max(xdata._values[0])]).range([0, wChart - wChart / ydata._values[0].length]);
var y = d3.scale.linear().domain([0, d3.max(ydata._values[0])]).range([hChart, 0]);
var svg = d3.select(".chart")
.append("svg")
.attr("width", width)
.attr("height", height);
svg.append('g')
.append("text")
.attr("x", width / 2)
.attr("y", hTitle / 2)
.style("text-anchor", "middle")
.attr("class", "title")
.text(title);
svg.append('g')
.attr('transform', 'translate(' + (wLabelsy + wTitley) + ',0)')
.append("text")
.attr("x", width / 2)
.attr("y", height - hTitlex / 2)
.style("text-anchor", "middle")
.text(xlabel);
svg.append('g')
.attr('transform', ' rotate(-90)')
.append("text")
.attr("x", - hTitle - (hChart / 2))
.attr("y", wTitley / 2)
.style("text-anchor", "middle")
.text(ylabel);
var barsContainer = svg.append('g')
.attr('transform', 'translate(' + (wLabelsy + wTitley) + ',' + hTitle + ')');
barsContainer.append("g")
.selectAll("rect")
.data(ydata._values[0])
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (wChart / ydata._values[0].length);
})
.attr("y", function(d) {
return hChart - d * hChart / d3.max(ydata._values[0]);
})
.attr("width", wChart / ydata._values[0].length - barPadding)
.attr("height", function(d) {
return d * hChart / d3.max(ydata._values[0]);
})
.attr("fill", "#7bb0cd");
var xAxis = d3.svg.axis()
.scale(x)
.tickSize(hChart)
.orient("bottom");
barsContainer.append("g")
.attr("class", "x axis")
.style("font-size", hLabelsx / 2)
.call(xAxis);
var yAxis = d3.svg.axis()
.scale(y)
.tickSize(wChart)
.orient("left");
barsContainer.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + wChart + ", 0)")
.style("font-size", hLabelsx / 2)
.call(yAxis);
var borderPath = barsContainer.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("height", hChart)
.attr("width", wChart)
.style("stroke", "black")
.style("fill", "none")
.style("stroke-width", "1");
}
</script>