GithubHelp home page GithubHelp logo

terrestris / d3-util Goto Github PK

View Code? Open in Web Editor NEW
4.0 13.0 3.0 2.71 MB

A helper library to create d3 charts

License: Other

JavaScript 2.02% TypeScript 97.97% Shell 0.02%
d3 chart line-chart bar-chart hacktoberfest

d3-util's Introduction

d3-util

Build Status Coverage Status

A set of helper classes for working with d3 charts.

Installation

npm i @terrestris/d3-util

In order to use the library in your project, make sure you load the moment.js library as well.

Usage

General notes

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 chart
  • size: the desired chart size in pixels
  • zoomType: 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.

Components

LegendComponent

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 group
  • legendEntryMaxLength: a number with the max number of characters per line for legend labels
  • position: an array [x, y] with the desired position within the svg
  • items: an array of legend element configurations

The 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 event
  • customRenderer: 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 element
  • onClick: can be a function getting the current d3.event once an item is clicked
  • onHover: can be a function getting the current d3.event once the mouse is over the item
  • onMouseOut: can be a function getting the current d3.event once the mouse is leaving the item
  • style: can be an object with svg style parameters such as stroke, stroke-width etc. in order to style the legend icon
  • title: the legend text
  • value: if set, a value attribute will be added to the legend node
  • type: 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 block

BarComponent

The 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 string
  • extraClasses: a string with extra CSS classes to set on the bar chart node
  • position: the [x, y] coordinates
  • rotateBarLabel: if true, the labels will be rotated by 90°
  • size: the [width, height] size
  • title: if set, a heading will be rendered with this text
  • titleColor: the color hex string
  • titlePadding: title padding from the top
  • titleSize: title size
  • groupedInitiallyHidden: a list of grouping indexes for bars that should initially be hidden
  • groupsInitiallyHidden: a list of group indexes for bars that should initially be hidden
  • data: 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 value
  • uncertainty: if set to a value, an uncertainty indicator will be rendered on top of the bar
  • color: the hex color value
  • belowThreshold: if true, no bar will be drawn, just the label
  • label: if set, a label will be rendered onto the bar
  • tooltipFunc: if set, the function will be called with the bar element upon mouseover

TimeseriesComponent

The timeseries component can render multiple line charts. It supports the following configuration parameters:

  • axes: an axis configuration object (see below)
  • backgroundColor: as color hex string
  • extraClasses: extra classes to set on the timeseries chart node
  • position: the [x, y] coordinates
  • size: the [width, height] size
  • title: if set, a heading will be rendered with this text
  • titleColor: the color hex string
  • titlePadding: title padding from the top
  • titleSize: title size
  • initialZoom: 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 right
  • series: a list of line chart configurations as follows

A 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 string
  • curveType: a d3 curve type string like curveStepBefore
  • shapeType: 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 function
  • data: an array with the data: [xvalue, yvalue, mouseOverFunc, styleObject, mouseOutFunc]
  • initiallyVisible: a boolean indicating whether the line is initially visible defaulting to true

The 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
      }

Axis configuration objects

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 not
  • format: a d3 format string like ",.2f", "dynamic", a format array or a format function
  • label: an optional axis label
  • labelColor: the label color hex string
  • labelPadding: label padding
  • labelRotation: label rotation
  • labelSize: label size
  • max: 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 padding
  • tickSize: the tick size
  • factor: 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 values

If the format is set to "dynamic", it will be dynamically adjusted to the following d3-format equivalents, based on the rendered value:

  • value < 1: .3f
  • 1 < value < 10: .2f
  • 10 < value < 100: .1f
  • 100 < value: .0f

If 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).

Development

If you want to contribute, you can build the project like this:

  • git checkout https://github.com/terrestris/d3-util
  • cd d3-util
  • npm i

and then either

  • npm run build:dev

in order to get a development build or

  • npm run build:dist

in order to get a production build.

d3-util's People

Contributors

annarieger avatar dependabot[bot] avatar dnlkoch avatar greenkeeper[bot] avatar hblitza avatar hwbllmnn avatar jansule avatar kaivolland avatar marcjansen avatar unraveler avatar weskamm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

d3-util's Issues

An in-range update of webpack is breaking the build 🚨

The devDependency webpack was updated from 4.35.3 to 4.36.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Release Notes for v4.36.0

Features

  • SourceMapDevToolPlugin append option now supports the default placeholders in addition to [url]
  • Arrays in resolve and parser options (Rule and Loader API) support backreferences with "..." when overriding options.
Commits

The new version differs by 42 commits.

  • 95d21bb 4.36.0
  • aa1216c Merge pull request #9422 from webpack/feature/dot-dot-dot-merge
  • b3ec775 improve merging of resolve and parsing options
  • 53a5ae2 Merge pull request #9419 from vankop/remove-valid-jsdoc-rule
  • ab75240 Merge pull request #9413 from webpack/dependabot/npm_and_yarn/ajv-6.10.2
  • 0bdabf4 Merge pull request #9418 from webpack/dependabot/npm_and_yarn/eslint-plugin-jsdoc-15.5.2
  • f207cdc remove valid jsdoc rule in favour of eslint-plugin-jsdoc
  • 31333a6 chore(deps-dev): bump eslint-plugin-jsdoc from 15.3.9 to 15.5.2
  • 036adf0 Merge pull request #9417 from webpack/dependabot/npm_and_yarn/eslint-plugin-jest-22.8.0
  • 37d4480 Merge pull request #9411 from webpack/dependabot/npm_and_yarn/simple-git-1.121.0
  • ce2a183 chore(deps-dev): bump eslint-plugin-jest from 22.7.2 to 22.8.0
  • 0beeb7e Merge pull request #9391 from vankop/create-hash-typescript
  • bf1a24a #9391 resolve super call discussion
  • bd7d95b #9391 resolve discussions, AbstractMethodError
  • 4190638 chore(deps): bump ajv from 6.10.1 to 6.10.2

There are 42 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.