{"id":1287,"date":"2019-03-05T12:24:48","date_gmt":"2019-03-05T06:54:48","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1287"},"modified":"2019-03-05T12:36:57","modified_gmt":"2019-03-05T07:06:57","slug":"add-chart-js-graphs-react-application","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/","title":{"rendered":"Easiest Process to Add Chart JS graphs to a React application"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>This blog is all about <strong>Chart JS<\/strong> to be implemented in a <strong>React<\/strong> application. Before we start, we assume that we have a new or existing React application with us. Now open <strong>Terminal<\/strong> (for <em>Ubuntu\/Mac<\/em> users) or <strong>Command Prompt<\/strong> (for <em>Windows<\/em> users) and go to the project folder.<\/p>\n<p>&nbsp;<\/p>\n<p>Now we have to install the<strong> Chart Js<\/strong> plugin via <strong>Node Package Manager (NPM)<\/strong>. Type<\/p>\n<pre>npm install react-chartjs-2 chart.js --save<\/pre>\n<p>We have now <strong>react-chartjs-2<\/strong> plugin installed in our application as well as added to <strong>package.json<\/strong> file. The next thing to do is we have to call the Graph modules within the component we are about to use. Suppose we have <strong>App.js<\/strong> within our application.<\/p>\n<p>&nbsp;<\/p>\n<h3>Add Vertical Bar Graph<\/h3>\n<pre>import React from 'react';\r\nimport {Bar} from 'react-chartjs-2'; \/\/import Bar module from react-chart-js-2\r\n\r\nconst mydata = {\r\n  labels: ['Sunday', 'Monday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],\r\n  datasets: [\r\n    {\r\n      label: 'Week Dataset',\r\n      backgroundColor: 'rgba(255,99,132,0.2)',\r\n      borderColor: 'rgba(255,99,132,1)',\r\n      borderWidth: 1,\r\n      hoverBackgroundColor: 'rgba(255,99,132,0.4)',\r\n      hoverBorderColor: 'rgba(255,99,132,1)',\r\n      data: [65, 59, 80, 81, 56, 55, 40]\r\n    }\r\n  ]\r\n};\r\n\r\nexport default React.createClass({\r\n  displayName: 'Bar Example',\r\n\r\n  render() {\r\n    return (\r\n      &lt;div&gt;\r\n        &lt;h2&gt;Vertical Bar Example (custom size)&lt;\/h2&gt;\r\n        &lt;Bar\r\n          data={mydata}\r\n          width={100}\r\n          height={50}\r\n          options={{\r\n            maintainAspectRatio: false\r\n          }}\r\n        \/&gt;\r\n      &lt;\/div&gt;\r\n    );\r\n  }\r\n});<\/pre>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<h3>Add Horizontal Bar Graph<\/h3>\n<div>\n<pre>import React from 'react';\r\nimport { HorizontalBar } from 'react-chartjs-2';\u00a0\/\/import Horizontal Bar module from react-chart-js-2\r\n\r\nconst mydata = { \r\n    ...\r\n    ... \r\n};\r\n\r\nexport default React.createClass({ \r\n    displayName: 'Vertical Bar Example', \r\n    render() { \r\n        return ( \r\n            &lt;div&gt; \r\n                &lt;h2&gt;Horizontal Bar Example (custom size)&lt;\/h2&gt; \r\n                &lt;HorizontalBar \r\n                   data={mydata} \r\n                   width={100} \r\n                   height={50} \r\n                   options={{ maintainAspectRatio: false }} \r\n                \/&gt; \r\n            &lt;\/div&gt; \r\n        ); \r\n    } \r\n});<\/pre>\n<\/div>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<h3>Add Line Graph<\/h3>\n<pre>import React from 'react';\r\nimport {Line} from 'react-chartjs-2'; \/\/import Line module from react-chart-js-2\r\n\r\nconst mydata = {\r\n    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],\r\n    datasets: [\r\n        {\r\n        label: 'My First dataset',\r\n        fill: false,\r\n        lineTension: 0.1,\r\n        backgroundColor: 'rgba(75,192,192,0.4)',\r\n        borderColor: 'rgba(75,192,192,1)',\r\n        borderCapStyle: 'butt',\r\n        borderDash: [],\r\n        borderDashOffset: 0.0,\r\n        borderJoinStyle: 'miter',\r\n        pointBorderColor: 'rgba(75,192,192,1)',\r\n        pointBackgroundColor: '#fff',\r\n        pointBorderWidth: 1,\r\n        pointHoverRadius: 5,\r\n        pointHoverBackgroundColor: 'rgba(75,192,192,1)',\r\n        pointHoverBorderColor: 'rgba(220,220,220,1)',\r\n        pointHoverBorderWidth: 2,\r\n        pointRadius: 1,\r\n        pointHitRadius: 10,\r\n        data: [65, 59, 80, 81, 56, 55, 40]\r\n        }\r\n    ]\r\n};\r\n\r\nexport default React.createClass({\r\n    displayName: 'Line Chart Example',\r\n\r\n    render() {\r\n        return (\r\n           &lt;div&gt;\r\n           &lt;h2&gt;Line Chart Example&lt;\/h2&gt;\r\n           &lt;Line data={mydata} \/&gt;\r\n           &lt;\/div&gt;\r\n        );\r\n    }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<h3>Add Pie Chart<\/h3>\n<pre>import React from 'react';\r\nimport {Pie} from 'react-chartjs-2'; \/\/import Pie module from react-chart-js-2\r\n\r\nconst mydata = {\r\n    labels: [\r\n        'Red',\r\n        'Green',\r\n        'Yellow'\r\n    ],\r\n    datasets: [{\r\n        data: [300, 50, 100],\r\n        backgroundColor: [\r\n            '#FF6384',\r\n            '#36A2EB',\r\n            '#FFCE56'\r\n        ],\r\n        hoverBackgroundColor: [\r\n            '#FF6384',\r\n            '#36A2EB',\r\n            '#FFCE56'\r\n       ]\r\n   }]\r\n};\r\n\r\nexport default React.createClass({\r\n   displayName: 'Pie Example',\r\n\r\n   render() {\r\n       return (\r\n           &lt;div&gt;\r\n           &lt;h2&gt;Pie Example&lt;\/h2&gt;\r\n           &lt;Pie data={mydata} \/&gt;\r\n           &lt;\/div&gt;\r\n       );\r\n    }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>These are the general Chart Js graphs which are using regularly in a web application. You can find more about the charts in the React Chart Js official site.\u00a0<a href=\"https:\/\/github.com\/jerairrest\/react-chartjs-2\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/jerairrest\/react-chartjs-2<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; This blog is all about Chart JS to be implemented in a React application. Before we start, we assume that we have a new or existing React application with us. Now open Terminal (for Ubuntu\/Mac users) or Command Prompt (for Windows users) and go to the project folder. &nbsp; Now we have to install [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":1283,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72,18,5],"tags":[73],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Easiest Process to Add Chart JS graphs to a React application<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Easiest Process to Add Chart JS graphs to a React application\" \/>\n<meta property=\"og:description\" content=\"&nbsp; This blog is all about Chart JS to be implemented in a React application. Before we start, we assume that we have a new or existing React application with us. Now open Terminal (for Ubuntu\/Mac users) or Command Prompt (for Windows users) and go to the project folder. &nbsp; Now we have to install [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-05T06:54:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-05T07:06:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/02\/statistics-3338107_1920.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1357\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/02\/statistics-3338107_1920.jpg\",\"width\":1920,\"height\":1357},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/\",\"name\":\"Easiest Process to Add Chart JS graphs to a React application\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/#primaryimage\"},\"datePublished\":\"2019-03-05T06:54:48+00:00\",\"dateModified\":\"2019-03-05T07:06:57+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/615090f954d8460cf6186cf920b47398\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/add-chart-js-graphs-react-application\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/615090f954d8460cf6186cf920b47398\",\"name\":\"Preetam Mallick\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ed836329485e177373e4ff5323b6670a?s=96&r=g\",\"caption\":\"Preetam Mallick\"},\"description\":\"Frontend developer at Openweb Solutions\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1287"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1287"}],"version-history":[{"count":5,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1287\/revisions"}],"predecessor-version":[{"id":1319,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1287\/revisions\/1319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1283"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}