Skip to content

Instantly share code, notes, and snippets.

@joebordes
Created May 15, 2020 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebordes/790b7c1e1d852e9b2323e309b446d8bd to your computer and use it in GitHub Desktop.
Save joebordes/790b7c1e1d852e9b2323e309b446d8bd to your computer and use it in GitHub Desktop.
react chartjs corebos question

Example and steps to show a coreBOS Business Question of type chartjs in a react application

yarn add react-chartjs-2 chart.js corebos-ws-lib

tested with

"chart.js": "^2.9.3",
"react-chartjs-2": "^2.9.0",

adapt and deploy the code in the files index.js and pie.js

cbqchartreact1

cbqchartreact2

related to https://gist.github.com/joebordes/7a42f37a26610ab91891c0f3360451e8

Name Text Question No Type Status SQL Query Collection Module Page Size Assigned To Created By Created Time Modified Time Columns Condition Description Order by Column Group by Column Type Properties Main Table Alias Unique ID Field Materialized View Cron Map Id Materialized View Workflow Conditions in Filter Format CRM Entity Alias Update View when Related Changes Related Module List Unique Identifier
Contacts per Country cbQ-0000008 Pie Active 0 Contacts 10.00 admin admin 2020-04-19 16:27:14 2020-05-14 22:20:39 [{"fieldname":"mailingcountry","operation":"is","value":"mailingcountry","valuetype":"fieldname","joincondition":"custom","groupid":0,"groupjoin":"mailingcountry"},{"fieldname":"countres","operation":"is","value":"count(contact_no)","valuetype":"expression","joincondition":"count","groupid":0,"groupjoin":"contact_no"}] [{"fieldname":"mailingcountry","operation":"contains","value":"S","valuetype":"rawtext","joincondition":"and","groupid":"0","groupjoin":""},{"fieldname":"mailingcity","operation":"contains","value":"s","valuetype":"rawtext","joincondition":"and","groupid":"0","groupjoin":""}] mailingcountry ASC,countres DESC mailingcountry {"key_label":"mailingcountry","key_value":"countres"} 0 0 0 0 0eb53c50e479b8475b3155433b114398fa6733b6
import React, { Component } from 'react'
import { connect } from 'react-redux'
import RCPie from './pie';
import MainLayout from 'Hoc/MainLayout'
import H1 from 'Components/H1'
class Home extends Component {
static propTypes = {}
render() {
return (
<MainLayout>
<H1>This is a secondary page</H1>
<textarea
rows="4"
onChange={(e) => this.handleChange(e.target.value)}
/>
<RCPie></RCPie>
</MainLayout>
)
}
}
const mapStateToProps = state => ({})
const mapDispatchToProps = dispatch => ({})
export default connect(mapStateToProps, mapDispatchToProps)(Home)
import React, { Component } from 'react'
import Chart from 'chart.js';
import {Pie} from 'react-chartjs-2';
import * as cbconn from 'corebos-ws-lib/WSClientm';
const apiUrl = 'http://localhost/coreBOSwork';
cbconn.setURL(apiUrl);
class RCPie extends Component {
constructor(props) {
super(props);
this.state = {
data : {
labels: [
'Red',
'Blue',
'Yellow',
'Green',
],
datasets: [{
data: [300, 50, 100],
backgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56',
'green'
]
}]
}
};
this.getGraphData();
}
getGraphData = async () => {
await cbconn.doLogin('admin', 'admin', true);
let dt = await cbconn.doInvoke('cbQuestionAnswer', { qid: '48x47035', params: [] }, 'GET');
/*
data.answer array with row values
data.title
*/
let ps = JSON.parse(dt.properties);
let lbls = [];
let dsets = [];
for (let row=0; row<dt.answer.length; row++) {
lbls.push(dt.answer[row][ps.key_label]);
dsets.push(dt.answer[row][ps.key_value]);
}
this.setState({ data: {
labels: lbls,
datasets: [{data: dsets}]
}});
};
render() {
return (
<div>
<h2>Pie Example</h2>
<Pie id="cbchart" data={this.state.data} width={400} height={200} options={{ maintainAspectRatio: false }} />
</div>
);
}
};
export default RCPie;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment