Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kubk/use-amcharts

React hook for using Amcharts 4 library
https://github.com/kubk/use-amcharts

Last synced: 25 days ago
JSON representation

React hook for using Amcharts 4 library

Awesome Lists containing this project

README

        

# use-amcharts

React hook for working with Amcharts 4 library. Supports TypeScript type-inference and automatically destroys on component unmount.

### Installation

```
npm i use-amcharts
```

### Usage
```tsx
export const Chart = () => {
const { amchartsElRef } = useAmcharts(am4charts.XYChart, (chart) => {
// Configure chart...
});

return

;
};
```

### Runnable example

```tsx
let data = [];
let visits = 10;

for (let i = 1; i < 366; i++) {
visits += Math.round((Math.random() < 0.5 ? 1 : -1) * Math.random() * 10);
data.push({ date: new Date(2018, 0, i), name: "name" + i, value: visits });
}

export const Chart = () => {
const { amchartsElRef } = useAmcharts(am4charts.XYChart, (chart) => {
chart.data = data;

const dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.grid.template.location = 0;

const valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
valueAxis.renderer.minWidth = 35;

const series = chart.series.push(new am4charts.LineSeries());
series.dataFields.dateX = "date";
series.dataFields.valueY = "value";
series.tooltipText = "{valueY.value}";
});

return

;
};
```

### Storybook

To run Storybook example:
- `git clone`
- `npm install`
- `npm run storybook`