Skip to content

Instantly share code, notes, and snippets.

@orgcontrib
Created August 31, 2022 15:44
Show Gist options
  • Save orgcontrib/f882aa61fb194dba6da08f17c5603a94 to your computer and use it in GitHub Desktop.
Save orgcontrib/f882aa61fb194dba6da08f17c5603a94 to your computer and use it in GitHub Desktop.
Visualising a CSV file with Chart.js (basic)
<div id="wrapper">
<canvas id="chart"></canvas>
</div>
function makeChart(players) {
// players is an array of objects where each object is something like:
// {
// "Name": "Steffi Graf",
// "Weeks": "377",
// "Gender": "Female"
// }
var playerLabels = players.map(function(d) {
return d.Name;
});
var weeksData = players.map(function(d) {
return +d.Weeks;
});
var chart = new Chart('chart', {
type: "horizontalBar",
options: {
maintainAspectRatio: false,
legend: {
display: false
}
},
data: {
labels: playerLabels,
datasets: [
{
data: weeksData
}
]
}
});
}
// Request data using D3
d3
.csv("https://s3-us-west-2.amazonaws.com/s.cdpn.io/2814973/atp_wta.csv")
.then(makeChart);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>
#wrapper {
height: 1000px;
}

Visualising a CSV file with Chart.js (basic)

This pen demonstrates how to load a comma separated value (CSV) file using D3 and visualising it using Chart.js. The data is hosted as a Codepen asset, but could be hosted anywhere on the web.

A Pen by Peter Cook on CodePen.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment