0.0.8 • Published 1 year ago

wcs-chart-lib v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Wild Chart School

English Version Version Française Storybook

License Version Language Maintained with Node.js Maintained with npm Tests Contributions welcome Build Status Documentation

English Version

Welcome to your free open-source library for data visualization with React (utilizing PropTypes).

Wild Chart School is a project maintained by the community, contributions are welcome!

Visualize your data in 4 different ways (4 chart types currently: bars, lines, points and pie charts).

The graph is responsive and resizes automatically with the window (unless a specific size is specified).

It can be used as soon as it is installed with test data or by replacing it with its own, while retaining the structure given below.

Getting started with WildChartSchool

Installation

Download and install with npm with:

npm i wcs-chart-lib

Or clone this repository (you will be able to download the library with npm install chart-wcs)

  git clone git@github.com:WildCodeSchool-CDA-LYON-02-2024/wcs-chart-lib.git

Usage

The WCS component allows to display a Chart by passing different props (dataset, config, theme).

Implementation exemple code (ready-to-use)

import { WCS } from 'wcs-chart-lib';

function App() {
  const config = {
    type: 'point',
  };

  const dataset = [
    {
      tag: ['Titre1'],
      data: {
        labels: [
          'Janvier',
          'Fevrier',
          'Mars',
          'Avril',
          'Mai',
          'Juin',
          'Juillet',
          'Aout',
          'Septembre',
          'Octobre',
          'Novembre',
          'Decembre',
        ],
        values: [[10, 20, 30, 40, 50, 60, 51, 11, 51, 23, 47, 56]],
      },
    },
  ];

  return (
    <>
      <WCS config={config} dataset={dataset} theme="sea" legend="top" />
    </>
  );
}

export default App;

Theme

The chart has two default themes: "sea" and "nature".

Simply pass the value as a string in the component props.

Without any props, the chart will take "sea" as its default theme.

Customize your theme

You can pass an object to the theme props with the following values :

PropriétéTypeDescriptionExample
backGroundColorStringChange the background color of the canvas"yellow"
fillColorArray of stringChange the color of points and bars"blue","yellow"
strokeColorStringChange the color of points"blue"
gridColorChaine de caractèresChange the grid color"grey"
axiesColorChaine de caractèresChange X and Y axies"red"
adaptFontSizeYNumber(int) negative or positiveChange the size of numbers on the Y axis5
adaptFontSizeXNumber(int) negative or positiveChange the size of labels on the X axis-5

Data

Example of the dataset object, the structure does not change depending on the chart type :

const dataset = [
    {
      tag: ["Titre1"],
      data: {
        labels: [
          "Janvier",
          "Février",
          "Mars",
          "Avril",
          "Mai",
          "Juin",
          "Juillet",
          "Août",
          "Septembre",
          "Octobre",
          "Novembre",
          "Décembre",
        ],
        values: [[88, 100, 30, 40, 50, 60, 51, 11, 51, 23, 47, 56]],
      },
    },
  ];
PropretyTypeDescriptionExamplerequired
datasetArray of objectThe whole structureYes
tagArray of stringTitle of the chart"Title1","Title2"No
dataObjectContains labels and valueYes
labelsArray of stringRepresents the labels associated with the values"Janurary","February"Yes
valuesArray of array with number intThe values to display in the chart10,20,30Yes

Note : To display multiple charts, for comparisons for example, add another array.

If there are multiple arrays, the first one should be the longest, or it will cause bugs!

Example : [[10,20,30,40,70], [40,50,60]] [[40,50,60],[10,20,30,40,70]]

Chart type

The different chart types: bar, line, point, pie...

The chart type is defined in the config object and is mandatory for the component to function, even if it is empty. The chart type is defined with type (type string): if no value is supplied, it will default to “point”. Possible values: “bar”, “point”, “line”, “pie”.

The configuration examples config explain the properties specific to each type of graphic. If a property is present for one type of graphic and absent for another, it's simply not functional. type, height and width properties are common to all graphics.

Chart line

Example for the line chart :

  const config = {
    type: 'line',
    height: 1000,
    width: 1000,
  };
PropretyTypeDescriptionExampleRequiredBy default
typeStringDefines the chart type"line"Yes"point"
heightNumber(int)It's a percentage, it defines the height of the canvas50No100
widthNumber (int)It's a percentage, it defines the width of the canvas80No100

Line Screenshot

Chart point

Example for the point chart :

const config = {
  type: 'point',
  toLine: false,
  grid: true,
  radius: 5,
};
PropretyTypeDescriptionExampleBy default
toLineBooleanAllows you to connect the points with a linetruefalse
gridBooleanAllows you to remove the gridfalsetrue
radiusNumber (int)Sets the radius of the circle (point)102

