1.31.13 • Published 10 months ago

@nebula.js/sn-line-chart v1.31.13

Weekly downloads
1,854
License
MIT
Repository
github
Last release
10 months ago

@nebula.js/sn-line-chart

The line chart is a common, widely used visualization. It is often used to show trends over time.

Requirements

Requires @nebula.js/stardust version 1.7.0 or later.

Installing

If you use npm: npm install @nebula.js/sn-line-chart. You can also load through the script tag directly from https://unpkg.com.

Usage

import { embed } from '@nebula.js/stardust';
import line from '@nebula.js/sn-line-chart';

// 'app' is an enigma app model
const nuked = embed(app, {
  types: [
    {
      // register line chart
      name: 'line-chart',
      load: () => Promise.resolve(line),
    },
  ],
});

nuked.render({
  element: document.querySelector('.lines'),
  type: 'line-chart',
  fields: ['Date.autoCalendar.YearMonth', '=Avg(Gold)', '=Avg(Bitcoin)'],

  // Overrides default properties
  properties: {
    title: 'Price of Gold and Bitcoin',
    dataPoint: {
      show: true,
      showLabels: true,
    },
    gridLine: {
      auto: false,
    },
    dimensionAxis: {
      show: 'all',
      dock: 'near',
    },
    measureAxis: {
      spacing: 0.5,
      dock: 'near',
      show: 'all',
      logarithmic: true,
    },
  },
});

More examples

One dimension, two measures, area styling

nuked.render({
  element: document.querySelector('.lines'),
  type: 'line-chart',
  fields: ['Date.autoCalendar.YearMonth', '=Avg(Gold)', '=Avg(Bitcoin)'],

  // Overrides default properties
  properties: {
    lineType: 'area',
  },
});

One dimension, two measures, vertical orientation

nuked.render({
  element: document.querySelector('.lines'),
  type: 'line-chart',
  fields: ['Date.autoCalendar.Quarter', 'Date.autoCalendar.Year', '=Avg(Bitcoin)'],

  // Overrides default properties
  properties: {
    orientation: 'vertical',
    dimensionAxis: {
      continuousAuto: false,
      dock: 'near',
    },
    dataPoint: {
      show: true,
      showLabels: true,
    },
    preferContinuousAxis: false,
  },
});

Two dimensions, one measure

nuked.render({
  element: document.querySelector('.lines'),
  type: 'line-chart',
  // Two dimensions, one measure
  fields: ['Date.autoCalendar.Quarter', 'Date.autoCalendar.Year', '=Avg(Bitcoin)'],
  properties: {
    measureAxis: {
      dock: 'near',
      show: 'all',
      logarithmic: true,
    },
    dataPoint: {
      show: true,
      showLabels: true,
    },
  },
});

One dimension, two measures, two reference lines

nuked.render({
      element: document.querySelector('.lines'),
      type: 'line-chart',
      fields: ['Date.autoCalendar.YearMonth', '=Avg(Gold)', '=Avg(Bitcoin)'],

      // Overrides default properties
      properties: {
        refLine: {
          refLines: [
            {
              label: '',
              paletteColor: {
                color: 'green',
              },
              refLineExpr: {
                value: 52500,
              },
              show: true,
            },
            {
              label: '',
              paletteColor: {
                color: 'red',
              },
              refLineExpr: {
                value: 3570,
              },
              show: true,
            },
          ],
        },
        measureAxis: {
          dock: 'near',
          show: 'all',
          logarithmic: true,
        },
        dataPoint: {
          show: true,
        },
      },
    });
  });

Line chart plugins

A plugin can be passed into a line chart to add or modify its capability or visual appearance. A plugin needs to be defined before it can be rendered together with the chart.

// Step 1: define the plugin

// Modifying the look of the minor axis title
const minorAxisTitlePlugin = {
  info: {
    name: 'minor-axis-title-plugin',
    type: 'component-definition',
  },
  fn: ({ keys, layout }) => {
    const componentDefinition = {
      type: 'data-title',

      // Provide the same name as the exisiting component to override it
      key: keys.COMPONENT.MINOR_AXIS_TITLE,
      style: {
        fontFamily: 'Tahoma, san-serif',
        fontSize: '15px',
      },
    };
    return componentDefinition;
  },
};

// Step 2: passing the plugin definition into the render function

// Rendering a line chart with plugins
nuked.render({
  element: document.querySelector('#object'),
  type: 'sn-line-chart',
  plugins: [majorAxisTitlePlugin],
  fields: ['Date.autoCalendar.YearMonth', '=Avg(Gold)', '=Avg(Bitcoin)'],
  // eslint-disable-next-line no-undef
  properties: {
    title: 'History Price of Gold vesus Bitcoin (USD)',
    measureAxis: { show: 'all', logarithmic: true },
  },
});

The plugin definition is an object, with two properties info and fn. The fn returns a picasso.js component. To build this component, some important chart internals are passed into the argument object of fn.

