logo polydile dile-components

dile-graph

Web Component to create a custom charts based on the library Chart.js

Installation

npm i @dile/dile-graph

Usage

Add the ChartJS library to your page, with your preferred way, por example:

<script src="https://cdn.jsdelivr.net/npm/chart.js@3.9.1/dist/chart.min.js"></script>

Is necessary to use Chart.js version 3 or higher. To use Chart.js version 2 you need to use the version 0.1.1 of @dile/dile-graph component.

Import the component your js module

import '@dile/dile-graph/dile-graph.js';

Use the component

<dile-graph 
    class="example"
    charttype="line" labels='["A", "B", "C", "D", "E", "F"]'
    datasets='[{ "label": "Dataset 1", "data": [65, 59, 80, 81, 56, 55], "borderColor": "rgb(255, 99, 132)", "backgroundColor": "rgb(177, 199, 232)"},{ "label": "Dataset 2", "data": [28, 48, 40, 19, 86, 27], "borderColor": "rgb(33, 150, 232)", "backgroundColor": "rgb(55, 199, 200)"}]'>
</dile-graph>

Set the graph values ​​with the following properties

PropertyDescriptionExamples
cartTypeChart type'line', 'bar'...
labelsHorizonal Labels['Jan', 'Feb', 'Mar']
datasetsData[ { "label": "Users", "data": [1, 4, 8], "backgroundColor": "rgba(255, 99, 132, 0.2)", "borderColor": "rgba(255,99,132,1)", "borderWidth": 2 } ]

Consult the Chart.js documentation for more information about datasets.

CSS Custom Properties

It is possible to use some CSS custom properties to customize the component.

Custom variableDescriptionDefault
--dile-graph-widthcard width100%
--dile-graph-background-colorcard background colorwhite
--dile-graph-border-radiuscard border radius0px
--dile-graph-shadowcard box shadow0 4px 8px 0 rgba(0, 0, 0, 0.2)
--dile-graph-hover-shadowcard hover box shadow0 8px 16px 0 rgba(0, 0, 0, 0.2)
--dile-graph-paddingcard padding10px

dile-graph demos

Default Graph

<dile-graph 
    charttype="bar" 
    labels='["Jan","Feb","Mar"]'
    datasets='[ { "label": "Users", "data": [1, 4, 6], "backgroundColor": "rgba(255, 99, 132, 0.2)", "borderColor": "rgba(255,99,132,1)", "borderWidth": 2 } ]'
></dile-graph>

Custom Graph

<style>
  .example {
      --dile-graph-width: 700px;
      --dile-graph-border-radius: 12px;
      --dile-graph-background-color: #ddf0ff;
      --dile-graph-width: 400px;
      --dile-graph-shadow: none;
      --dile-graph-hover-shadow: 0 0 10px rgba(40, 100, 200, 0.2);
  }
</style>
<dile-graph 
    class="example"
    charttype="line" labels='["A", "B", "C", "D", "E", "F"]'
    datasets='[{ "label": "Dataset 1", "data": [65, 59, 80, 81, 56, 55], "borderColor": "rgb(255, 99, 132)", "backgroundColor": "rgb(177, 199, 232)"},{ "label": "Dataset 2", "data": [28, 48, 40, 19, 86, 27], "borderColor": "rgb(33, 150, 232)", "backgroundColor": "rgb(55, 199, 200)"}]'>
</dile-graph>