npm package: @geins/ralph-module-chart-js
Description: Chart.js module for Geins PWA Storefront Ralph
Author: arvidsson-geins
Latest version: 1.0.1

[![NPM Package][npm]][npm-url]
[![NPM Downloads][npm-downloads-per-month]][npm-trends]
![Geins][mit-shield]

[![Start Geins Free Trial][geins-tiral-img]][geins-tiral-url] [![Geins Docs][geins-docs-img]][geins-docs-url]


[![geins repo banner](https://raw.githubusercontent.com/geins-io/resources/master/images/banners/repos/chart-js.jpg)](https://www.geins.io)

# Chart.js module for Geins PWA Storefront Ralph

A module for Geins PWA Storefront Ralph that adds [Chart.js](https://www.chartjs.org/) charts to your storefront.

## Pre-requisites

- Geins Account and PWA Storefront Ralph. [Get a free trial here](https://www.geins.io)

## Description

This module enables you to add Chart.js charts to your storefront in seconds.

Module can be used in any page of your storefront and is configuerd to work with Geins Cart out-of-box. Module can be added via Geins CMS or any CMS of your choice.

## Installation

### 1. Install the module

```bash
npm i @geins/ralph-module-chart-js
```

### 2. Add the module to your Geins PWA Storefront Ralph

Add the module to your Geins PWA Storefront Ralph by adding the following line to your `nuxt.config.js` file:

```js
// nuxt.config.js
...
  modules: [
    [
        '@geins/ralph-module-chart-js',
        {
            enabled: true,
            debug: true
        }
    ]
  ],
```


## To use with Geins CMS (no-code)

#### 1. Add the module to your Geins PWA Storefront Ralph
Use the [@geins/ralph-module-cms-json-container](https://www.npmjs.com/package/@geins/ralph-module-cms-json-container)

```bash
npm i @geins/ralph-module-cms-json-container
```

#### 2. Add the module to your Geins PWA Storefront Ralph

Add module to your `nuxt.config.json` file:
```js
...
    modules: [
      '@geins/ralph-module-cms-json-container'
    ]
..
``` 
Set the `widgetRenderTypesComponents` in your `nuxt.config.json` file to use the `GeinsWidgetJsonContainer` component for the `JSON` widget type.
```js
// nuxt.config.js
...
  publicRuntimeConfig: {
      widgetRenderTypesComponents: {
        JSON: 'GeinsWidgetJsonContainer'
      },
  }
...
```

## Module Options

Add extra options to module configuration in `nuxt.config.js` file.
| Parameter | Type | Default | Example |
|-|-|-|
| enabled | String | `true` | Enables the module|
| debug | String | `false` | Enables debug info to console |
| chartOptions | Object |  {} | For chart-customization, add your own options object here [Chart.js docs](https://www.chartjs.org/docs) |
| chartPlugins | Array | [] | If available, add an arrray of your chart-plugins here [Chart.js docs](https://www.chartjs.org/docs) |

## Usage

Add component to desired page of your storefront. Either by cms or as a component. 

## Component

## Components
Module adds two components to your storefront. `GeinsChartJs` and `GeinsWidgetChartJs`.

### GeinsChartJs
This component is used to show the streamify player. It is used by the `GeinsWidgetChartJs` component. You can use it directly in your page if you want to add the player directly to your page.

### Properties

The properties of the component are one to one with the [Chart.js API](https://www.chartjs.org/docs/latest). You can add any property to the component and it will be passed to the API.

| Property        | Type    | Default Value | Required | Description                                                                                        |
| --------------- | ------- | ------------- | -------- | -------------------------------------------------------------------------------------------------- |
| type            | String  | 'line'    | No      | Type of chart |
| data            | Object  | -         | Yes      | Dataset and labels for your chart  |
| options         | Object  | default Chart.js options  | No      | Chart options |
| plugins      | Array   | []         | No      | Chart plugins|

### Use with Geins CMS

Add a `JSON Widget` to your page in the [Geins CMS](https://docs.geins.io/docs/launchpads/web/content). Add the following JSON to your widget (replace with your own dataset and labels). Customize the values according to your own dataset and preferences.

Example of JSON widget content:
```json
{
  "renderWidget": "GeinsWidgetChartJs",
  "data": {
    "data": {
        "labels": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
        "datasets": [{
            "label": "Sunlight hours per month",
            "data": [197, 216, 232, 244, 248, 290, 288, 274, 268, 246, 212, 182]
        }]
    }
  }
}
```
## Customizing Styling of chart to match your storefront

To add global customization to your charts, create a folder inside your static-folder called `chartJs`. Inside this create a file called `chartOptions.js`. 
Inside this file, add your custom options (see below example):

```js
// chartOptions.js

export const chartOptions = {
    responsive: true,
    maintainAspectRatio: false,
    scales: {
      y: {
        border: {
          display: false
        },
        grid: {
          display: false,
          drawTicks: false
        }
      },
      x: {
        border: {
          display: false
        },
        grid: {
          display: false,
          drawTicks: false
        }
      }
    }
}
```
See docs for [Chart.js options](https://www.chartjs.org/docs/latest/api/interfaces/LineOptions.html) for more options.

Import your `chartOptions.js` file into your `nuxt.config.js`:
```js
import { chartOptions } from './static/chartJs/chartOptions';
```
Add the chartOptions to your module options:
```js
      [
        '@geins/ralph-module-chart-js',
        {
          enabled: true,
          chartOptions: chartOptions,
        }
      ]
```

### As a component or with any CMS

Add the component to your page. Add the type as properties to the component.

```vue
<GeinsChartJs 
    :type="chartType"
    :data="chartData"
    :options="chartOptions"
    :plugins="chartPlugins"
/>
```

### Tips!
Add a chart to you PDP to display [product meta data](https://docs.geins.io/docs/geins/pim/product#parameters) in a intuitive way.

[npm]: https://img.shields.io/npm/v/@geins/ralph-module-chart-js
[npm-url]: https://www.npmjs.com/package/@geins/ralph-module-chart-js
[npm-downloads-per-month]: https://img.shields.io/npm/dm/@geins/ralph-module-chart-js.svg
[npm-trends]: https://npmtrends.com/@geins/ralph-module-chart-js
[geins-docs-url]: https://docs.geins.io
[geins-docs-img]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/geins-io/resources/master/sheilds/geins-docs-read-v3.json
[geins-tiral-url]: https://www.geins.io
[geins-tiral-img]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/geins-io/resources/master/sheilds/geins-fee-tiral.json
[mit-shield]: https://img.shields.io/badge/license-MIT-green
[mit-url]: https://en.wikipedia.org/wiki/MIT_License