// Structure of the argument object of fn
const pluginArgs = {
  layout,
  keys: {
    SCALE: {
      MAIN: {
        MINOR,
        MAJOR,
      },
    },
    COMPONENT: {
      LINE,
      MAJOR_AXIS,
      MAJOR_AXIS_TIME_INNER,
      MAJOR_AXIS_TITLE,
      MINOR_AXIS,
      MINOR_AXIS_TITLE,
    },
    COLLECTION: {
      MAIN,
    },
  },
};

With plugins, you can either add new components or modify existing components of the line chart.

Add new components

The new component can be a standard Picasso component or a custom Picasso component. Here we demo a custom component which add labels to the min/max positions of the line.

// Implement a custom min/max labels plugin, so that we can use it later
// to show the min/max of Bitcoin price since 2018
const minMaxLabelsPluginImplementation = {
  info: {
    componentName: 'custom-labels-plugin',
    name: 'custom-labels-plugin',
    type: 'custom-component',
  },
  fn: ({ keys }) => {
    const implementation = {
      require: ['chart', 'renderer'],
      render() {
        // We are only interested in data of Bitcoin after year 2018
        const items = this.chart
          .component(keys.COMPONENT.LINE)
          .data.items.filter((item) => item.line.value === 1 && item.label >= '2018');
        const scale = this.chart.scales();
        const timeScale = scale[keys.SCALE.MAIN.MAJOR];
        const lineScale = scale[keys.SCALE.MAIN.MINOR];
        const { width, height } = this.rect;
        const min = Math.min(...items.map((item) => item.end.value));
        const max = Math.max(...items.map((item) => item.end.value));
        const labels = [];
        items.forEach((item) => {
          if (item.end.value === min) {
            labels.push({
              type: 'text',
              text: `min: ${item.end.label}`,
              x: timeScale(item.major.value) * width,
              y: lineScale(item.end.value) * height + 15,
              anchor: 'middle',
              fontFamily: 'Tahoma, san-serif',
              fontSize: '15px',
              fill: 'darkred',
            });
          } else if (item.end.value === max) {
            labels.push({
              type: 'text',
              text: `max: ${item.end.label}`,
              x: timeScale(item.major.value) * width,
              y: lineScale(item.end.value) * height - 15,
              anchor: 'middle',
              fontFamily: 'Tahoma, san-serif',
              fontSize: '15px',
              fill: 'darkgreen',
            });
          }
        });
        return labels;
      },
    };
    return implementation;
  },
};

// Using the min/max labels plugin, defined above
const minMaxLabelsPlugin = {
  info: {
    name: 'labels',
    type: 'component-definition',
  },
  fn: ({ keys }) => {
    const componentDefinition = {
      // The type has to match with the componentName of the labels plugin definition above
      type: 'custom-labels-plugin',
      key: 'my-labels',
    };
    return componentDefinition;
  },
};

Modify existing components

As an example, the appearance of the line can be modified by plugin.

To overide an existing component, fn should returns a picasso.js component that has the same key as the existing component (keys.COMPONENT.LINE in this example)

// Modifying the look of the existing line component
const linePlugin = {
  info: {
    name: 'line-plugin',
    type: 'component-definition',
  },
  fn: ({ layout, keys }) => {
    const componentDefinition = {
      type: 'line',

      // Provide the same name as the exisiting line component to override it
      key: keys.COMPONENT.LINE,
      settings: {
        layers: { curve: 'monotone', line: { strokeWidth: 3 } },
      },
    };
    return componentDefinition;
  },
};

Plugins disclaimer

  • The plugins API is still experimental.
  • We can not guarantee our charts to be compatible with all different settings, especially when modifying existing components.
1.31.13

10 months ago

1.31.12

10 months ago

1.31.10

12 months ago

1.31.11

12 months ago

1.31.6

1 year ago

1.31.9

12 months ago

1.31.7

1 year ago

1.31.8

12 months ago

1.31.5

1 year ago

1.31.4

1 year ago

1.31.3

1 year ago

1.31.2

1 year ago

1.31.1

1 year ago

1.31.0

1 year ago

1.30.14

1 year ago

1.30.13

1 year ago

1.30.12

1 year ago

1.30.11

2 years ago

1.30.10

2 years ago

1.30.9

2 years ago

1.29.1

2 years ago

1.29.2

2 years ago

1.30.6

2 years ago

1.30.7

2 years ago

1.30.4

2 years ago

1.30.5

2 years ago

1.30.8

2 years ago

1.30.2

2 years ago

1.30.3

2 years ago

1.30.0

2 years ago

1.30.1

2 years ago

1.28.3

2 years ago

1.29.0

2 years ago

1.26.5

2 years ago

1.27.0

2 years ago

1.28.1

2 years ago

1.28.2

2 years ago

1.28.0

2 years ago

1.25.0

2 years ago

1.25.1

2 years ago

1.25.2

2 years ago

1.26.0

2 years ago

