[go: up one dir, main page]

0% found this document useful (0 votes)
26 views4 pages

Code

Uploaded by

Ankit barnwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Code

Uploaded by

Ankit barnwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CODE:-

HTML Code:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Chart Generator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Chart Generator</h1>
<div class="form">
<label for="chartType">Select Chart Type:</label>
<select id="chartType">
<option value="bar">Bar Chart</option>
<option value="line">Line Chart</option>
<option value="pie">Pie Chart</option>
</select>
<label for="dataInput">Enter Data (comma-separated):</label>
<input type="text" id="dataInput" placeholder="e.g., 10, 20, 30">
<button onclick="generateChart()">Generate Chart</button>
</div>
<div class="chart-container">
<canvas id="myChart"></canvas>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="script.js"></script>
</body>
</html>
CSS Code:-

body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}

.container {
max-width: 800px;
margin: 50px auto;
text-align: center;
}
.form {
margin-bottom: 20px;
}body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 50px auto;
text-align: center;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
transition: box-shadow 0.3s ease;
}
.container:hover {
box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
}
h1 {
color: #333;
}
.form {
margin-bottom: 20px;
}
label {
margin-right: 10px;
color: #555;
}
input[type="text"] {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
transition: border-color 0.3s ease;
}
input[type="text"]:focus {
border-color: #66afe9;
}
select, button {
padding: 8px;
border: none;
border-radius: 4px;
background-color: #007bff;
color: #fff;
cursor: pointer;
transition: background-color 0.3s ease;
}
select:hover, button:hover {
background-color: #0056b3;
}
.chart-container {
margin-top: 20px;
}
label {
margin-right: 10px;
}
input, select, button {
margin-bottom: 10px;
}
.chart-container {
margin-top: 20px;
}

Javascript code:-

function generateChart() {
// Fetching user input
const chartType = document.getElementById('chartType').value;
const dataInput = document.getElementById('dataInput').value;

// Parsing data into an array of numbers


const data = dataInput.split(',').map(Number);
// Clearing existing chart if any
if(window.myChart instanceof Chart) {
window.myChart.destroy();
}
// Rendering new chart
const ctx = document.getElementById('myChart').getContext('2d');
window.myChart = new Chart(ctx, {
type: chartType,
data: {
labels: Array.from({ length: data.length }, (_, i) => 'Label ' + (i + 1)),
datasets: [{
label: 'Data',
data: data,
backgroundColor: 'rgba(54, 162, 235, 0.6)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}

You might also like