Export Image
Export Code

V5 Reusable Examples - Nested data, Chart (line) series - Example 2

JimMaltby

Last edited Nov 11, 2021
Created on Oct 31, 2021

An exploration of nesting data. Example 2.1 line chart with data nested.

Using the tutorial and series of charts described here:

https://amber.rbind.io/2017/05/02/nesting/

NOTES: tricky elements here are:

  1. setting up the data binding so that it selects the nested data for the line correctly
  2. setting up the scales so that they get the max and min values from the nested data correctly*

*Accessing the values is described here: https://stackoverflow.com/questions/37688982/nesting-d3-max-with-array-of-arrays

max_value = d3.max(data, function(topLevel) { return d3.max(topLevel.values, function(bottomLevel) { return bottomLevel.valuesNeededName; }); });

*Creating and accessor function that enables the values to be selected at the bottom level - allowing refactoring and reduction in code volume.

https://stackoverflow.com/questions/17156800/nested-arrays-in-d3-js function getValue(aValue){ return aValue.valuesNeededName }; const maxValue = d3.max(data.values, getValue);

D3.js version: v5.

D3 pattern used:

Addtional help from and aknowledgement to:

MIT Licensed