0.26.3 • Published 1 year ago

@intoto-dev/bibliotheca-graph v0.26.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

<Graph /> component

Live Demo

Graph allows the user to visualize data in a graph format. It has multiple variations of how to visualise the data which is provided. It supports displaying tooltip information, line charts, bar charts, missing data, predictions, threshold ranges, and mean level.

Example Graph

Installation

Add Graph to your project with the following command:

npm i @intoto-dev/bibliotheca-graph

Basic Usage

There are two properties on the graph component which are mandatory. series is a list of lines or bars to be displayed and their options (more details below). t is the translation function to be used to translate messages and text used in the component, more information below.

import { Graph } from '@intoto-dev/bibliotheca-graph';

const series = []; // Array of series data
const t = translationLibrary; // Load in your preferred translation library

<Graph
  series={series}
  t={t}
/>

What are series?

Series is an array containing information about the data which needs to be plotted in the graph. Each item describes a line or bar, their name, and the data they contain.

This is an example of a series, which is very bare bones and has no options whatsoever. You can use this as a starting point for your own series. Refer to the live demos for some examples of how to make nicer looking series.

const series = [
  {
    key: 'identifier',
    data: [
      { value: 1, date: '2022-05-06T09:00:00.000Z' },
      { value: 2, date: '2022-05-06T09:01:00.000Z' },
      // ... etc
    ],
  },
];

Note that dates are in ISO 8601 format and can contain (and should contain) timezone information. This information is used to render the data points when they happened locally, and not relative to the user. For instance: a data point recorded at 12:00:00 in UTC+0:00 is also shown as 12:00:00 when the user viewing the data is in UTC+02:00. Graph handles the shifting in date for you.

Translation function

The function you provide to t is used to translate messages and text. It gets a string as the first argument and expects a string to be returned. It would look like so in TypeScript:

const t = (key: string) => 'Translated string';

A great library to use for this is i18next, or more specifically in React: react-i18next.

The most basic implementation (also found in Storybook examples) is:

const t = (key: string) => {
    switch (key) {
      case 'updated_at':
        return 'Updated {time} ago';
      case 'missing':
        return 'Missing data';
      case 'mean_level':
        return 'Mean-level';
      default:
        return 'Prediction';
    }
};

This includes all possible translation keys and an example of how {time} is used in updated_at.

API

React Component: <Graph />

import { Graph } from '@intoto-dev/bibliotheca-graph';
PropertyDescriptionTypeDefault
series *List of series to be displayed in the graph.GraphSeries[]
t *Translation function.(key: string) => string
heightHeight of the graph in pixels.number200
tooltipWhether to show a tooltip when hovering over a point.booleantrue
stackedWhether to display the series in a stacked manner. Default is merged into a single Y-axis (multiple lines in a graph).booleanfalse
navigationWhether to display the navigation bar. This allows zooming of large data sets.booleanfalse
langLanguage to used for the graph. This is mainly for date formatting.'en' or 'nb'en
localeThe date-fns locale to use for date formatting.Localeen-US
showCurrentDraws a line at the current time and shows the current value of the first series at that point.booleanfalse
linesA list of extra lines to be drawn in the graph. Useful for mean-level and "now" (helpers available)GraphLine[][]
onTooltipValueChangeCallback for when the tooltip value changes. Used for external communication.(value: number) => void

Type: GraphSeries

