项目作者: sgratzl

项目描述 :
Chart.js Parallel Coordinates Plot
高级语言: TypeScript
项目地址: git://github.com/sgratzl/chartjs-chart-pcp.git
创建时间: 2020-04-14T12:37:23Z
项目社区:https://github.com/sgratzl/chartjs-chart-pcp

开源协议:MIT License

下载


Chart.js Parallel Coordinate Plots

NPM Package Github Actions

Chart.js module for charting parallel coordinate plots (PCPs). Adding new chart type: pcp.

MTCars

Open in CodePen

MTCars Tooltip

MTCars Tension

Open in CodePen

Check out also my other chart.js plugins:

Install

  1. npm install --save chart.js chartjs-chart-pcp

Usage

see Examples

Open in CodePen

PCP

Data Structure

the data items are the regular data items along with their labels. For each attribute there is a dataset. e.g., in the following example there are three items (A, B, C) with three axes / features (f1, f2, f3).

  1. const objs = [
  2. { label: 'A', f1: 5, f2: 3, f4: 3 },
  3. { label: 'B', f1: 2, f2: 1, f4: 8 },
  4. { label: 'C', f1: 10, f2: 6, f4: 2 },
  5. ];
  6. const attrs = ['f1', 'f2', 'f3'];
  7. const config = {
  8. type: 'pcp',
  9. data: {
  10. labels: objs.map((obj) => obj.label),
  11. datasets: attrs.map((attr) => ({
  12. label: attr,
  13. data: objs.map((obj) => obj[attr]),
  14. })),
  15. },
  16. options: {},
  17. };

Styling of elements

Two new elements were added: lineSegment as a subclass of line for a line segment between two axes and linearAxis for representing the vertical axis as a wrapper around a linear scale.

see https://github.com/sgratzl/chartjs-chart-pcp/blob/develop/src/elements/lineSegment.ts#L3-L9

see https://github.com/sgratzl/chartjs-chart-pcp/blob/develop/src/elements/axis.ts#L13-L21

Scaling

The Parallel Coordinates controller pcp uses a linear scale. There is also the logarithmicPcp that uses a logarithmic scale.
Using chart.js hybrid charts, one can specify the type per dataset. e.g.,

  1. const config = {
  2. type: 'pcp',
  3. data: {
  4. labels: ['1', '2', '3'],
  5. datasets: [
  6. {
  7. label: 'A',
  8. data: [1, 2, 3]
  9. },
  10. {
  11. type: 'logarithmicPcp',
  12. label: 'B',
  13. data: [1, 2, 10000]
  14. }
  15. })),
  16. },
  17. options: {},
  18. };

ESM and Tree Shaking

The ESM build of the library supports tree shaking thus 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.

Variant A:

  1. import Chart from 'chart.js';
  2. import { ParallelCoordinatesController, LinearAxis, LineSegment, PCPScale } from 'chartjs-chart-pcp';
  3. Chart.register(ParallelCoordinatesController, PCPScale, LineSegment);
  4. Chart.registry.addElements(LinearAxis);
  5. ...
  6. new Chart(ctx, {
  7. type: ParallelCoordinatesController.id,
  8. data: [...],
  9. });

Variant B:

  1. import { ParallelCoordinatesChart } from 'chartjs-chart-pcp';
  2. new ParallelCoordinatesChart(ctx, {
  3. data: [...],
  4. });

Development Environment

  1. npm i -g yarn
  2. yarn install
  3. yarn sdks vscode

Common commands

  1. yarn compile
  2. yarn test
  3. yarn lint
  4. yarn fix
  5. yarn build
  6. yarn docs