1.26.3

2 years ago

1.26.4

2 years ago

1.26.1

2 years ago

1.26.2

2 years ago

1.24.0

2 years ago

1.22.0

3 years ago

1.21.4

3 years ago

1.21.5

3 years ago

1.21.8

3 years ago

1.21.9

3 years ago

1.21.6

3 years ago

1.21.7

3 years ago

1.21.10

3 years ago

1.21.11

3 years ago

1.21.0

3 years ago

1.21.1

3 years ago

1.21.2

3 years ago

1.21.3

3 years ago

1.20.1

3 years ago

1.20.0

3 years ago

1.19.12

3 years ago

1.19.10

3 years ago

1.19.11

3 years ago

1.19.8

3 years ago

1.19.7

3 years ago

1.19.6

3 years ago

1.19.5

3 years ago

1.19.9

3 years ago

1.19.0

3 years ago

1.19.4

3 years ago

1.19.3

3 years ago

1.19.2

3 years ago

1.19.1

3 years ago

1.18.1

3 years ago

1.18.0

3 years ago

1.18.3

3 years ago

1.18.2

3 years ago

1.15.12

3 years ago

1.17.5

3 years ago

1.17.4

3 years ago

1.17.2

3 years ago

1.17.1

3 years ago

1.17.0

3 years ago

1.17.3

3 years ago

1.16.0

3 years ago

1.15.11

3 years ago

1.15.9

4 years ago

1.15.10

3 years ago

1.14.1

4 years ago

1.14.0

4 years ago

1.14.2

4 years ago

1.15.0

4 years ago

1.15.4

4 years ago

1.15.3

4 years ago

1.15.2

4 years ago

1.15.1

4 years ago

1.15.8

4 years ago

1.15.7

4 years ago

1.15.6

4 years ago

1.15.5

4 years ago

1.13.2

4 years ago

1.12.3

4 years ago

1.12.7

4 years ago

1.12.6

4 years ago

1.12.5

4 years ago

1.12.4

4 years ago

1.13.1

4 years ago

1.13.0

4 years ago

1.12.2

4 years ago

1.12.1

4 years ago

1.12.0

4 years ago

1.11.0

4 years ago

1.10.2

4 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

0.10.0

4 years ago

0.9.26

4 years ago

0.9.25

4 years ago

0.9.24

4 years ago

0.9.23

4 years ago

0.9.22

4 years ago

0.9.21

4 years ago

0.9.20

4 years ago

0.9.19

4 years ago

0.9.18

4 years ago

0.9.13

4 years ago

0.9.14

4 years ago

0.9.15

4 years ago

0.9.16

4 years ago

0.9.17

4 years ago

0.9.12

4 years ago

0.9.11

4 years ago

0.9.10

4 years ago

0.9.9

4 years ago

0.9.8

4 years ago

0.9.7

4 years ago

0.9.6

4 years ago

0.9.5

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

5 years ago

0.2.7

5 years ago

0.7.4

5 years ago

0.3.6

5 years ago

0.4.3

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.1

5 years ago

0.8.2

5 years ago

0.8.0

5 years ago

0.7.1-next.0

5 years ago

0.4.2

5 years ago

0.3.5

5 years ago

0.2.6

5 years ago

0.7.3

5 years ago

0.7.3-next.0

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.1-next.0

5 years ago

0.5.0

5 years ago

0.3.4

5 years ago

0.2.5

5 years ago

0.4.2-next.0

5 years ago

0.4.1

5 years ago

0.4.1-next.2

5 years ago

0.4.1-next.1

5 years ago

0.4.1-next.0

5 years ago

0.4.0

5 years ago

0.4.0-next.0

5 years ago

0.3.3

5 years ago

0.3.3-next.1

5 years ago

0.3.3-next.0

5 years ago

0.2.4

5 years ago

0.3.2

5 years ago

0.3.2-next.6

5 years ago

0.3.2-next.5

5 years ago

0.3.2-next.4

5 years ago

0.3.2-next.3

5 years ago

0.3.2-next.2

5 years ago

0.3.2-next.1

5 years ago

0.3.2-next.0

5 years ago

0.3.1

5 years ago

0.3.1-next.6

5 years ago

0.3.1-next.5

5 years ago

0.3.1-next.4

5 years ago

0.3.1-next.3

5 years ago

0.3.1-next.2

5 years ago

0.3.1-next.1

5 years ago

0.3.1-next.0

5 years ago

0.3.0

5 years ago

0.3.0-next.11

5 years ago

0.3.0-next.10

5 years ago

0.3.0-next.9

5 years ago

0.3.0-next.8

5 years ago

0.3.0-next.7

5 years ago

0.3.0-next.6

5 years ago

0.3.0-next.5

5 years ago

0.3.0-next.4

5 years ago

0.3.0-next.3

5 years ago

0.3.0-next.2

5 years ago

0.3.0-next.1

5 years ago

0.3.0-next.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.0.1

6 years ago