# Hover (`chartist-hover`, Chartist plugin)

> Calls your functions when the pointer enters or leaves a point, so you can show the value anywhere you like.

- Author: Pier-Luc Gendreau
- Source: https://github.com/Zertz/chartist-hover, npm: https://www.npmjs.com/package/chartist-hover
- License: MIT
- 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: 9844 a month, last release 2018-05-15

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-hover@1.0.5/chartist-hover.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.hover())]
  });
</script>
```

Example (`src/main.ts`):

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

// Plugin scripts register on the global `Chartist`.
const { plugins } = Chartist;
const caption = document.querySelector('#caption')!;

lineChart(
  '#chart',
  {
    labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    series: [[1, 5, 3, 4, 6, 2, 3]]
  },
  {
    height: 220,
    plugins: [
      fromLegacyPlugin(
        plugins.hover({
          // The plugin passes the event, with the hovered point as target.
          onMouseEnter: (event: { target: Element }) => {
            caption.textContent = `Value: ${event.target.getAttribute('ct:value')}`;
          },
          onMouseLeave: () => {
            caption.textContent = 'Hover the chart.';
          }
        })
      )
    ]
  }
);
```

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