Gathering detailed insights and metrics for mivl-chartjs-2
Gathering detailed insights and metrics for mivl-chartjs-2
Gathering detailed insights and metrics for mivl-chartjs-2
Gathering detailed insights and metrics for mivl-chartjs-2
npm install mivl-chartjs-2
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
223 Commits
12 Branches
1 Contributors
Updated on 28 Jan 2023
JavaScript (97.42%)
HTML (2.58%)
Cumulative downloads
Total Downloads
Last day
-88.9%
1
Compared to previous day
Last week
-95.7%
2
Compared to previous week
Last month
575%
54
Compared to previous month
Last year
4.4%
215
Compared to previous year
2
44
React wrapper for Chart.js 2 Open for PRs and contributions!
As of 2.x we have made chart.js a peer dependency for greater flexibility. Please add chart.js as a dependency on your project to use 2.x. Currently, 2.5.x is the recommended version of chart.js to use.
Live demo: reactchartjs.github.io/react-chartjs-2
To build the examples locally, run:
1npm install 2npm start
Then open localhost:8000
in a browser.
We have to build the package, then you can run storybook.
1npm run build 2npm run storybook
Then open localhost:6006
in a browser.
1npm install --save react-chartjs-2 chart.js
1yarn add react-chartjs-2 chart.js
Check example/src/components/* for usage.
1import { Doughnut } from 'react-chartjs-2'; 2 3<Doughnut data={...} />
In order for Chart.js to obey the custom size you need to set maintainAspectRatio
to false, example:
1<Bar 2 data={data} 3 width={100} 4 height={50} 5 options={{ maintainAspectRatio: false }} 6/>
Chart.js instance can be accessed by placing a ref to the element as:
1class MyComponent extends React.Component { 2 constructor(props) { 3 super(props); 4 this.chartReference = React.createRef(); 5 } 6 7 componentDidMount() { 8 console.log(this.chartReference); // returns a Chart.js instance reference 9 } 10 11 render() { 12 return (<Doughnut ref={this.chartReference} data={data} options={options} />) 13 } 14}
Canvas node and hence context, that can be used to create CanvasGradient background, is passed as argument to data if given as function:
This approach is useful when you want to keep your components pure.
1render() { 2 const data = (canvas) => { 3 const ctx = canvas.getContext("2d") 4 const gradient = ctx.createLinearGradient(0,0,100,0); 5 ... 6 return { 7 ... 8 backgroundColor: gradient 9 ... 10 } 11 } 12 13 return (<Line data={data} />) 14}
Chart.js defaults can be set by importing the defaults
object:
1import { defaults } from 'react-chartjs-2'; 2 3// Disable animating charts by default. 4defaults.global.animation = false;
If you want to bulk set properties, try using the lodash.merge function. This function will do a deep recursive merge preserving previously set values that you don't want to update.
1import { defaults } from 'react-chartjs-2'; 2import merge from 'lodash.merge'; 3// or 4// import { merge } from 'lodash'; 5 6merge(defaults, { 7 global: { 8 animation: false, 9 line: { 10 borderColor: '#F85F73', 11 }, 12 }, 13});
You can access the internal Chart.js object to register plugins or extend charts like this:
1import { Chart } from 'react-chartjs-2'; 2 3componentWillMount() { 4 Chart.pluginService.register({ 5 afterDraw: function (chart, easing) { 6 // Plugin code. 7 } 8 }); 9}
If you're using Chart.js 2.6 and below, add the showLines: false
property to your chart options. This was later added in the default config, so users of later versions would not need to do this extra step.
A function to be called when mouse clicked on chart elememts, will return all element at that point as an array. Check
1{ 2 onElementsClick: (elems) => {}, 3 getElementsAtEvent: (elems) => {}, 4 // `elems` is an array of chartElements 5} 6
Calling getElementAtEvent(event) on your Chart instance passing an argument of an event, or jQuery event, will return the single element at the event position. If there are multiple items within range, only the first is returned Check
1{ 2 getElementAtEvent: (elems) => {}, 3 // => returns the first element at the event point. 4}
Looks for the element under the event point, then returns all elements from that dataset. This is used internally for 'dataset' mode highlighting Check
1{ 2 getDatasetAtEvent: (dataset) => {} 3 // `dataset` is an array of chartElements 4}
You will find that any event which causes the chart to re-render, such as hover tooltips, etc., will cause the first dataset to be copied over to other datasets, causing your lines and bars to merge together. This is because to track changes in the dataset series, the library needs a key
to be specified - if none is found, it can't tell the difference between the datasets while updating. To get around this issue, you can take these two approaches:
label
property on each dataset. By default, this library uses the label
property as the key to distinguish datasets.datasetKeyProvider
prop to your chart component, which would return a unique string value for each dataset.src
, lib
and the build process)NOTE: The source code for the component is in src
. A transpiled CommonJS version (generated with Babel) is available in lib
for use with node.js, browserify and webpack. A UMD bundle is also built to dist
, which can be included without the need for any build system.
To build, watch and serve the examples (which will also watch the component source), run npm start
. If you just want to watch changes to src
and rebuild lib
, run npm run watch
(this is useful if you are working with npm link
).
MIT Licensed Copyright (c) 2017 Jeremy Ayerst
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
project is archived
Details
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
122 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More