-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
433 lines (381 loc) · 16 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="zpkJm8KWRDtbMaW0OX3DHdY1eYqJabj5V7ER9K0xKbA" name="google-site-verification"/>
<meta content="Online visualizer of Plotly.js graphs in Plotly JSON visualization schema format" name="description">
<title>Plotly JSON Visualizer</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.plot.ly/plotly-2.32.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/moveable.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/moveable.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" rel="stylesheet">
<link href="icons/icons8-area-chart-100.png" rel="icon" sizes="96x96" type="image/png">
<style>
body {
height: 100vh;
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
position: relative;
}
.box {
width: 700px;
height: 600px;
background-color: #fff;
border: 1px solid lightblue;
border-radius: 5px;
box-shadow: 0 0 15px darkgrey;
position: absolute;
top: 50px;
left: 50px;
/*resize: both;*/
overflow: hidden;
z-index: 0;
}
.box-header {
color: #fff;
background-color: white;
padding: 5px 10px;
display: flex;
justify-content: space-between;
align-items: center;
height: 40px;
flex-shrink: 0;
position: relative;
cursor: move;
}
.header-buttons {
display: flex;
gap: 5px;
flex-shrink: 0;
}
.box-body {
height: calc(100% - 40px);
padding: 10px;
overflow: auto;
background-color: lightblue;
}
.btn {
border: 1px solid #ccc;
padding: 3px 6px; /* Adjusted padding for smaller buttons */
background-color: white;
cursor: pointer;
flex-shrink: 0;
font-size: 15px; /* Adjusted font size for smaller buttons */
}
.btn:hover {
background-color: #f0f0f0;
}
.btn:active {
background-color: #e0e0e0;
}
#add-box-btn {
position: absolute;
top: 10px;
left: 10px;
cursor: pointer;
color: lightsteelblue;
}
#add-box-btn:hover {
color: lightblue;
}
#overlay-text {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
font-size: 24px;
color: rgba(0, 0, 0, 0.5);
pointer-events: none;
z-index: 1000;
user-select: none;
}
.hidden {
display: none;
}
.moveable-control {
opacity: 0;
}
.moveable-line {
opacity: 0;
}
.input-label-required {
font-weight: bold;
}
.view-chart {
box-shadow: 0 0 15px darkgrey;
width: 100%;
height: 100%;
}
.github-link {
position: absolute;
left: 10px;
bottom: 10px;
color: black;
font-size: 30px;
z-index: 0;
}
.github-link:hover {
opacity: 0.5;
cursor: pointer;
}
</style>
</head>
<body>
<i id="add-box-btn" class="fas fa-plus-circle fa-2x"></i>
<a href="https://github.com/nikita715/plotly-json-visualizer" title="GitHub repo"><i
class="fa-brands fa-github github-link"></i></a>
<div id="overlay-text">Plotly JSON Visualizer</div>
<script src="https://unpkg.com/moveable@latest/dist/moveable.min.js"></script>
<script>
let highestZIndex = 1000;
let boxIndex = 0;
const jsonExample = "{\r\n \"data\": [\r\n {\r\n \"x\": [\r\n \"giraffes\",\r\n \"orangutans\",\r\n \"monkeys\"\r\n ],\r\n \"y\": [\r\n 20,\r\n 14,\r\n 23\r\n ],\r\n \"type\": \"bar\"\r\n }\r\n ]\r\n}";
document.getElementById('add-box-btn').addEventListener('click', addNewBox);
function addNewBox() {
const box = document.createElement('div');
box.className = 'box';
box.style.zIndex = ++highestZIndex;
let currentBoxIndex = ++boxIndex;
box.innerHTML = `
<div class="box-header" data-drag-handle="true">
<div class="header-buttons">
<button class="btn btn-back" style="display:none;">Back</button>
<button class="btn btn-generate">Generate</button>
</div>
<span class="btn btn-close">X</span>
</div>
<div class="box-body">
<div class="view-input-type">
<label for="form-input-type-${currentBoxIndex}" class="input-label-required">Input Type</label>
<select id="form-input-type-${currentBoxIndex}" class="form-control input-type">
<option value="json">JSON Text</option>
<option value="http">HTTP Request</option>
</select><br>
</div>
<div class="view-json">
<label for="form-json-input-${currentBoxIndex}" class="input-label-required">JSON Data</label><br>
<textarea id="form-json-input-${currentBoxIndex}" class="json-input form-control" rows="10"></textarea><br>
<button class="btn btn-beautify">Beautify JSON</button><br><br>
<label for="form-json-path-${currentBoxIndex}">JSON Path</label><br>
<input id="form-json-path-${currentBoxIndex}" type="text" class="json-path-json form-control" placeholder="data.chart"><br>
</div>
<div class="view-http hidden">
<form id="requestForm">
<label for="form-http-url-${currentBoxIndex}" class="input-label-required">URL</label>
<input id="form-http-url-${currentBoxIndex}" type="text" class="http-url form-control" required placeholder="http://localhost:8080/generate?key1=value1&key2=value2"><br>
<label for="form-http-method-${currentBoxIndex}" class="input-label-required">HTTP Method</label>
<select id="form-http-method-${currentBoxIndex}" class="http-method form-control">
<option value="GET">GET</option>
<option value="POST">POST</option>
</select><br>
<div class="headersContainer">
<label for="form-http-headers-${currentBoxIndex}">Headers</label><br>
<textarea id="form-http-headers-${currentBoxIndex}" class="http-headers form-control" placeholder='Content-Type=application/json\nAccept=*/*'></textarea><br>
</div>
<div class="bodyContainer">
<label for="form-http-body-${currentBoxIndex}">Body</label><br>
<textarea id="form-http-body-${currentBoxIndex}" class="http-body form-control" placeholder='{"key1": "value1", "key2": "value2"}'></textarea><br>
</div>
<div class="jsonPathContainer">
<label for="form-http-json-path-${currentBoxIndex}">JSON Path</label><br>
<input id="form-http-json-path-${currentBoxIndex}" type="text" class="json-path-http form-control" placeholder="data.chart"><br>
</div>
</form>
</div>
<div class="view-chart" style="display:none;">
<div class="chart" style="width: 100%; height: 100%;"></div>
</div>
</div>
`;
document.body.appendChild(box);
const dragHeader = box.querySelector('[data-drag-handle]');
const moveable = new Moveable(document.body, {
target: box,
dragTarget: dragHeader,
resizable: true,
keepRatio: false,
edge: true,
draggable: true,
checkInput: true,
});
box.querySelector('.input-type').addEventListener("mousedown", (e) => {
e.stopPropagation();
});
box.querySelector('.http-method').addEventListener("mousedown", (e) => {
e.stopPropagation();
});
moveable.on("drag", ({target, left, top}) => {
target.style.left = `${left}px`;
target.style.top = `${top}px`;
});
moveable.on("resize", ({target, width, height, delta, direction}) => {
const minWidth = 100;
const minHeight = 200;
if (width >= minWidth && height >= minHeight) {
target.style.width = `${width}px`;
target.style.height = `${height}px`;
if (direction[0] === -1) {
target.style.left = `${target.offsetLeft - delta[0]}px`;
}
if (direction[1] === -1) {
target.style.top = `${target.offsetTop - delta[1]}px`;
}
}
updatePlotSize(box);
});
setupZIndex(box);
box.querySelector('.btn-generate').addEventListener('click', () => reloadPlot(box));
box.querySelector('.btn-back').addEventListener('click', () => switchToJson(box));
box.querySelector('.btn-close').addEventListener('click', () => {
moveable.destroy(); // Clean up movable instance
box.remove();
});
box.querySelector('.btn-beautify').addEventListener('click', () => beautifyJson(box));
box.querySelector('.input-type').addEventListener('change', (event) => switchInputType(box, event.target.value));
box.querySelector('.json-input').value = jsonExample;
}
function switchInputType(box, type) {
const viewJson = box.querySelector('.view-json');
const viewHttp = box.querySelector('.view-http');
if (type === 'json') {
viewJson.style.display = 'block';
viewHttp.style.display = 'none';
} else {
viewJson.style.display = 'none';
viewHttp.style.display = 'block';
}
}
function switchToChart(box) {
const boxBody = box.querySelector('.box-body');
const viewJson = box.querySelector('.view-json');
const viewHttp = box.querySelector('.view-http');
const viewChart = box.querySelector('.view-chart');
const btnGenerate = box.querySelector('.btn-generate');
const btnBack = box.querySelector('.btn-back');
const viewInputType = box.querySelector('.view-input-type');
viewJson.style.display = 'none';
viewHttp.style.display = 'none';
viewChart.style.display = 'block';
btnGenerate.textContent = 'Reload';
btnBack.style.display = 'block';
viewInputType.style.display = 'none';
boxBody.style.padding = '30px';
updatePlotSize(box);
}
function switchToJson(box) {
const boxBody = box.querySelector('.box-body');
const inputType = box.querySelector('.input-type').value;
const viewJson = box.querySelector('.view-json');
const viewHttp = box.querySelector('.view-http');
const viewChart = box.querySelector('.view-chart');
const btnGenerate = box.querySelector('.btn-generate');
const btnBack = box.querySelector('.btn-back');
const viewInputType = box.querySelector('.view-input-type');
viewChart.style.display = 'none';
btnGenerate.textContent = 'Generate';
viewInputType.style.display = 'block';
btnBack.style.display = 'none';
if (inputType === 'json') {
viewJson.style.display = 'block';
viewHttp.style.display = 'none';
} else {
viewJson.style.display = 'none';
viewHttp.style.display = 'block';
}
boxBody.style.padding = '10px';
}
async function reloadPlot(box) {
const inputType = box.querySelector('.input-type').value;
let plotData;
try {
if (inputType === 'json') {
const jsonData = box.querySelector('.json-input').value;
const jsonPath = box.querySelector('.json-path-json').value;
plotData = JSON.parse(jsonData);
if (jsonPath) {
const pathParts = jsonPath.split('.');
for (const part of pathParts) {
plotData = plotData[part];
}
}
} else {
const url = box.querySelector('.http-url').value;
const method = box.querySelector('.http-method').value;
const headers = box.querySelector('.http-headers').value;
const body = box.querySelector('.http-body').value;
let parsedHeaders = parseHeaders(headers);
const response = await fetch(url, {
method: method,
headers: parsedHeaders,
body: method === 'POST' ? body : null
});
const jsonData = await response.json();
const jsonPath = box.querySelector('.json-path-http').value;
plotData = jsonData;
if (jsonPath) {
const pathParts = jsonPath.split('.');
for (const part of pathParts) {
plotData = plotData[part];
}
}
}
var layout = {
title: 'Responsive Plotly.js Chart',
autosize: true
};
var config = {
responsive: true
};
const plotDiv = box.querySelector('.chart');
Plotly.newPlot(plotDiv, plotData, layout, config);
switchToChart(box); // Switch to chart view immediately after generating the plot
} catch (error) {
console.error('Error:', error);
const inputType = box.querySelector('.input-type').value;
if (inputType === 'json') {
alert('Invalid JSON data');
} else {
alert('Invalid HTTP request');
}
}
}
function parseHeaders(headerString) {
const headers = {};
const lines = headerString.split('\n'); // Split the string by new lines
lines.forEach(line => {
const [key, value] = line.split('='); // Split each line by '='
if (key && value) {
headers[key.trim()] = value.trim(); // Trim and add to headers object
}
});
return headers;
}
function beautifyJson(box) {
const jsonInput = box.querySelector('.json-input');
try {
const jsonData = JSON.parse(jsonInput.value);
jsonInput.value = JSON.stringify(jsonData, null, 4);
} catch (error) {
console.error('Error:', error);
alert('Invalid JSON data');
}
}
function setupZIndex(box) {
box.addEventListener('mousedown', () => {
box.style.zIndex = ++highestZIndex;
});
}
function updatePlotSize(box) {
const plotDiv = box.querySelector('.chart');
if (plotDiv) {
Plotly.Plots.resize(plotDiv);
}
}
addNewBox();
</script>
</body>
</html>