PropertyDescriptionTypeDefault
key *Unique identifier for the series.string
data *List of data points to be displayed in the series.DataPoint[]
typeType of series to be displayed.'line' or 'bar''line'
nameHow the series is called and displayed in the legend.string
colorHexadecimal color code line / bar in the series.string
thresholdThe level (value) where the threshold is broken.number
thresholdColorHexadecimal color code for the threshold line (normal will be other color).string
thresholdDirectionDirection of where the threshold is calculated from.'up' or 'down''up'
domainDomain of the series (min and max values). This automatically calculated from the data, but can be overwritten.[number, number]
labelWidthWidth of the label in pixels on the Y-Axis.number44
axisHeightHeight of the axis in pixels on the Y-Axis. Defaults to the graph's height.number
barWidthWidth of the bar in a bar chart. This is automatically calculated, but can be overwritten.number
barPaddingPadding between bars in a bar chart. This is automatically calculated, but can be overwritten.number
barOpacityOpacity of the bar in a bar chart.number1
unitUnit to be appended to the values in the series.string
formatValueFormat function for the values in the series.(value: number) => string or number(value: number) => value
curveCurve to be used for drawing the line. A list of curves can be found in the visx/curves library.CurveFactorycurveNatural
areaWhether to draw a gradient filled area from the drawn line to the bottom.booleanfalse
bottomThe value which is considered the bottom for the area. Automatically calculated as the lowest value in data.number
tooltipExtraPass a React Component or Function Component which accepts point as a property to render extra content in the tooltip on hover.FunctionComponent<{ point: DatePoint }> or ComponentClass<{ point: DatePoint }>

Type: DataPoint

PropertyDescriptionTypeDefault
value *Value of the data pointnumber
date *Date of the data point (ISO 8601)string
missingWhether the data point needs to be marked as missingbooleanfalse
predictionWhether the data point is a predictionbooleanfalse
minValueWhen the data point is a prediction, this is the smallest predicted valuenumber
maxValueWhen the data point is a prediction, this is the largest predicted valuenumber

Type: GraphLine

A GraphLine is either a HorizontalLine or a VerticalLine.

Type: HorizontalLine

PropertyDescriptionTypeDefault
name *Name of the line. Also the name which will be displayed in the legendstring
color *The color of the linestring
value *The value on the Y-axis where the line needs to be drawnnumber
widthStroke width of the linenumber1
dasharrayStroke dash array of the linestring'5,3'
opacityStroke opacitynumber1
indicatorWhether to show the name of the line in the legend, also show a small indication arrow head on the axisbooleanfalse

Type: VerticalLine

Same properties as HorizontalLine but with value and date swapped.

PropertyDescriptionTypeDefault
date *The date on the X-axis where the line needs to be drawnDate

Helper: isMissing

import { isMissing } from '@intoto-dev/bibliotheca-graph';

Given a DataPoint object, returns whether the data point is considered missing or not. This is a type guard way of being sure of the MissingDataPoint interface.

function isMissing(dataPoint: DataPoint): boolean;
PropertyDescriptionTypeDefault
dataPoint *Data point to checkDataPoint

Helper: isPredicted

import { isPredicted } from '@intoto-dev/bibliotheca-graph';

Given a DataPoint object, returns whether the data point is a prediction or not. This is a type guard way of being sure of the PredictedDataPoint interface.

function isPredicted(dataPoint: DataPoint): boolean;
PropertyDescriptionTypeDefault
dataPoint *Data point to checkDataPoint

Helper: createYScale

import { createYScale } from '@intoto-dev/bibliotheca-graph';

Creates a d3 and visx compatible scale for the Y-Axis based and the series data and desired height.

function createYScale(series: GraphSeries, height: number, padding: number): ScaleLinear<number, number>;
PropertyDescriptionTypeDefault
series *Series to create the Y-scale fromGraphSeries[]
height *Height in pixels of the graph's Y-axisnumber
paddingPadding in pixels added to the bottom and top of the Y-axisnumber0

Helper: createXScale

import { createXScale } from '@intoto-dev/bibliotheca-graph';

Creates a d3 and visx compatible scale for the X-Axis based on provided dates and the desired width.

function createXScale(dates: GraphSeries, width: number): ScaleTime<number, number>;
PropertyDescriptionTypeDefault
dates *Dates which make up the X-axis. Needs a minimum of two dates.Date[]
width *Width in pixels of the graph's X-axisnumber

Helper: shiftDate

import { shiftDate } from '@intoto-dev/bibliotheca-graph';

Get the date from an ISO 8601 compatible date string and shifts the time so that it will display the same time as stated in the date string instead of the relative time (which is default in browsers).

