-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_plot.html
More file actions
129 lines (112 loc) · 3.75 KB
/
interactive_plot.html
File metadata and controls
129 lines (112 loc) · 3.75 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plotly Slider Example</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="plot"></div>
<div id="sliders"></div>
<script>
// Define the data
var data = [
{
x: [200, 400, 600, 800],
y: [10, 15, 13, 17],
text: ['Project A', 'Project B', 'Project C', 'Project D'],
mode: 'markers+text',
marker: {
size: 12,
color: 'blue'
},
type: 'scatter',
name: '',
textposition: 'right',
hovertemplate:
"<b>%{text}</b><br><br>" +
"Benefits: %{x}<br>" +
"Costs: %{y}<extra></extra>"
}
];
// Define the layout
var layout = {
title: 'Cost vs. Benefits by Project',
xaxis: {
title: 'Benefits',
range: [0, 1000],
fixedrange: true,
},
yaxis: {
title: 'Costs',
range: [0, 10],
fixedrange: true,
},
hovermode: 'closest',
};
// Define the config
var config = {
responsive: true,
displayModeBar: false,
};
// Define the plot div
var plotDiv = document.getElementById('plot');
// Create the plot
Plotly.newPlot(plotDiv, data, layout, config);
// Define the sliders
var slider1 = createSlider('slider1', 'Project A');
var slider2 = createSlider('slider2', 'Project B');
var slider3 = createSlider('slider3', 'Project C');
var slider4 = createSlider('slider4', 'Project D');
// Add the sliders to the sliders div
var slidersDiv = document.getElementById('sliders');
slidersDiv.appendChild(slider1);
slidersDiv.appendChild(slider2);
slidersDiv.appendChild(slider3);
slidersDiv.appendChild(slider4);
// Function to create a slider
function createSlider(sliderId, projectName) {
var sliderContainer = document.createElement('div');
sliderContainer.id = sliderId;
sliderContainer.classList.add('slider-container');
var sliderLabel = document.createElement('div');
sliderLabel.innerText = projectName;
sliderLabel.classList.add('slider-label');
var sliderInput = document.createElement('input');
sliderInput.type = 'range';
sliderInput.min = 0;
sliderInput.max = 1000;
sliderInput.value = 500;
sliderInput.classList.add('slider-input');
sliderInput.addEventListener('input', function () {
var xValue = parseInt(this.value);
var pointIndex = data[0].text.indexOf(projectName);
data[0].x[pointIndex] = xValue;
Plotly.redraw(plotDiv);
});
sliderContainer.appendChild(sliderLabel);
sliderContainer.appendChild(sliderInput);
return sliderContainer;
}
</script>
<style>
.slider-container {
margin: 10
<style>
.slider-container {
margin: 10px 0;
}
.slider-label {
font-size: 14px;
font-weight: bold;
margin-bottom: 5px;
}
.slider-input {
width: 100%;
}
.project-dot {
stroke-width: 1px;
}
</style>
</body></html>