Table of Contents
npm install -g react-cli react
Step 1.2 To install the create-react-app modules for React globally, execute the command given below on the command line: npm install -g create-react-app
Step 1.3.1 From the cmd shell, navigate to the directory where you want to save your project. Step 1.3.2 To create the project skeleton, execute the following command on the command line: create-react-app my-app
Step 1.4 To install FusionCharts on your system, execute the command given below: npm install fusioncharts --save
Step 1.5 To install the React-FusionCharts plugin, execute the command given below: npm install react-fusioncharts --save
app.js
file: import fusioncharts from 'fusioncharts'; import charts from 'fusioncharts/fusioncharts.charts'; import ReactFC from 'react-fusioncharts'; import theme from 'fusioncharts/themes/fusioncharts.theme.fint';To pass these files as dependencies, use the code snippet given below:
charts(fusioncharts) theme(fusioncharts)
function grabUsers(query) { const promises = []; let i = 1; for(; i<=12; i++) { const promise = fetch("https://api.github.com/graphql", { method: "POST", async: false, headers: { "Authorization": 'bearer d23e97911c0554fe8e1e13010cea0e7e85580781', "Content-Type": 'application/json' }, body: JSON.stringify({ query: ` { search(type: USER, query: "created:<2017-${i < 10 ? ('0' + i) : i + ''}-01 ${query}") { userCount } } ` }) }) .then((r) => r.json()) promises.push(promise); } return Promise.all(promises); }Click here to read more about the JavaScript Fetch API.
function generateChartConfig(data,chartAttr,chartType){ const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; const chartData = data.map((d, i) => { return { label: monthNames[i], value: d.data.search.userCount }; }); const dataSource = { "chart": chartAttr, "data": chartData }; var revenueChartConfigs = { type: chartType, width:800, height: 400, dataFormat: 'json', dataSource: dataSource }; return revenueChartConfigs; }
class App extends Component { constructor(props) { super(props); this.state = { msg: "Loading....." }; render() { return ( // code to be written ); } } }Next, to load the chart data from the methods defined above, we will call the
componentDidMount()
method from the component life cycle using the code given below: componentDidMount(){ grabUsers("") .then((data) => { var chartConfig= generateChartConfig(data,{ "caption":"GitHub Users - 2017", "captionfontsize":"20", "theme":"fint", "logoURL":"https://19386-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2017/10/github-logo-1.png", "logoAlpha": "80", "logoScale": "8", "canvastopmargin":"75", "logoPosition": "TR", "logoleftmargin":"-20", "logotopmargin":"10", "yaxisminvalue":"12000000" },"column2d"); ReactDOM.unmountComponentAtNode(document.getElementById('chart-viewer')); ReactDOM.render(<ReactFC {...chartConfig} />, document.getElementById('chart-viewer')); //ReactDOM.render(<radio/> , document.getElementById('radio')); }) .catch((err) => { console.log(err); this.setState({ msg: String(err) }); }); }Click here to know more about the
componentDidMount()
method: Finally, to render the chart, we will create the chart container inside the render()
method using the <div>
element as shown in the code below: render() { return ( <div> <div id="chart-viewer"> {this.state.msg} </div> ); }
render() { return ( <div> <div id="chart-viewer"> {this.state.msg} </div> <br /> <div id="radio" onChange= {this.setlang.bind(this)}> <label id="container">Java <input type="radio" value="java" name="lang" /> <span id="checkmark"></span> </label> <label id="container">C <input type="radio" value="C" name="lang" /> <span id="checkmark"></span> </label> <label id="container">C++ <input type="radio" value="cplus" name="lang" /> <span id="checkmark"></span> </label> <label id="container">JavaScript <input type="radio" value="javascript" name="lang" /> <span id="checkmark"></span> </label> <label id="container">All <input type="radio" value="all" name="lang" defaultChecked="true"/> <span id="checkmark"></span> </label> </div> </div> ); }The above code will render simple radio buttons. You can customize the radio buttons’ cosmetics according to your requirements using an external CSS file.
setlang(event) { console.log(event.target.value); if(event.target.value==="java"){ console.log("Hi I am on Java"); grabUsers("language:java") .then((data) => { var chartConfig= generateChartConfig(data,{ "caption":"Java Users - 2017", "captionfontsize":"20", "theme":"fint", "logoURL":"https://19386-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2017/10/github-logo-1.png", "logoAlpha": "80", "logoScale": "8", "canvastopmargin":"75", "logoPosition": "TR", "logoleftmargin":"-20", "logotopmargin":"10", "yaxisminvalue":"1000000" },"column2d"); ReactDOM.unmountComponentAtNode(document.getElementById('chart-viewer')); ReactDOM.render(<ReactFC {...chartConfig} />, document.getElementById('chart-viewer')); //ReactDOM.render(<radio/> , document.getElementById('radio')); }) .catch((err) => { console.log(err); this.setState({ msg: String(err) }); }); } else if (event.target.value==="C"){ console.log("Hi I am on C"); grabUsers("language:C") .then((data) => { var chartConfig= generateChartConfig(data,{ "caption":"C Users - 2017", "captionfontsize":"20", "theme":"fint", "logoURL":"https://19386-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2017/10/github-logo-1.png", "logoAlpha": "80", "logoScale": "8", "canvastopmargin":"75", // "valueFontColor":"#000000", "logoPosition": "TR", "logoleftmargin":"-20", "logotopmargin":"10", "yaxisminvalue":"245000", "plotfillalpha":"90", },"column2d"); ReactDOM.unmountComponentAtNode(document.getElementById('chart-viewer')); ReactDOM.render(<ReactFC {...chartConfig} />, document.getElementById('chart-viewer')); //ReactDOM.render(<radio/> , document.getElementById('radio')); }) .catch((err) => { console.log(err); this.setState({ msg: String(err) }); }); } else if(event.target.value==="cplus"){ console.log("Hi I am on C++") grabUsers("language:C++") .then((data) => { var chartConfig= generateChartConfig(data,{ "caption":"C++ Users - 2017", "captionfontsize":"20", "theme":"fint", "logoURL":"https://19386-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2017/10/github-logo-1.png", "logoAlpha": "80", "logoScale": "8", "canvastopmargin":"75", // "valueFontColor":"#000000", "logoPosition": "TR", "logoleftmargin":"-20", "logotopmargin":"10", "yaxisminvalue":"350000" },"column2d"); ReactDOM.unmountComponentAtNode(document.getElementById('chart-viewer')); ReactDOM.render(<ReactFC {...chartConfig} />, document.getElementById('chart-viewer')); //ReactDOM.render(<radio/> , document.getElementById('radio')); }) .catch((err) => { console.log(err); this.setState({ msg: String(err) }); }); } else if (event.target.value==="javascript"){ console.log("Hi I am on JavaScript") grabUsers("language:JavaScript") .then((data) => { var chartConfig= generateChartConfig(data,{ "caption":"JavaScript Users - 2017", "captionfontsize":"20", "subCaption":"Of 2017", "theme":"fint", "logoURL":"https://19386-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2017/10/github-logo-1.png", "logoAlpha": "80", "logoScale": "8", "canvastopmargin":"75", // "valueFontColor":"#000000", "logoPosition": "TR", "logoleftmargin":"-20", "logotopmargin":"10", "yaxisminvalue":"1200000" },"column2d"); ReactDOM.unmountComponentAtNode(document.getElementById('chart-viewer')); ReactDOM.render(<ReactFC {...chartConfig} />, document.getElementById('chart-viewer')); //ReactDOM.render(<radio/> , document.getElementById('radio')); }) .catch((err) => { console.log(err); this.setState({ msg: String(err) }); }); } else{ grabUsers("") .then((data) => { var chartConfig= generateChartConfig(data,{ "caption":"GitHub Users - 2017", "captionfontsize":"20", "theme":"fint", "logoURL":"https://19386-presscdn-pagely.netdna-ssl.com/wp-content/uploads/2017/10/github-logo-1.png", "logoAlpha": "80", "logoScale": "8", "canvastopmargin":"75", "logoPosition": "TR", "logoleftmargin":"-20", "logotopmargin":"10", "yaxisminvalue":"12000000" },"column2d"); ReactDOM.unmountComponentAtNode(document.getElementById('chart-viewer')); ReactDOM.render(<ReactFC {...chartConfig} />, document.getElementById('chart-viewer')); //ReactDOM.render(<radio/> , document.getElementById('radio')); }) .catch((err) => { console.log(err); this.setState({ msg: String(err) }); }); } }And that’s it! The dashboard is complete.
npm start
. Doing this will start the server. If everything has been done as explained in the steps above, then this is how your dashboard will render. You can try the live example here. We’re excited to announce the upcoming release of FusionCharts v4.1—a groundbreaking step forward in the…
Have you ever been overwhelmed by a massive data set and wondered, "How do I…
If you’ve ever tried to make sense of the stock market, you’ve probably come across…
Imagine you’re comparing the sales performance of your top product lines across different regions, or…
Have you ever spent hours buried in documentation, hunting for a specific piece of code?…
Do you feel like your data is a cryptic puzzle, locked away from revealing its…