Point Screenshot

Chart bar

Example for the bar chart :

const config = {
   type: 'bar',
   barWidth: 30,
 };
PropretyTypeDescriptionExampleBy default
barWidthNumber (int) (int)Allows you to define the width of the bar2015

Note: Currently, the bar chart can only accommodate two value tables. Beyond that, the result will not be as expected.

BarChart Screenshot

Chart pie

Example of a pie chart :

const config = {
   type: 'pie',
   radius: 100,
 };
PropriétéTypeDescriptionExempleBy defaut
radiusNumber (int)Defines the size of the pie chart100See below

By default, the radius of the pie chart is half the smaller of width or heigh, minus some spacing (so that the ends of the pie chart are not stuck to the window edges)

PieChart Screenshot

Example of legend display :

<WCS config={config} dataset={dataset} legend='top' />

Legend takes 4 possible parameters (in string):

  • "none" : Legend is not displayed,
  • "top": The legend is displayed at the top (note that if the pie chart has a radius greater than 150, it may overflow onto the legend),
  • "bottom": The legend is displayed at the bottom (note that if the pie chart has a radius greater than 150, it may overlap the legend),
  • "left": The legend is displayed on the left (do not use in charts other than pie chart, otherwise the axis labels will overlap),
  • "right": Legend is displayed on the right.

For greater clarity, we'll show you examples with the pie chart. For other charts, only the "top" parameter is relevant.

Without legend ("none") :

LEGEND 3 Screenshot

Legend at left ("left") :

LEGEND 1 Screenshot

Legend at right ("right") :

LEGEND 2 Screenshot

Legend on top ("top") :

LEGEND 4 Screenshot

Legend on bottom ("bottom") :

LEGEND 5 Screenshot

French Version

License Version Language Maintained with Node.js Maintained with npm Tests Contributions welcome Build Status Documentation

Bienvenue dans votre bibliothèque open source gratuite pour la visualisation des données avec React (utilisant PropTypes).

Wild Chart School est un projet maintenu par la communauté, les contributions sont les bienvenues !

Visualisez vos données de 4 manières différentes (4 types de graphiques actuellement : barres, lignes, points et à secteurs).

Le graphique est responsive et se redimensionne automatiquement avec la fenêtre (sauf si vous passez outre en donnant une taille définie).

Elle est utilisable dès son installation avec des données de test ou en remplaçant celles-ci par les siennes, tout en conservant la structure donnée ci-dessous.

Commencer avec WildChartSchool

Installer depuis npm :

npm i wcs-chart-lib

Ou cloner le repo :

https://github.com/WildCodeSchool-CDA-LYON-02-2024/wcs-chart-lib

Le composant WCS permet d’afficher un graphique en passant différentes props (dataset, config, thème)

Exemple d'implémentation (prêt à l'emploi) :

import { WCS } from 'wcs-chart-lib';

function App() {
 const config = {
   type: 'point',
 };

 const dataset = [
   {
     tag: ['Titre1'],
     data: {
       labels: [
         'Janvier',
         'Fevrier',
         'Mars',
         'Avril',
         'Mai',
         'Juin',
         'Juillet',
         'Aout',
         'Septembre',
         'Octobre',
         'Novembre',
         'Decembre',
       ],
       values: [[10, 20, 30, 40, 50, 60, 51, 11, 51, 23, 47, 56]],
     },
   },
 ];

 return (
   <>
     <WCS config={config} dataset={dataset} theme="sea" legend="top" />
   </>
 );
}

export default App;

Thème

Le graphique a deux thèmes par defaut : "sea" et "nature".

Passer simplement la valeur en chaine de caractères dans les props du composant.

Sans aucun props, le graphique prendra "sea" en thème par defaut.

Faire son thème personnaliser

Vous pouvez passer un objet au props theme avec les valeurs suivante :

PropriétéTypeDescriptionExemple
backGroundColorChaine de caractèresChange la couleur de fond du canvas"yellow"
fillColorTableau de chaine de caractèresChange la couleur des points et des barres"blue","yellow"
strokeColorChaine de caractèresChange la couleur du contour du point"blue"
gridColorChaine de caractèresChange la couleur du quadrillage"grey"
axiesColorChaine de caractèresChange la couleur de l'axe X et Y"red"
adaptFontSizeYNombre(int) négatif ou positifChange la taille des nombres sur l'axe Y5
adaptFontSizeXNombre(int) négatif ou positifChange la taille des labels sur l'axe X-5

Données

Exemple de l’objet dataset, la structure ne change pas en fonction du type de graphique :

