# Legend (`chartist-plugin-legend`, Chartist plugin)

> Adds a legend from the series names. Click an entry to hide or show its series.

It ships no stylesheet; its README has the CSS, as in the example.

- Author: Kees Kluskens
- Source: https://github.com/CodeYellowBV/chartist-plugin-legend, npm: https://www.npmjs.com/package/chartist-plugin-legend
- License: ISC
- Written for the Chartist 0.x API; runs on v2 with `chartist/compat` (a script that reads the global `Chartist`, for backwards compatibility)
- Charts: line, bar, pie
- Server rendering: no, it needs a browser
- Downloads: 29053 a month, last release 2017-08-05

Setup:

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/chartist@2.0.0-rc.2/dist/index.min.css">
<script type="module">
  import 'https://cdn.jsdelivr.net/npm/chartist@2.0.0-rc.2/dist/compat.min.js';
</script>
<script defer src="https://cdn.jsdelivr.net/npm/chartist-plugin-legend@0.6.2/chartist-plugin-legend.js"></script>
<script type="module">
  import { lineChart } from 'https://cdn.jsdelivr.net/npm/chartist@2.0.0-rc.2/dist/index.min.js';
  import { fromLegacyPlugin } from 'https://cdn.jsdelivr.net/npm/chartist@2.0.0-rc.2/dist/compat.min.js';

  lineChart('#chart', data, {
    plugins: [fromLegacyPlugin(Chartist.plugins.legend())]
  });
</script>
```

Example (`src/main.ts`):

```ts
import 'chartist/styles.css';
import './style.css';
import { lineChart } from 'chartist';
import { fromLegacyPlugin } from 'chartist/compat';

// Plugin scripts register on the global `Chartist`.
const { plugins } = Chartist;

lineChart(
  '#chart',
  {
    labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
    series: [
      { name: 'Tea', data: [5, 6, 4, 7, 6] },
      { name: 'Coffee', data: [8, 9, 7, 9, 10] },
      { name: 'Cocoa', data: [2, 1, 3, 2, 4] }
    ]
  },
  {
    plugins: [
      fromLegacyPlugin(
        // An element of its own, so the legend doesn't overlap the chart.
        plugins.legend({ position: document.querySelector('#legend') })
      )
    ]
  }
);
```

All plugins: https://chartist.dev/plugins.md
