-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
60 lines (57 loc) · 1.71 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>D3 Bar Chart with Details on Click</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
.chart {
display: flex;
align-items: flex-end;
height: 300px;
width: 600px;
}
.bar {
width: 60px;
margin-right: 10px;
background-color: steelblue;
cursor: pointer;
}
.details {
width: 300px;
height: 300px;
overflow-y: auto;
margin-left: 20px;
}
</style>
</head>
<body>
<div id="chart" class="chart"></div>
<div id="details" class="details"></div>
<script>
// Assuming the JSON file is named "data.json" and located in the same directory as this HTML file
fetch('data.json')
.then(response => response.json())
.then(data => {
const dataArray = Array.isArray(data) ? data : [data]; // Ensure data is in an array format
// Create bars
const chart = d3.select('#chart');
chart.selectAll('.bar')
.data(dataArray)
.enter()
.append('div')
.attr('class', 'bar')
.style('height', d => `${d.average_score * 100}px`) // Adjust scaling as needed
.text(d => d.diagnosis)
.on('click', (event, d) => {
const details = d3.select('#details');
details.html(''); // Clear previous content
d.items.forEach(p => {
details.append('p').text(p);
});
});
})
.catch(error => console.error('Error loading the JSON file:', error));
</script>
</body>
</html>