4.4.0 • Published 2 months ago

chartjs-chart-error-bars v4.4.0

Weekly downloads
77
License
MIT
Repository
github
Last release
2 months ago

Chart.js Error Bars

License: MIT NPM Package Github Actions

Chart.js module for charting error bars plots. This plugin extends the several char types (bar, horizontalBar, line, scatter, polarArea) with their error bar equivalent (barWithErrorBars, horizontalBarWithErrorBars, lineWithErrorBars, scatterWithErrorBars, polarAreaWithErrorBars).

Works only with Chart.js >= 3.0.0

Bar Chart

bar char with error bars

Horizontal Bar Chart

horizontal bar chart with error bars

Line Chart

line chart with error bars

Scatterplot

scatter plot with error bars

Polar Area plot

polar area plot with error bars

Install

npm install --save chart.js chartjs-chart-error-bars

Usage

see Samples on Github

and Open in CodePen

Styling

Several new styling keys are added to the indiviual chart types

interface IErrorBarStyling {
  /**
   * line width of the center line
   * @default 1
   * @scriptable
   */
  errorBarLineWidth: number;
  /**
   * color of the center line
   * @default '#2c2c2c'
   * @scriptable
   */
  errorBarColor: string;
  /**
   * line width of the whisker lines
   * @default 1
   * @scriptable
   */
  errorBarWhiskerLineWidth: number;
  /**
   * width of the whiskers in relation to the bar width, use `0` to force a fixed with, see below
   * @default 0.2
   * @scriptable
   */
  errorBarWhiskerRatio: number;
  /**
   * pixel width of the whiskers for non bar chart cases
   * @default 20
   * @scriptable
   */
  errorBarWhiskerSize: number;
  /**
   * color of the whisker lines
   * @default '#2c2c2c'
   * @scriptable
   */
  errorBarWhiskerColor: string;
}

Data structure

The data structure depends on the chart type. It uses the fact that chart.js is supporting scatterplot. Thus, it is already prepared for object values.

Chart types: bar, line, `scatter

interface IErrorBarItem {
  /**
   * the actual value
   */
  y: number;
  /**
   * the minimal absolute error bar value
   */
  yMin: number;
  /**
   * the maximal absolute error bar value
   */
  yMax: number;
}

Chart type: horizontalBar

interface IErrorBarItem {
  /**
   * the actual value
   */
  x: number;
  /**
   * the minimal absolute error bar value
   */
  xMin: number;
  /**
   * the maximal absolute error bar value
   */
  xMax: number;
}

Chart type: scatter

interface IErrorBarItem {
  /**
   * the actual x value
   */
  x: number;
  /**
   * the minimal absolute error bar x value
   */
  xMin: number;
  /**
   * the maximal absolute error bar x value
   */
  xMax: number;
  /**
   * the actual y value
   */
  y: number;
  /**
   * the minimal absolute error bar y value
   */
  yMin: number;
  /**
   * the maximal absolute error bar y value
   */
  yMax: number;
}

Chart type: polarArea

interface IErrorBarItem {
  /**
   * the actual value
   */
  r: number;
  /**
   * the minimal absolute error bar value
   */
  rMin: number;
  /**
   * the maximal absolute error bar value
   */
  rMax: number;
}

Multiple Error Bars

Multiple error bars are supported.

multiple error bars

Styling

The styling options support different array version.

Note: as with other chart.js style options, using an array will be one value per dataset. Thus, to specify the values for different error bars, one needs to wrap it in an object with a v key having the value itself. The outer for the dataset, the inner for the error bars.

interface IErrorBarStyling {
  /**
   * line width of the center line
   * @default {v: [1, 3]}
   * @scriptable
   */
  errorBarLineWidth: number | { v: number[] };
  /**
   * color of the center line
   * @default {v: ['#2c2c2c', '#1f1f1f']}
   * @scriptable
   */
  errorBarColor: string | { v: string[] };
  /**
   * line width of the whisker lines
   * @default {v: [1, 3]}
   * @scriptable
   */
  errorBarWhiskerLineWidth: number | { v: number[] };
  /**
   * width of the whiskers in relation to the bar width, use `0` to force a fixed with, see below
   * @default {v: [0.2, 0.25]}
   * @scriptable
   */
  errorBarWhiskerRatio: number | { v: number[] };
  /**
   * pixel width of the whiskers for non bar chart cases
   * @default {v: [20, 24]}
   * @scriptable
   */
  errorBarWhiskerSize: number | { v: number[] };
  /**
   * color of the whisker lines
   * @default {v: ['#2c2c2c', '#1f1f1f']}
   * @scriptable
   */
  errorBarWhiskerColor: string | { v: string[] };
}

Data structure

Just use array of numbers for the corresponding data structures attributes (xMin, xMax, yMin, yMax). The error bars will be rendered in reversed order. Thus, by convention the most inner error bar is in the first place.

e.g.

{
  y: 4,
  yMin: [2, 1],
  yMax: [5, 6]
}

ESM and Tree Shaking

The ESM build of the library supports three shaking but having no side effects. As a consequence the chart.js library won't be automatically manipulated nor new controllers automatically registered. One has to manually import and register them.

import Chart from 'chart.js';
import { BarWithErrorBars } from 'chartjs-chart-error-bars';

// register controller in chart.js and ensure the defaults are set
BarWithErrorBars.register();

const chart = new Chart(document.getElementById('canvas').getContext('2d'), {
  type: BarWithErrorBars.id,
  data: {
    labels: ['A', 'B'],
    datasets: [
      {
        data: [
          {
            y: 4,
            yMin: 1,
            yMax: 6,
          },
          {
            y: 2,
            yMin: 1,
            yMax: 4,
          },
        ],
      },
    ],
  },
});

Development Environment

npm i -g yarn
yarn set version 2
yarn
yarn pnpify --sdk

Building

yarn install
yarn build
4.4.0

2 months ago

4.3.4

4 months ago

4.3.2

7 months ago

4.2.3

10 months ago

4.3.1

8 months ago

4.3.3

7 months ago

4.2.4

10 months ago

4.3.0

10 months ago

4.2.2

10 months ago

4.2.1

10 months ago

4.2.0

1 year ago

4.1.2

1 year ago

4.1.0

1 year ago

4.0.1

1 year ago

4.1.1

1 year ago

4.0.0-alpha

2 years ago

3.10.0

2 years ago

4.0.0

1 year ago

3.9.2

2 years ago

3.9.1

2 years ago

3.9.0

2 years ago

3.8.0

2 years ago

3.7.2

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

3.6.0

3 years ago

3.5.0

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-rc.0

3 years ago

3.0.0-beta.9

3 years ago

3.0.0-beta.7

3 years ago

3.0.0-beta.6

3 years ago

3.0.0-beta.5

4 years ago

3.0.0-beta.4

4 years ago

3.0.0-beta.3

4 years ago

3.0.0-beta.1

4 years ago

3.0.0-alpha.20

4 years ago

3.0.0-alpha.2

4 years ago

3.0.0-alpha.1

4 years ago

1.2.1

4 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago