How can I use reactjs to create a morris.js chart? I am using an Asp.net MVC5 project with react.js library. It is my first react project, and I want to change a morris.js chart when some button clicked. I don't want to use other react library just morris js librayr inside react js script
Asked
Active
Viewed 2,910 times
2 Answers
4
Here's how you can make Morris.js work with React.
- Install Morris.js and its dependency - Raphael.js:
npm install --save-dev morris.js
npm install --save-dev raphael
- In your component:
import Raphael from 'raphael';
import 'morris.js/morris.css';
import 'morris.js/morris.js';
- Morris looks for Raphael to be in global scope
constructor() {
super();
window.Raphael = Raphael;
}
Important notes
If you get a compilation error for
morris.css
, please read this.If you write
import Morris from 'morris.js/morris.js'
, it won't work.There's another way to make Raphael be in global scope.
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.ProvidePlugin({
Raphael: 'raphael'
})
],
...
}
If you prefer this variant, you don't need:
import Raphael from 'raphael';
window.Raphael = Raphael;

Arsen K.
- 5,494
- 7
- 38
- 51
-
tried this solution but I get a "ReferenceError: jQuery is not defined", i don't know why. I have installed jQuery because I also have boostrap. – Mr_LinDowsMac Jul 17 '20 at 06:08
-
I would make sure jQuery is called before Raphael and Morris scripts. – Arsen K. Jul 18 '20 at 07:18
-
Done. But after trying to use it like in the answer provided by @novasaint, I got an error – Mr_LinDowsMac Jul 20 '20 at 00:33
1
Simple solution
In componentDidMount()
draw your chart, in my example it's a donut:
yourChart = Morris.Donut({
element: 'elementId',
data: data,
// ...other options
});
where yourChart
is declared outside of the class or you can do this.yourChart
in the constructor()
.
If you want to update/redraw your chart, you can call yourChart.setData(newData)
at the button click.

novasaint
- 700
- 8
- 13
-
Thank you for your answer I found this framework on internet: http://recharts.org/ and it worked well for me – Aggounix Mar 16 '17 at 13:38
-
Now I get a `TypeError: morris_js_morris_js__WEBPACK_IMPORTED_MODULE_3___default.a.Area is not a function` – Mr_LinDowsMac Jul 20 '20 at 00:33