const dataset = [
    {
      tag: ["Titre1"],
      data: {
        labels: [
          "Janvier",
          "Février",
          "Mars",
          "Avril",
          "Mai",
          "Juin",
          "Juillet",
          "Août",
          "Septembre",
          "Octobre",
          "Novembre",
          "Décembre",
        ],
        values: [[88, 100, 30, 40, 50, 60, 51, 11, 51, 23, 47, 56]],
      },
    },
  ];
PropriétéTypeDescriptionExempleRequis
datasetTableau d'objetsToute la structureOui
tagTableau de chaine de caractèresTitre du graphique"Titre1","Titre2"Non
dataObjetContient tous les labelsOui
labelsTableau de chaine de caractèresReprésente les labels associés aux valeurs"Janvier","Février"Oui
valuesTableau de tableau de nombres, intThe values to display in the chart10,20,30Oui

Note : pour afficher plusieurs graphiques, pour des comparaisons par exemple, ajouter un autre tableau.

Si plusieurs tableaux, le premier doit être le plus long, ou cela causera des bugs !

Exemple : [[10,20,30,40,70], [40,50,60]] [[40,50,60],[10,20,30,40,70]]

Types de graphiques

Les différents types de graphiques : barre, ligne, point, à secteur (camembert)...

Le type de graphique se définit dans l’objet config et il est obligatoire pour le fonctionnement du composant, même s' il est vide. Le type de graphique est a définir avec type (type string) : si pas de valeur fournis, il prendra “point” par défaut. Valeurs possibles : “bar”, “point”, “line”, “pie”

Dans les exemples de config ci dessous, sont expliquées les propriétés propres à chaque type de graphique, si une propriété est présente pour un type de graphique et absente pour un autre, elle n’est tout simplement pas fonctionnelle. Les propriétés type, height, width, sont communes à tous les graphiques.

Graphique en ligne

Exemple pour le graphique en ligne :

  const config = {
    type: 'line',
    height: 100,
    width: 100,
  };
PropriétéTypeDescriptionExempleRequisPar defaut
typeChaine de caractèresDéfinit le type de graphique"line"Oui"point"
heightNombre (int)C'est un pourcentage, il definit la hauteur du canvas50Non100
widthNombre (int)C'est un pourcentage, il définit la largeur du canvas80Non100

Line Screenshot

Graphique en points

Exemple pour le graphique en point:

  const config = {
    type: 'point',
    toLine: false,
    grid: true,
    radius: 5,
  };
PropriétéTypeDescriptionExempleDéfaut
toLineBooléenPermet de relier les points par une lignetruefalse
gridBooléenPermet d'enlever le quadrillagefalsetrue
radiusNombre (int)Définit le rayon du cercle (point)102

Point Screenshot

Graphique en barres

Exemple pour le graphique en secteur :

const config = {
  type: 'bar',
  barWidth: 30,
};
PropriétéTypeDescriptionExempleDéfaut
barWidthNombre (int)Permet de definir la largeur de la barre2015

Note : Pour l'instant, le graphique en barre ne peut accepter que deux tableaux de valeur. Au délà, le rendu ne sera pas celui attendu.

BarChart Screenshot

Graphique camembert

Exemple pour le graphique en camembert :

  const config = {
    type: 'pie',
    radius: 100,
  };
PropriétéTypeDescriptionExempleDéfaut
radiusNombre (int)Définit le taille du camembert100Voir ci-dessous

Par défaut, le rayon du camembert prend la moitié de la valeur la plus petite entre width et heigh, moins un espacement (pour que les extrémités du camembert ne soient pas collées aux bords de la fenêtre)

PieChart Screenshot

Le composant utilise l’API canvas pour dessiner le graphique, est responsive et se resize automatiquement par rapport à la taille de la fenêtre du navigateur.

Exemple pour l'affichage de la légende :

<WCS config={config} dataset={dataset} legend='top' />

Legend prend 4 paramètres possible (en string):

  • “none” : La légende ne s’affiche pas,
  • “top” : La légende s’affiche en haut (attention si pie chart a un radius supérieur à 150, cela peut déborder sur la légende),
  • “bottom” : La légende s’affiche en bas (attention si pie chart a un radius supérieur à 150, cela peut déborder sur la légende),
  • “left” : La légende s’affiche à gauche (ne pas utiliser dans les autres graphiques que pie chart sinon les labels de l’axe se chevauchent),
  • “right” : La légende s’affiche à droite

Pour plus de clarté, nous vous montrerons des exemples avec le camembert. Pour les autres graphiques, seul le paramètre “top” sera pertinent

Sans légrende ("none") :

LEGEND 3 Screenshot

Légende à gauche ("left"):

LEGEND 1 Screenshot

Légende à droite ("right"):

LEGEND 2 Screenshot

Légende en haut ("top"):

LEGEND 4 Screenshot

Légend en bas ("bottom") :

LEGEND 5 Screenshot

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago