
Product
Introducing PHP and Composer Support in Socket
Socket now supports PHP with full Composer and Packagist integration, enabling developers to search packages, generate SBOMs, and protect their PHP dependencies from supply chain threats.
@terrestris/d3-util
Advanced tools
A set of helper classes for working with d3 charts.
npm i @terrestris/d3-util
In order to use the library in your project, make sure you load the moment.js library as well.
The ChartRenderer is the main entry point to render a chart. It features a
render method that can be used to render its components to a given DOM node.
The chart renderer can be constructed with a few parameters:
components: a list of chart components constituting the chartsize: the desired chart size in pixelszoomType: the desired zoom type. 'none' for no zoom, 'transform' for a
zoom using the svg transform attribute without rebuilding the DOM or
'rerender' for a zoom rebuilding the DOM. Using the transform method has the
effect of also zooming the shapes, i.e. a line of 1px will get bigger when
zooming in.onMouseMoveFunc: the function that will be called on every mouse move over the
entire svg.The legend component simply renders a list of legend elements. It accepts the following configuration parameters:
extraClasses: a string with extra CSS classes to add to the legend grouplegendEntryMaxLength: a number with the max number of characters per line
for legend labelsposition: an array [x, y] with the desired position within the svgitems: an array of legend element configurationsThe legend items can have the following configuration parameters:
contextmenuHandler: can be a function that is called in case the user
right clicks or long touches the item. Argument is the d3.event eventcustomRenderer: can be a function that is called during rendering and will
be called with the item's group element. Can be used to e.g. render custom
buttons for the legend elementonClick: can be a function getting the current d3.event once an item is
clickedonHover: can be a function getting the current d3.event once the mouse is
over the itemonMouseOut: can be a function getting the current d3.event once the mouse is
leaving the itemstyle: can be an object with svg style parameters such as stroke,
stroke-width etc. in order to style the legend icontitle: the legend textvalue: if set, a value attribute will be added to the legend nodetype: can be 'line', 'bar', 'area' or 'background' in order to
render a line, a small bar chart icon, a line with the area below it filled or
just a filled blockThe bar component can render (grouped) bar charts. It accepts the following configuration parameters:
axes: an axis configuration object (see below)backgroundColor: can be a hex color stringextraClasses: a string with extra CSS classes to set on the bar chart nodeposition: the [x, y] coordinatesrotateBarLabel: if true, the labels will be rotated by 90°size: the [width, height] sizetitle: if set, a heading will be rendered with this texttitleColor: the color hex stringtitlePadding: title padding from the toptitleSize: title sizegroupedInitiallyHidden: a list of grouping indexes for bars that should initially be hiddengroupsInitiallyHidden: a list of group indexes for bars that should initially be hiddendata: the data object. Example:{
data: [{
value: 'Value 1',
values: [{
index: 'Group 1',
value: 23,
uncertainty: undefined,
color: '#ff0000',
belowThreshold: false,
label: '23',
tooltipFunc: target => 'Tooltip'
}, {
index: 'Group 3',
value: 42,
uncertainty: 5.6,
color: '#00ff00',
belowThreshold: true,
label: '<'
}]
}],
grouped: ['Group 1', 'Group 2', 'Group 3']
}
The grouped field contains a list of the grouped indexes, the data list
contains the actual groups. A group object contains a value with the group
name/label and a list of values corresponding to the actual bars.
The bar objects can have a number of configuration values:
index: corresponds to one of the value in the grouped list (not all
grouped values must be contained in every group)value: the actual y valueuncertainty: if set to a value, an uncertainty indicator will be rendered
on top of the barcolor: the hex color valuebelowThreshold: if true, no bar will be drawn, just the labellabel: if set, a label will be rendered onto the bartooltipFunc: if set, the function will be called with the bar element upon
mouseoverThe timeseries component can render multiple line charts. It supports the following configuration parameters:
axes: an axis configuration object (see below)backgroundColor: as color hex stringextraClasses: extra classes to set on the timeseries chart nodeposition: the [x, y] coordinatessize: the [width, height] sizetitle: if set, a heading will be rendered with this texttitleColor: the color hex stringtitlePadding: title padding from the toptitleSize: title sizeinitialZoom: if set, an initial zoom transform object (with x, y, kx and ky set)moveToRight: if set to true, the initialZoom will be modified so the timeseries is scrolled completely to the rightseries: a list of line chart configurations as followsA line chart configuration has the following options:
axes: a list of axis ids referencing x and y axis configurations like
['x', 'y0']color: the color hex stringcurveType: a d3 curve type string like curveStepBeforeshapeType: usually 'line', can also be 'area'useTooltipFunc: a boolean indicating whether the third data value is a
tooltip mouseover function and fifth value is a mouseout functiondata: an array with the data: [xvalue, yvalue, mouseOverFunc, styleObject, mouseOutFunc]initiallyVisible: a boolean indicating whether the line is initially visible
defaulting to trueThe tooltip function is optional as well as the style. If the style is set, it is used to configure the corresponding point symbol. Examples for style objects would be:
{
"type": "circle",
"radius": "5"
}
{
"type": "circle",
"radius": "10"
}
{
"type": "star",
"sides": 5,
"radius": 10
}
{
"type": "rect",
"width": 15,
"height": 20
}
The axis configuration objects configure the scales and axes of a chart. You'll
need to configure at least an x and a y axis, but at least for line charts
multiple y axes are allowed (you still must use the same x axis for all lines).
The object maps axis ids (with x and y mandatory) to axis configurations,
which may have the following options:
display: boolean that determines whether the axis is drawn or notformat: a d3 format string like ",.2f", "dynamic", a format array or a format functionlabel: an optional axis labellabelColor: the label color hex stringlabelPadding: label paddinglabelRotation: label rotationlabelSize: label sizemax: the axis and scale max value (optional)min: the axis and scale min value (optional)orientation: the axis orientation (x or y)sanitizeLabels: if set to true, overlapping tick labels will be removed
(only supported for y axes)scale: the scale to use (linear, log, time)tickPadding: the tick paddingtickSize: the tick sizefactor: if set, the axis' max value will be divided by this value so
you can get a bigger interval when using auto calculated min/max valuesIf the format is set to "dynamic", it will be dynamically adjusted to the
following d3-format equivalents, based on the rendered value:
value < 1: .3f1 < value < 10: .2f10 < value < 100: .1f100 < value: .0fIf the format is set to an array, it must be an array of objects like this:
[{
"min": 0,
"max": 1,
"format": ".3f"
}, {
"min": 1,
"max": 100,
"format": ".0f"
}]
A format matches a value, if the value is >= min and < max. You can use
"-Infinity" and "Infinity" for min and max to get unbounded classes.
A note on line charts: you can have multiple y axes here. Using the line chart axis references you can have some lines correspond to one y axis and some lines correspond to another y axis. As noted above, you still need to use the same x axis for all lines (else the chart would supposedly be confusing anyway).
If you want to contribute, you can build the project like this:
git checkout https://github.com/terrestris/d3-utilcd d3-utilnpm iand then either
npm run build:devin order to get a development build or
npm run build:distin order to get a production build.
FAQs
A set of helper classes for working with d3 charts.
We found that @terrestris/d3-util demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 17 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports PHP with full Composer and Packagist integration, enabling developers to search packages, generate SBOMs, and protect their PHP dependencies from supply chain threats.

Security News
An AI agent is merging PRs into major OSS projects and cold-emailing maintainers to drum up more work.

Research
/Security News
Chrome extension CL Suite by @CLMasters neutralizes 2FA for Facebook and Meta Business accounts while exfiltrating Business Manager contact and analytics data.