const salesData = [
{ month: 'January', sales: 1000 },
{ month: 'February', sales: 1200 },
{ month: 'March', sales: 1500 }
];
fetch
API to get stock price data.fetch('https://api.example.com/stock/prices')
.then(response => response.json())
.then(data => {
// Use the data for visualization
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device - width, initial - scale=1.0">
<title>D3 Bar Chart</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<script>
const data = [4, 8, 15, 16, 23, 42];
const width = 400;
const height = 200;
const barPadding = 1;
const svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * (width / data.length))
.attr("y", d => height - d * 4)
.attr("width", width / data.length - barPadding)
.attr("height", d => d * 4)
.attr("fill", "teal");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device - width, initial - scale=1.0">
<title>Chart.js Line Chart</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="myChart"></canvas>
<script>
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June'],
datasets: [{
label: 'Sales',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'rgb(75, 192, 192)',
fill: false
}]
},
options: {}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device - width, initial - scale=1.0">
<title>Highcharts Pie Chart</title>
<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body>
<div id="container"></div>
<script>
Highcharts.chart('container', {
chart: {
type: 'pie'
},
title: {
text: 'Browser market shares in January, 2022'
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Chrome',
y: 70.67
}, {
name: 'Safari',
y: 13.82
}, {
name: 'Firefox',
y: 4.63
}]
}]
});
</script>
</body>
</html>
npm install d3
) or included directly from a CDN as shown in the examples.npm install chart.js
) or included from a CDN.<div>
or <canvas>
) where the visualization will be rendered.data()
method to bind data to DOM elements. The enter()
method is used to create new elements for data points that don’t have corresponding DOM elements yet.responsive
option to true
in the chart configuration.on()
method to listen for mouse events.svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.on("mouseover", function () {
d3.select(this).attr("fill", "orange");
})
.on("mouseout", function () {
d3.select(this).attr("fill", "teal");
});
requestAnimationFrame
method instead of setInterval
or setTimeout
for better performance.JavaScript offers a wide range of libraries and tools for data visualization, each with its own strengths and use cases. Whether you need to create simple charts or complex custom visualizations, there is a JavaScript library that can meet your requirements. By following the usage methods, common practices, and best practices outlined in this blog, you can create effective and engaging data visualizations that help users understand and analyze data more easily.