D3 template that should integrate with current production.
Last active
July 31, 2017 15:39
-
-
Save LayneSmith/f08d8a6c16aa07836d9fd38be1c6644e to your computer and use it in GitHub Desktop.
D3.js bl.ock template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ////////////////////////////////////////////////////////////////////////// | |
// BUILD D3 CHART | |
// ////////////////////////////////////////////////////////////////////////// | |
const buildChart = (data) => { | |
console.log('buildChart()', data); | |
// Margins | |
const margin = { top: 0, right: 0, bottom: 0, left: 0 }; | |
// Set width and height | |
const w = $('#chart-id').width() - margin.left - margin.right; | |
const h = $('#chart-id').height() - margin.top - margin.bottom; | |
// Create svg and append chart area | |
const svg = d3.select('#chart-id').append('svg') | |
.attr('width', w + margin.left + margin.right) | |
.attr('height', h + margin.top + margin.bottom) | |
.append('g') | |
.classed('chart-area', true) | |
.attr('transform', `translate(${margin.left}, ${margin.top})`); | |
// X-AXIS | |
// Definie xScale | |
const xScale = d3.scaleLinear() | |
.range([0, w]) | |
.domain([]) | |
.nice(); | |
// add the x axis | |
svg.append('g') | |
.attr('transform', `translate(0, ${h})`) | |
.call(d3.axisBottom(xScale)); | |
// Y-AXIS | |
// Definie yScale | |
const yScale = d3.scaleLinear() | |
.range([h, 0]) | |
.domain([]) | |
.nice(); | |
// add the y axis | |
svg.append('g') | |
.call(d3.axisLeft(yScale)); | |
} | |
d3.csv('data.csv', (data) => { | |
buildChart(data); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<!-- Add styles if necessary --> | |
<head> | |
<meta charset="utf-8"> | |
<title>D3 Template</title> | |
<style></style> | |
</head> | |
<body> | |
<div class="chart" id="chart-id"></div> | |
<script src="https://d3js.org/d3.v4.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment