A reusable function for creating charts with axes
The formula for creating a Cartesian chart (rectangular, with X and Y coordinates) is pretty straightforward. Whether you're creating a bar chart, a scatter plot, a line or area chart, you'll almost always follow the same steps and create the same pieces.
This is of course the type of scenario where a reusable bit of code is an easy win.
While building examples for my upcoming book on D3 + SVG, I also wanted to hide these "plumbing" steps to keep the code focused on the skill being demonstrated. To that end, I created the function I'm sharing with you today.
(Click here to be notified as soon as the D3 + SVG book becomes available!)
Using the function
Before we walk through the function's code to understand it, let's look at how to use it.
That's it.
Well, that's it if you're fine with the defaults, which produces a chart that looks like this.
The default configuration is 600 pixels wide and 400 pixels tall, with both axes running from 0 to 100. The only thing you have to pass to initChart
is a selector string so it knows where to create the chart.
The optional second argument to initChart
is a configuration object which will be merged into a default config. The default configuration takes this shape.
config = width: 600 height: 400 margin: top: 10 right: 10 bottom: 20 left: 25 xScale: d3 yScale: d3
All of the following options would work.
Understanding the function
The function itself is fairly straightforward. Rather than break it into pieces with explanations above partial code blocks, I want to try something different. I have heavily annotated the function below in hopes it will be easier to understand.
Is this format helpful? Hit reply and let me know 👍 or 👎!
{ // default config definition // is merged with partial or full config param // largely thanks to ES6 rest params // see https://mzl.la/2AIh0Sk for more config = width: 600 height: 400 margin: top: 10 right: 10 bottom: 20 left: 25 // merge any margin props provided ...config || {}margin xScale: d3 yScale: d3 ...config // merge all top level props provided ; // store refs to important props const width height margin = config; // store chart's usable area in props const w = width - marginleft - marginright; const h = height - margintop - marginbottom; const svg = d3 // use the provided selector // create SVG of specified size // create root group that respects margins ; // create Y axis that fills vertical space svg; // create X axis that fills horizontal space // and sits at the bottom of the chart svg ; // return the root container so commands can be chained return svg;}
That's it! Make sense? If not, please let me know.
As always, you can play with the real thing here.
Happy hacking!