<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Line Graph Maker</title>
<link rel="stylesheet" href="styles.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div class="container">
<h1>Line Graph Maker</h1>
<div class="input-section">
<label>Graph Title:</label>
<input type="text" id="graphTitle" placeholder="Enter graph title">
<label>Labels (comma separated):</label>
<input type="text" id="labels" placeholder="e.g. Jan, Feb, Mar">
<label>Data (comma separated):</label>
<input type="text" id="data" placeholder="e.g. 10, 20, 30">
<label>Line Color:</label>
<input type="color" id="lineColor" value="#007bff">
<label>Background Color:</label>
<input type="color" id="bgColor" value="#a8d5ff">
<button id="generateBtn">Generate Graph</button>
<button id="downloadBtn">Download Graph</button>
</div>
<canvas id="lineChart"></canvas>
</div>
<script src="script.js"></script>
</body>
</html>
/* General Styles */
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
/* Container */
.container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
width: 90%;
max-width: 600px;
text-align: center;
}
/* Title */
h1 {
color: #333;
}
/* Input Fields */
.input-section {
display: flex;
flex-direction: column;
gap: 10px;
margin-bottom: 20px;
}
input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 14px;
}
/* Buttons */
button {
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
color: white;
transition: 0.3s;
}
#generateBtn {
background: #28a745;
}
#downloadBtn {
background: #007bff;
}
button:hover {
opacity: 0.8;
}
/* Responsive */
@media (max-width: 500px) {
input, button {
width: 100%;
}
}
let chart; // Store chart instance
document.getElementById('generateBtn').addEventListener('click', function () {
const title = document.getElementById('graphTitle').value;
const labels = document.getElementById('labels').value.split(',').map(item => item.trim());
const data = document.getElementById('data').value.split(',').map(item => parseFloat(item.trim()));
const lineColor = document.getElementById('lineColor').value;
const bgColor = document.getElementById('bgColor').value;
if (labels.length === 0 || data.length === 0 || labels.length !== data.length) {
alert("Please enter valid labels and data.");
return;
}
// Destroy previous chart if exists
if (chart) chart.destroy();
const ctx = document.getElementById('lineChart').getContext('2d');
chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: title || 'My Graph',
data: data,
borderColor: lineColor,
backgroundColor: bgColor,
borderWidth: 2,
fill: true,
tension: 0.4
}]
},
options: {
responsive: true,
plugins: {
legend: { display: true }
},
scales: {
y: { beginAtZero: true }
}
}
});
});
document.getElementById('downloadBtn').addEventListener('click', function () {
if (!chart) {
alert("Please generate a graph first!");
return;
}
const link = document.createElement('a');
link.href = document.getElementById('lineChart').toDataURL();
link.download = 'line_graph.png';
link.click();
});
