cb-charts
v1.0.0
Published
Sparrow Marketing custom charts library.
Downloads
18
Readme
Sparrow Charts
Custom charts library for sparrow marketing
Why
The Sparrow Storybook was built with the purpose of holding tye design system required by the various projects under Sparrow Marketing. Example being Sparrow Frontend and Sparrow Report Generator
Architecture
Under the hood, Sparrow Storybook uses the following:
- Typescript
- React Storybook - For displaying the components
- Rollup - For bundling
Code quality control
- Eslint - Linting Typescript
- Stylelint - Linting CSS
- Prettier - Code formatting
- Husky & Lint staged - Precommit hook to execute commands and do fixes before the file is committed
Testing
- Jest(https://jestjs.io/) - Test runner
- React Testing Library(https://testing-library.com/docs/react-testing-library/intro)
Development
Usage
Depending on chart, it accepts different type of data and config. All config values can be found in documentation. Here are some examples how to create basic chart of each type:
Bar Chart
// array of labels for x axis
const labels = [
'Date 1',
'Date 2',
'Date 3',
'Date 4'
]
const data = {
labels,
datasets: [
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
}
]
}
<Chart type="bar" data={data} />
Multiple Bar Chart
// array of labels for x axis
const labels = [
'Date 1',
'Date 2',
'Date 3',
'Date 4'
]
const data = {
labels,
datasets: [
//pass multiple dataset objects
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
},
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
},
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
}
]
}
<Chart type="bar" data={data} />
Stacked Bar Chart
// array of labels for x axis
const labels = [
'Date 1',
'Date 2',
'Date 3',
'Date 4'
]
const data = {
labels,
datasets: [
//pass multiple dataset objects
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
},
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
},
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
}
]
}
// pass in the config.chart property stacked
<Chart type="bar" data={data} config={{chart: {stacked: true}}} />
Area Chart
// array of labels for x axis
const labels = [
'Date 1',
'Date 2',
'Date 3',
'Date 4'
]
const data = {
labels,
datasets: [
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
}
]
}
<Chart type="area" data={data} />
Multiple Area Chart
// array of labels for x axis
const labels = [
'Date 1',
'Date 2',
'Date 3',
'Date 4'
]
const data = {
labels,
datasets: [
//pass multiple dataset objects
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
},
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
},
{
label: 'Followers',
data: [1, 2, 3, 4],
backgroundColor: '#c12382',
}
]
}
<Chart type="area" data={data} />
Curve Chart
const data = {
// labels for boundary values
labels: ['Bellow average', 'Average', 'Above average'],
// boundary values
datasets: [0, 35, 70],
// value for point on curve chart
value: state
}
<Chart type="curve" data={data} />
Donut Chart
const data = {
datasets: [
{ label: 'Slice 1', data: 1, color: '#00c9dd' },
{ label: 'Slice 2', data: 2, color: '#1d9dee' },
{ label: 'Slice 3', data: 3, color: '#ff9600' }
]
}
<Chart type="new-donut" data={data} />
Diverging Chart
// array of labels for x axis
const labels = [
'Strongly disagree',
'Disagree',
'Not Sure',
'Agree',
'Strongly agree'
]
const data = {
labels,
datasets: [
{
total: 282 // optional value to provide total value for chart by x axis
label: 'Question 1',
data: [1,2,3,4,5],
backgroundColor: ['#FF5860', '#FFCDCF', '#BEC4CD', '#C2EEC2', '#33C635']
}
]
}
<Chart type="diverging" data={data} />
Polar Area Chart
const labels = ['Slice 1','Slice 2','Slice 3','Slice 4','Slice 5']
const data = {
labels,
datasets: {
data: [1,2,3,4,5],
backgroundColor: ['red', 'green', 'blue', 'purple', 'orange']
}
}
<Chart type="polar-area" data={data} />
Prerequisites
NodeJS & NPM - Use NVM as mentioned here
Developement
To start the storybook development server, execute npm run dev
To build storybook, execute npm run build-storybook
Build
To build the component library, execute npm run build
. This would use rollup under the hood to build the component library
Working
The storybook internally uses the prepare
script from npm which helps us to build the package in the target repository once it is being updated/installed there. Hence every time a change happens and something new is added to storybook,
Release management
In order to be used effectively in other projects relying on storybook, we found it necessary to have a release management process. Each time a new change is merged to master branch, the developer can release the changes by checking out to a new release branch from the master branch by executing git checkout -b release/<version_no>
and then pushing it to upstream using git push -u origin release/<version_no>
. This can then be used in other projects by using: "storybook": "git+https://sparrow+storybook-deploy-token:<token>@gitlab.com/sparrow-marketing/storybook.git#release/<version_no></version_no>"
. Please make sure the version number is always incremented.
Integration with other projects
Since we dont have a private NPM registry at the moment, we rely on building the storybook at the installation target. This is made possible by using the npm prepare
script which gets executed every time storybook is installed in a different project as a dependency. We are currently facing issues with the same when doing installations in gitlab and fixing this is a work in progress.