<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图表生成器</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(45deg, #3498db, #8e44ad);
color: white;
}
.container {
background: rgba(255, 255, 255, 0.1);
padding: 2em;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
backdrop-filter: blur(10px);
width: 80%;
max-width: 800px;
}
h1 {
text-align: center;
margin-bottom: 1em;
}
.input-area {
display: flex;
flex-direction: column;
gap: 1em;
margin-bottom: 1em;
}
input, select, button {
padding: 0.5em;
border: none;
border-radius: 5px;
}
button {
background-color: #2ecc71;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #27ae60;
}
#chartContainer {
background: white;
border-radius: 5px;
padding: 1em;
margin-top: 1em;
}
</style>
</head>
<body>
<div class="container">
<h1>图表生成器</h1>
<div class="input-area">
<input type="text" id="labels" placeholder="标签 (逗号分隔)">
<input type="text" id="data" placeholder="数据 (逗号分隔)">
<select id="chartType">
<option value="bar">柱状图</option>
<option value="line">折线图</option>
<option value="pie">饼图</option>
</select>
<button onclick="generateChart()">生成图表</button>
</div>
<div id="chartContainer">
<canvas id="myChart"></canvas>
</div>
</div>
<script>
let myChart = null;
function generateChart() {
const labels = document.getElementById('labels').value.split(',');
const data = document.getElementById('data').value.split(',').map(Number);
const chartType = document.getElementById('chartType').value;
if (myChart) {
myChart.destroy();
}
const ctx = document.getElementById('myChart').getContext('2d');
myChart = new Chart(ctx, {
type: chartType,
data: {
labels: labels,
datasets: [{
label: '数据',
data: data,
backgroundColor: [
'rgba(255, 99, 132, 0.6)',
'rgba(54, 162, 235, 0.6)',
'rgba(255, 206, 86, 0.6)',
'rgba(75, 192, 192, 0.6)',
'rgba(153, 102, 255, 0.6)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
],
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true
}
}
}
});
}
</script>
</body>
</html>