function shiftDate(date: string, direction?: 1 | -1): string;
PropertyDescriptionTypeDefault
date *Date string in ISO 8601 formatstring
directionThe direction the time is shifted to. Pass -1 to shift the opposite direction of the timezone offset.1 or -11

Helper: getTimezoneOffset

import { getTimezoneOffset } from '@intoto-dev/bibliotheca-graph';

Gets the timezone offset (same as Date.getTimezoneOffset()) given an ISO 8601 compatible date string. The result is the amount of minutes the offset is from UTC.

function getTimezoneOffset(date: string): number;
PropertyDescriptionTypeDefault
date *Date string in ISO 8601 formatstring

Helper: createMeanLevelLine

import { createMeanLevelLine } from '@intoto-dev/bibliotheca-graph';

Creates a HorizontalLine which is a line which is drawn at the mean level of the series. Creates an object to use in the lines property.

function createMeanLevelLine(name: string, value: number): HorizontalLine;
PropertyDescriptionTypeDefault
name *Name of the line which will also be used in the legend.string
value *The mean level in mASL.number

Helper: createMeanLevelLine

import { createNowLine } from '@intoto-dev/bibliotheca-graph';

Creates a VerticalLine which is a line which is drawn at the given date. Creates an object to use in the lines property.

function createNowLine(date: Date): VerticalLine;
PropertyDescriptionTypeDefault
date *The date where the 'now' line will be drawn.Date

React Hook: useDimensions

import { useDimensions } from '@intoto-dev/bibliotheca-graph';

A React hook which returns a ref binding function and a Dimensions object containing { width: number; height: number }. Use the ref binding function on the element you want to know the dimensions of.

function SomeComponent() {
  const [ref, dimensions] = useDimensions();
  
  return (
    <html>
      <body>
        <main ref={ref}>
          My size is {dimensions.width} x {dimensions.height} 
        </main>
      </body>
    </html>
  );
}

React Hook: useSeriesDates

import { useSeriesDates } from '@intoto-dev/bibliotheca-graph';

A React hook which gets and sorts dates which are present in a list of series.

function useSeriesDates(series: GraphSeries[]): Date[];
PropertyDescriptionTypeDefault
series *Series to get all the dates fromGraphSeries[]
0.25.0

1 year ago

0.26.3

1 year ago

0.26.2

1 year ago

0.26.1

1 year ago

0.26.0

1 year ago

0.24.8

1 year ago

0.24.7

2 years ago

0.24.6

2 years ago

0.23.6

2 years ago

0.23.5

2 years ago

0.23.4

2 years ago

0.23.3

2 years ago

0.23.9

2 years ago

0.23.8

2 years ago

0.23.7

2 years ago

0.23.10

2 years ago

0.24.5

2 years ago

0.24.4

2 years ago

0.24.3

2 years ago

0.24.2

2 years ago

0.24.1

2 years ago

0.23.2

3 years ago

0.23.1

3 years ago

0.20.0

3 years ago

0.19.0

3 years ago

0.17.2

3 years ago

0.19.1

3 years ago

0.17.3

3 years ago

0.19.2

3 years ago

0.17.4

3 years ago

0.19.3

3 years ago

0.17.5

3 years ago

0.15.0

3 years ago

0.17.0

3 years ago

0.17.1

3 years ago

0.23.0

3 years ago

0.21.2

3 years ago

0.21.1

3 years ago

0.21.0

3 years ago

0.18.1

3 years ago

0.18.2

3 years ago

0.16.0

3 years ago

0.14.2

3 years ago

0.18.0

3 years ago

0.22.0

3 years ago

0.11.0

3 years ago

0.11.1

3 years ago

0.13.0

3 years ago

0.11.2

3 years ago

0.11.3

3 years ago

0.11.4

3 years ago

0.9.2

3 years ago

0.10.1

3 years ago

0.12.0

3 years ago

0.14.0

3 years ago

0.14.1

3 years ago

0.10.0

3 years ago

0.8.1

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.3

3 years ago

0.8.0

3 years ago

0.6.2

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.3.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.1.10

3 years ago

0.1.0

4 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.1

4 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.9

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago