DV Lab Exp 1-3
DV Lab Exp 1-3
Setting up an environment for data visualization tools depends on the specific tools or frameworks you want to use.
Here’s a general guide to get you started:
bash
Install libraries:
bash
python
2. Tableau
3. Power BI
install.packages("ggplot2")
install.packages("shiny")
Test your setup with a simple ggplot2 visualization:
library(ggplot2)
ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point()
For Google Data Studio, sign in with your Google account at datastudio.google.com.
Connect to your data source and start building interactive dashboards.
If you have a specific tool in mind, let me know, and I can provide more tailored instructions!
Exp :2
2. Develop the following Program Using HTML5 CANVAS and SVG TAG
To develop a program using HTML5 Canvas and the SVG tag, here's an example code snippet that combines these
two features for creating and displaying shapes and animations:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
svg {
display: block;
canvas {
display: block;
</style>
</head>
<body>
</svg>
<script>
ctx.fillStyle = 'blue';
ctx.beginPath();
ctx.fill();
ctx.fillStyle = 'red';
</script>
</body>
</html>
Explanation:
1. SVG Section:
o The <circle> element creates a blue circle.
o The <rect> element creates a red rectangle.
o The <text> element displays the text "SVG Shapes" in green color.
2. Canvas Section:
o The <canvas> tag provides a drawable area.
o The JavaScript code uses the getContext() method to draw a blue circle, a red rectangle, and
green text.
Feel free to adapt this code to your specific requirements or add interactivity! If you need further assistance with
enhancements or explanations, let me know!
2a. Develop the Different basic Graphical Shapes using HTM5 CANVAS
Here’s an example of how you can create different basic graphical shapes using HTML5 Canvas. The <canvas>
element, along with JavaScript, allows us to draw and manipulate shapes dynamically:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
canvas {
display: block;
margin: 20px auto;
</style>
</head>
<body>
<script>
// Draw a rectangle
ctx.fillStyle = 'blue';
// Draw a circle
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.fill();
// Draw a line
ctx.strokeStyle = 'green';
ctx.lineWidth = 5;
ctx.beginPath();
ctx.stroke();
// Draw a triangle
ctx.fillStyle = 'orange';
ctx.beginPath();
ctx.fill();
// Draw an ellipse
ctx.fillStyle = 'purple';
ctx.beginPath();
ctx.ellipse(100, 300, 50, 25, 0, 0, Math.PI * 2); // x, y, radiusX, radiusY, rotation, startAngle, endAngle
ctx.fill();
</script>
</body>
</html>
1. Rectangle:
o ctx.fillRect(x, y, width, height) draws a solid rectangle.
2. Circle:
o ctx.arc(x, y, radius, startAngle, endAngle) creates a circular path, and
ctx.fill() fills it with color.
3. Line:
o Use ctx.beginPath(), ctx.moveTo(), and ctx.lineTo() to draw a line, and
ctx.stroke() to render it.
4. Triangle:
o Move to the first point, then draw lines to the next two points using ctx.lineTo(). Finally,
close the path with ctx.closePath() and fill it.
5. Ellipse:
o ctx.ellipse() creates an ellipse with specified radii and orientation.
This script sets up a canvas and demonstrates how to draw five basic shapes: rectangle, circle, line, triangle, and
ellipse. You can modify the colors, positions, and sizes to suit your needs! If you'd like more examples or advanced
shapes, just let me know.
2b. Develop the Different Advanced Graphical Shapes using HTM5 CANVAS
Developing advanced graphical shapes with HTML5 Canvas involves combining geometric primitives,
transformations, patterns, and effects to create more intricate visuals. Here's an example of some advanced
shapes and techniques:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
canvas {
display: block;
</style>
</head>
<body>
<script>
gradient.addColorStop(0, 'blue');
gradient.addColorStop(1, 'white');
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.fill();
ctx.beginPath();
ctx.lineTo(outerX, outerY);
ctx.lineTo(innerX, innerY);
ctx.closePath();
ctx.fillStyle = 'gold';
ctx.fill();
}
drawStar(ctx, 150, 150, 50, 5, 0.5);
img.src = 'https://via.placeholder.com/100';
img.onload = () => {
ctx.fillStyle = pattern;
};
ctx.strokeStyle = 'purple';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(300, 400);
ctx.stroke();
ctx.beginPath();
ctx.lineTo(polygonX, polygonY);
}
ctx.closePath();
ctx.fillStyle = 'cyan';
ctx.fill();
</script>
</body>
</html>
1. Gradient Circle:
o Uses a radial gradient to create a shaded circle for a 3D effect.
2. Star Shape:
o A function draws a custom star with adjustable points and inset values for spikes.
3. Pattern-Filled Rectangle:
o Uses createPattern() to fill a rectangle with a tiled image pattern.
4. Bezier Curve:
o Demonstrates a smooth and elegant curve controlled by Bezier control points.
5. Polygon:
o A dynamic function creates a polygon with customizable sides and size.
How It Works:
Run this HTML file in a browser, and you'll see all these advanced shapes rendered on the Canvas.
Feel free to adjust colors, sizes, and positions, or add animation for even more interactivity. Let me know if
you'd like help making these shapes dynamic!
2c. Develop the Different basic Graphical Shapes using HTM5 SVG
Here’s an example of how to create different basic graphical shapes using the HTML5 <svg> tag. SVG (Scalable
Vector Graphics) is perfect for drawing vector-based shapes directly in HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG Basic Shapes</title>
<style>
svg {
display: block;
margin: 20px auto;
border: 1px solid black;
}
</style>
</head>
<body>
<h1>SVG Basic Shapes</h1>
<svg width="400" height="400">
<!-- Rectangle -->
<rect x="50" y="50" width="100" height="50" fill="blue" />
1. Rectangle:
o <rect> defines a rectangle with x and y as the top-left corner coordinates, and width and
height for size.
2. Circle:
o <circle> defines a circle using cx (center x), cy (center y), and r (radius).
3. Ellipse:
o <ellipse> creates an oval shape, defined by cx, cy, rx (horizontal radius), and ry (vertical
radius).
4. Line:
o <line> connects two points, defined by x1, y1 (starting point) and x2, y2 (ending point).
5. Polygon:
o <polygon> draws a closed shape with vertices specified in the points attribute.
6. Polyline:
o <polyline> is similar to a polygon but does not close the shape automatically.
7. Text:
o <text> is used to display text at specific coordinates, with customizable styles.
How It Works:
Copy and paste the code into an HTML file and open it in a browser to see the various shapes rendered on
the page.
SVG is scalable and resolution-independent, making it great for creating high-quality graphics.
2d. Develop the Different Advanced Graphical Shapes using HTM5 SVG
Here’s an example of how to create advanced graphical shapes using HTML5 SVG. SVG provides a wide variety of
features to create intricate and dynamic shapes, including gradients, patterns, paths, and transformations. Here's
how you can implement some advanced examples:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
svg {
display: block;
</style>
</head>
<body>
<defs>
</radialGradient>
</defs>
fill="red" />
<defs>
</pattern>
</defs>
fill="gold" />
</svg>
</body>
</html>
1. Gradient-Filled Circle:
o Uses <radialGradient> to fill the circle with a smooth gradient transitioning from blue to
white.
2. Complex Path (Heart Shape):
o The <path> element uses cubic Bézier curves (C) to create a heart shape.
3. Pattern-Filled Rectangle:
o A <pattern> is defined with small green circles, and the rectangle is filled with this repeating
pattern.
4. Star:
o A custom polygon with multiple points is used to create a star shape.
5. Cubic Bézier Curve:
o A curved path is drawn using control points for smooth transitions, styled with a purple stroke.
How to Use:
Copy and paste the code into an HTML file and open it in your browser to view the advanced SVG shapes.
You can customize the colors, positions, and sizes of shapes to fit your needs.
Let me know if you'd like more animations, interactivity, or additional SVG features! These advanced shapes can be
combined with CSS or JavaScript to create even more dynamic
Exp:3
Develop Following Program Using HTML5 and
HTML5 and data visualization, here's an example that combines HTML5 with JavaScript and a popular data
visualization library, such as Chart.js, to create an interactive bar chart:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
// Example data
const data = {
labels: labels,
datasets: [{
borderWidth: 1
}]
};
// Chart configuration
const config = {
data: data,
options: {
scales: {
y: {
beginAtZero: true
};
// Render the chart
document.getElementById('myChart'),
config
);
</script>
</body>
</html>
Key Points:
1. HTML5 Canvas:
o The <canvas> element serves as the container for the data visualization. It is rendered using the
Chart.js library.
2. Chart.js Library:
o The example uses Chart.js to generate a responsive bar chart. You can include Chart.js via a CDN
as shown in the <script> tag.
3. Data and Configuration:
o The data object specifies the labels (categories) and datasets (values) for the chart.
o The config object defines the chart type (bar chart in this case) and its display options.
4. Customizing the Chart:
o Modify the labels and data.datasets.data arrays to display your specific data.
o Change the type in the config object to switch to other chart types like line, pie, or
doughnut.
How to Run:
1. Copy the code into an .html file and open it in a web browser.
2. You’ll see a bar chart that displays the sales data for each month.
Would you like further enhancements, such as adding multiple datasets, interactivity (e.g., tooltips), or switching to
a different type of chart? Just let me know!
Here’s how you can develop a simple bar chart using HTML5 Canvas. This example demonstrates how to use the
<canvas> element and JavaScript to draw a basic bar chart:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
canvas {
display: block;
</style>
</head>
<body>
<script>
const data = [30, 70, 50, 90, 110]; // Heights of the bars
const labels = ['A', 'B', 'C', 'D', 'E']; // Labels for each bar
// Chart setup
// Draw bars
// Draw bar
ctx.fillStyle = 'blue';
ctx.fillStyle = 'black';
ctx.textAlign = 'center';
});
// Draw axes
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.beginPath();
// Add title
ctx.fillStyle = 'black';
ctx.textAlign = 'center';
</script>
</body>
</html>
Explanation:
1. Canvas Setup:
o A <canvas> element is used as a container for the bar chart, and a 2D rendering context
(getContext('2d')) is used to draw shapes.
2. Data and Labels:
o The data array contains the values for the height of each bar, while the labels array provides
labels for each bar.
3. Dynamic Bar Drawing:
o Each bar is scaled according to the maximum data value and the canvas height.
4. Axes and Labels:
o Basic x and y axes are drawn for reference, and labels are placed under each bar.
5. Title:
o The chart has a title displayed at the top.
How to Run:
Let me know if you'd like enhancements, such as grid lines, legends, or animations!
3b. Read the data .txt file and draw Data Table
To visualize data from a .txt file and display it in a data table using HTML5, JavaScript, and a bit of CSS, here's
an example implementation:
Steps:
1. Use a .txt file with tabular data, where rows and columns are separated by a specific delimiter (e.g.,
commas or tabs).
2. Use JavaScript to read and parse the data from the file.
Name,Age,Occupation
Alice,30,Engineer
Bob,25,Designer
Charlie,35,Teacher
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
table {
border-collapse: collapse;
width: 80%;
table, th, td {
th, td {
padding: 10px;
text-align: center;
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div id="tableContainer"></div>
<script>
function generateTable(data) {
const tr = document.createElement('tr');
cells.forEach(cell => {
cellElement.textContent = cell.trim();
tr.appendChild(cellElement);
});
table.appendChild(tr);
});
tableContainer.appendChild(table);
if (file) {
reader.onload = () => {
};
reader.readAsText(file);
});
</script>
</body>
</html>
ow It Works:
How to Use:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
document.getElementById('fileInput').addEventListener('change',
function(event) {
const file = event.target.files[0];
if (file) {
reader.onload = function(e) {
lines.forEach(line => {
labels.push(label.trim());
data.push(Number(value.trim()));
});
drawBarChart(labels, data);
};
reader.readAsText(file);
});
ctx.fillStyle = "skyblue";
ctx.fillStyle = "black";
ctx.textAlign = "center";
});
ctx.beginPath();
ctx.lineTo(50, 10);
ctx.stroke();
</script>
</body>
</html>
How to Use
This way, you can dynamically visualize data from a file. Let me know if you
need help with something specific!
3d. Read the data .csv file and draw Data Table
You can use an HTML element and JavaScript to read data from a file and
dynamically create a data table. Here's an example:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 80%;
border-collapse: collapse;
th, td {
padding: 8px;
text-align: center;
th {
background-color: #f2f2f2;
</style>
</head>
<body>
<div id="tableContainer"></div>
<script>
document.getElementById('fileInput').addEventListener('change',
function(event) {
if (file) {
reader.onload = function(e) {
const tr = document.createElement('tr');
cells.forEach(cell => {
cellElement.textContent = cell.trim();
tr.appendChild(cellElement);
});
table.appendChild(tr);
});
container.appendChild(table);
};
reader.readAsText(file);
}
});
</script>
</body>
</html>
How to Use:
3e. Read the data .csv file and draw Column Bar Chart
Here’s an example of how you can read data from a file and dynamically draw a
column bar chart using the HTML5 element and JavaScript:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
const canvas = document.getElementById('barChart');
document.getElementById('fileInput').addEventListener('change',
function(event) {
if (file) {
reader.onload = function(e) {
labels.push(cells[0].trim());
data.push(Number(cells[1].trim()));
});
drawColumnBarChart(labels, data);
};
reader.readAsText(file);
});
ctx.fillStyle = "steelblue";
ctx.fillStyle = "black";
ctx.textAlign = "center";
});
ctx.beginPath();
ctx.lineTo(50, 10);
ctx.stroke();
}
</script>
</body>
</html>
Output:
Label,Value
January,10
February,15
March,20
April,25
May,30
3f. Read the data XML file and draw Data Table
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 80%;
border-collapse: collapse;
th, td {
padding: 8px;
text-align: center;
th {
background-color: #f2f2f2;
</style>
</head>
<body>
<div id="tableContainer"></div>
<script>
document.getElementById('fileInput').addEventListener('change',
function(event) {
if (file) {
reader.onload = function(e) {
if (columns) {
const th = document.createElement('th');
th.textContent = columns[i].nodeName;
headerRow.appendChild(th);
}
table.appendChild(headerRow);
const tr = document.createElement('tr');
const td = document.createElement('td');
td.textContent = rows[i].children[j].textContent;
tr.appendChild(td);
table.appendChild(tr);
container.appendChild(table);
};
reader.readAsText(file);
}
});
</script>
</body>
</html>
<data>
<row>
<Name>John</Name>
<Age>30</Age>
<City>New York</City>
</row>
<row>
<Name>Jane</Name>
<Age>25</Age>
<City>Los Angeles</City>
</row>
<row>
<Name>Mike</Name>
<Age>35</Age>
<City>Chicago</City>
</row>
</data>
3g. Read the data XML file and draw Simple Chart
<!DOCTYPE html>
<html>
<head>
<title>Simple Chart from XML</title>
</head>
<body>
<input type="file" id="fileInput" accept=".xml" />
<canvas id="chartCanvas" width="800" height="500" style="border:1px
solid #000;"></canvas>
<script>
const canvas = document.getElementById('chartCanvas');
const ctx = canvas.getContext('2d');
document.getElementById('fileInput').addEventListener('change',
function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
// Parse XML
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "text/xml");
drawSimpleChart(labels, data);
};
reader.readAsText(file);
}
});
Here's an example of how to read JSON data and dynamically create a data
table in HTML using JavaScript:
<!DOCTYPE html>
<html>
<head>
table {
width: 80%;
border-collapse: collapse;
th, td {
padding: 8px;
text-align: center;
th {
background-color: #f2f2f2;
</style>
</head>
<body>
<div id="tableContainer"></div>
<script>
document.getElementById('fileInput').addEventListener('change',
function(event) {
if (file) {
reader.onload = function(e) {
createTable(jsonData);
};
reader.readAsText(file);
});
function createTable(data) {
headers.forEach(header => {
const th = document.createElement('th');
th.textContent = header;
headerRow.appendChild(th);
});
table.appendChild(headerRow);
data.forEach(row => {
const tr = document.createElement('tr');
headers.forEach(header => {
const td = document.createElement('td');
td.textContent = row[header];
tr.appendChild(td);
});
table.appendChild(tr);
});
// Display the table
container.appendChild(table);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
document.getElementById('fileInput').addEventListener('change',
function(event) {
if (file) {
reader.onload = function(e) {
labels.push(item.label);
data.push(item.value);
});
drawSimpleChart(labels, data);
};
reader.readAsText(file);
});
ctx.fillStyle = "steelblue";
ctx.fillStyle = "black";
ctx.textAlign = "center";
});
ctx.beginPath();
ctx.lineTo(50, 10);
ctx.stroke();
}
</script>
</body>
</html>
[output:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
];
.attr("width", svgWidth)
.attr("height", svgHeight);
.range([0, width])
.padding(0.2);
.range([height, 0]);
// Draw axes
chart.append("g")
.call(d3.axisLeft(yScale))
.attr("class", "y-axis");
chart.append("g")
.call(d3.axisBottom(xScale))
.attr("class", "x-axis")
// Draw bars
chart.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("width", xScale.bandwidth())
.attr("fill", "steelblue");
chart.selectAll(".label")
.data(data)
.enter()
.append("text")
.attr("class", "label")
.attr("text-anchor", "middle")
</script>
</body>
</html>
Steps:
This example uses D3.js to create a scalable and customizable column chart. Let
me know if you'd like a similar example using Canvas.js, or if you'd like me to
adjust the chart further
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
{ label: "January", categoryA: 10, categoryB: 20, categoryC: 30 },
];
// Set dimensions
// SVG container
.attr("width", svgWidth)
.attr("height", svgHeight);
// Stack generator
const stack = d3.stack()
// Scales
.range([0, width])
.padding(0.2);
.nice()
.range([height, 0]);
// Draw axes
chart.append("g")
.call(d3.axisLeft(yScale))
.attr("class", "y-axis");
chart.append("g")
.call(d3.axisBottom(xScale))
.attr("class", "x-axis")
// Draw bars
chart.selectAll(".layer")
.data(stackedData)
.enter()
.append("g")
.attr("class", "layer")
.selectAll("rect")
.data(d => d)
.enter()
.append("rect")
// Add legend
legendRow.append("rect")
.attr("width", 15)
.attr("height", 15)
.attr("fill", colorScale(category));
legendRow.append("text")
.attr("x", 20)
.attr("y", 12)
.attr("text-anchor", "start")
.text(category)
.style("font-size", "12px");
});
</script>
</body>
</html>
Steps to Run:
This chart uses D3.js to stack data and create color-coded groups for better
visualization. Let me know if you'd like further enhancements or need help with
Canvas.js!
4c. Showing the Data as a column chart for four age group
Here's how you can create a column chart representing data for four age groups
using D3.js:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
const data = [
{ ageGroup: "0-18", value: 20 },
];
.attr("width", svgWidth)
.attr("height", svgHeight);
// Create scales
.range([0, width])
.padding(0.2);
.range([height, 0]);
// Draw axes
chart.append("g")
.call(d3.axisLeft(yScale))
.attr("class", "y-axis");
chart.append("g")
.call(d3.axisBottom(xScale))
.attr("class", "x-axis")
// Draw bars
chart.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("width", xScale.bandwidth())
.attr("fill", "steelblue");
chart.selectAll(".label")
.data(data)
.enter()
.append("text")
.attr("class", "label")
.attr("text-anchor", "middle")
</script>
</body>
</html>
Steps to Use:
This example uses D3.js to handle the scales and draw a column chart. It can
easily be extended with interactivity, tooltips, or dynamic data if needed. Let
me know if you'
4d. Showing the data as a Line chart (single, fewer and multiple lines
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
// Set dimensions
.attr("width", svgWidth)
.attr("height", svgHeight);
// Scales
.range([0, width]);
.range([height, 0]);
// Draw axes
chart.append("g")
.call(d3.axisLeft(yScale));
chart.append("g")
.call(d3.axisBottom(xScale));
// Line generator
chart.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("d", line);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
];
// Set dimensions
.attr("width", svgWidth)
.attr("height", svgHeight);
// Scales
.range([0, width]);
const yScale = d3.scaleLinear()
.range([height, 0]);
// Draw axes
chart.append("g")
.call(d3.axisLeft(yScale));
chart.append("g")
.call(d3.axisBottom(xScale));
// Line generator
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("d", lineA);
chart.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "orange")
.attr("stroke-width", 2)
.attr("d", lineB);
</script>
</body>
</html>
If you want multiple lines (3 or more), you can generalize the approach:
<!DOCTYPE html>
<html>
<head>
<title>Multiple Line Chart</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
];
// Set dimensions
.attr("width", svgWidth)
.attr("height", svgHeight);
// Scales
.range([0, width]);
.range([height, 0]);
.domain(keys)
chart.append("g")
.call(d3.axisLeft(yScale));
chart.append("g")
.call(d3.axisBottom(xScale));
// Line generator
// Draw lines
keys.forEach(key => {
chart.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", colorScale(key))
.attr("stroke-width", 2)
.attr("d", d3.line()
.x(d => xScale(d.month))
);
});
// Add legend
keys.forEach((key, i) => {
legendRow.append("rect")
.attr("width", 15)
.attr("height", 15)
.attr("fill", colorScale(key));
legendRow.append("text")
.attr("x", 20)
.attr("y", 12)
.attr("text-anchor", "start")
.text(key)
.style("font-size", "12px");
});
</script>
</body>
</html>
4e. Showing the data as a Pie Chart (single and multiple pie)
Single Pie Chart Example
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
const data = [
];
.attr("width", width)
.attr("height", height)
.append("g")
svg.selectAll("path")
.data(pie(data))
.enter()
.append("path")
.attr("d", arc)
.attr("stroke", "white")
.style("stroke-width", "2px");
// Add labels
svg.selectAll("text")
.data(pie(data))
.enter()
.append("text")
.attr("text-anchor", "middle")
.style("font-size", "12px");
</script>
</body>
</html>
Multiple Pie Charts Example
To display multiple pie charts, we can generate one for each dataset and
position them side by side.
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
const datasets = [
],
[
{ label: "Group A", value: 25 },
];
.attr("width", width)
.attr("height", height);
datasets.forEach((data, i) => {
pieGroup.selectAll("path")
.data(pie(data))
.enter()
.append("path")
.attr("d", arc)
.attr("stroke", "white")
.style("stroke-width", "2px");
pieGroup.selectAll("text")
.data(pie(data))
.enter()
.append("text")
.attr("text-anchor", "middle")
});
</script>
</body>
</html>
This example creates a single bar chart displaying one set of data.
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
];
.attr("width", width)
.attr("height", height);
// Scales
.padding(0.2);
.range([chartHeight, 0]);
// Axes
chart.append("g")
.call(d3.axisLeft(yScale));
chart.append("g")
.call(d3.axisBottom(xScale));
// Bars
chart.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("width", xScale.bandwidth())
.attr("fill", "steelblue");
</script>
</body>
</html>
This example creates a grouped bar chart for multiple categories per label.
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
// Sample data
const data = [
];
.attr("width", width)
.attr("height", height);
.range([0, chartWidth])
.padding(0.2);
.domain(categories)
.range([0, xScale.bandwidth()])
.padding(0.1);
.range([chartHeight, 0]);
.domain(categories)
.range(["steelblue", "orange"]);
// Axes
chart.append("g")
.call(d3.axisLeft(yScale));
chart.append("g")
.call(d3.axisBottom(xScale));
// Bars
chart.append("g")
.selectAll("g")
.data(data)
.enter()
.append("g")
.selectAll("rect")
.enter()
.append("rect")
.attr("width", xSubScale.bandwidth())
</body>
</html>
exp5. Develop Following Program Using HTML5 and Google Chats API and
Map API
To create a program using HTML5, Google Chat API, and Google Maps API,
you need to integrate both APIs into your web application for specific
functionality like handling messages and visualizing location data.
Below is a simple example illustrating the integration of Google Maps API
(for map rendering) and Google Chat API (mockup of message handling). For
Google Chat API, you will need access to the API via Google Cloud Console
and proper authentication.
Example Program
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 400px;
width: 100%;
body {
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?
key=YOUR_API_KEY"></script>
</head>
<body>
<div>
<h4>Send a Message:</h4>
<button onclick="sendMessage()">Send</button>
</div>
<div>
<h4>Messages:</h4>
<ul id="messageList"></ul>
</div>
<div id="map"></div>
<script>
function initMap() {
zoom: 12,
center: defaultLocation,
});
new google.maps.Marker({
position: defaultLocation,
map: map,
});
function sendMessage() {
const li = document.createElement("li");
li.textContent = message;
messageList.appendChild(li);
// You can send the message to Google Chat API here (example mock
function)
document.getElementById("messageInput").value = "";
} else {
}
</script>
</body>
</html>