-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (145 loc) · 4.34 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import Waffle from "./apps/Wafflechart";
import "./styles/global.css";
import "./styles/custom_styles.css";
import "./styles/map_styles.css";
import "leaflet/dist/leaflet.css";
import config from "./wafflechart_config";
import waffle_data from "../../data/waffle_chart.csv";
// import annotations from "./annotations";
import * as d3 from "d3";
import de from "./components/german_locale";
d3.timeFormatDefaultLocale(de);
d3.formatDefaultLocale(de);
const app = new Waffle({
target: document.querySelector("#___waffle_chart"),
props: {
config: config,
data: waffle_data,
},
});
export default app;
import orig_data from "../../data/bubble_chart.csv"
var data = wrangle_data(orig_data)
console.log(data)
var container = d3.select('#___bubble_chart')
const intro = container.append("div").attr("class", "___chart_intro"); // Überschrift und Subtitel
const svg_container = container
.append("div")
.attr("class", "___chart_svg_container");
var legend_div = container
.append("div")
.style("text-align", "center")
.attr("class", "___chart_legende");
const quelle = container
.append("div")
.attr("class", "___chart_quelle article-figcaption"); // Quelle angeben
intro.append("h3").html('Artikelerfolg nach Themenfeld').attr("class", "___chart_intro_titel");
intro.append("p").attr("class", "___chart_intro_text").html('');
var tooltipText = function (d) {
return d;
};
var margin = {
top: 10,
right: 20,
bottom: 30,
left: 50
},
width = 500 - margin.left - margin.right,
height = 420 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#___bubble_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 + ")");
//Read the data
const tooltip_container = svg_container
.append("div")
.attr("class", "___tooltip")
.style("opacity", 0);
// Add X axis
var x = d3.scaleLinear()
.domain([2000, 4200])
.range([0, width]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add Y axis
var y = d3.scaleLinear()
.domain([50, 75])
.range([height, 0]);
svg.append("g")
.call(d3.axisLeft(y));
// Add a scale for bubble size
var z = d3.scaleLinear()
.domain([0, 5500])
.range([4, 40]);
// Add a scale for bubble color
var myColor = d3.scaleOrdinal()
.domain(["sicherheit", "gesundheit", "sport", "politik", "kultur", 'leben', 'verkehr', 'wissen', 'wirtschaft'])
.range(d3.schemeSet3);
// // -1- Create a tooltip div that is hidden by default:
var tooltip = d3.select("#___bubble_chart")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "black")
.style("border-radius", "5px")
.style("padding", "10px")
.style("color", "white")
// -2- Create 3 functions to show / update (when mouse move but stay on same circle) / hide the tooltip
function show_tooltip(event, d) {
d3.select(this)
.style("stroke-width", 2)
.style("stroke", "white")
.raise();
tooltip_container.style("opacity", 1).html(tooltipText('Thema: ' + d['themencluster'] + '</br>Visitors: '+ Math.round(d['visitors'])+ '</br> zuende gelesen: '+ Math.round(d['zuende'])+'%'+ '</br> Zeichenanzahl: '+ Math.round(d['charactercount'])));
console.log('hier passier was')
var pos = d3.pointer(event)[1];
if (pos < height / 2) {
svg.attr(
"style",
"align-items:flex-end;-webkit-align-items:flex-end;-ms-flex-align:end"
);
} else {
svg.attr(
"style",
"align-items:flex-start;-webkit-align-items:flex-start;-ms-flex-align:start"
);
}
}
function hide_tooltip() {
tooltip_container.style("opacity", 0);
// polygone.style("stroke-width", 1).style("stroke", "#878787");
}
// Add dots
svg.append('g')
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("class", "bubbles")
.attr("cx", function (d) {
return x(d.charactercount);
})
.attr("cy", function (d) {
return y(d.zuende);
})
.attr("r", function (d) {
return z(d.visitors);
})
.style("fill", function (d) {
return myColor(d.themencluster);
})
// -3- Trigger the functions
.on("mouseover", show_tooltip)
.on("mouseleave", hide_tooltip)
function wrangle_data(data) {
for (const item of data) {
item["charactercount"] = parseFloat(item["charactercount"]);
item["visitors"] = parseFloat(item["visitors"]);
}
return data;
}