pythonbook/fastapi_plotly/index2.html

62 lines
1.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Scatter Plot with Input</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#plot {
height: 80vh;
width: 100vw;
}
#inputForm {
padding: 20px;
}
</style>
</head>
<body>
<div id="inputForm">
<input type="number" id="xInput" placeholder="X value">
<input type="number" id="yInput" placeholder="Y value">
<input type="number" id="zInput" placeholder="Z value">
<button onclick="submitData()">Submit</button>
</div>
<div id="plot"></div>
<script>
var trace = {
x: [], y: [], z: [],
mode: 'markers',
type: 'scatter3d'
};
var layout = {
title: '3D Scatter Plot'
};
Plotly.newPlot('plot', [trace], layout);
function submitData() {
var xValue = document.getElementById('xInput').value;
var yValue = document.getElementById('yInput').value;
var zValue = document.getElementById('zInput').value;
fetch('http://localhost:8000/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({x: xValue, y: yValue, z: zValue}),
})
.then(response => response.json())
.then(data => {
Plotly.extendTraces('plot', { x: [[data.x]], y: [[data.y]], z: [[data.z]] }, [0]);
});
}
</script>
</body>
</html>