React UI Components Documentation
ReactJS Chart Component
The Chart component for ReactJS represents a lightweight and powerful chart widget. It offers many advanced features and supports three different rendering technologies - SVG, HTML5 Canvas & VML.
Prerequisites
Refer to ReactJS Getting Started before you start with this help topic.
Configuration
The Chart component for ReactJS requires the following imports.
import React from 'react';import ReactDOM from 'react-dom'; import JqxChart from 'jqwidgets-react/react_jqxchart.js';
Then we create our component class. Properties and methods are put as ReactJS props.
class App extends React.Component { render () { return ( <JqxChart style={{ width: 850, height: 500 }} showLegend={true} enableAnimations={true} source={dataAdapter} xAxis={xAxis} valueAxis={valueAxis} seriesGroups={seriesGroups} /> ) }}
Finally we render our class in the desired HTML element:
ReactDOM.render(<App />, document.getElementById('app'));
Events Methods & Properties
In order to bind to any event, change any property or call any method, we need a reference to the component.
For that we use the ReactJS "ref" Callback Attribute:
<JqxChart ref='myChart' style={{ width: 850, height: 500 }}..... />
Now, when we have a reference, we need to call the desired event/property/method.
This is done in the componentDidMount() ReactJS Component Lifecycle method.
class App extends React.Component { componentDidMount() { //your logic } render () { return ( .... ) }};
Events
The click event is raised when the user clicks on series element.
The following example demonstrates how to add an event listener.
componentDidMount (){ this.refs.myChart.on('click', (event) => { //your logic }); }
Methods & Properties
This is how you call methods & props:
Every component have a method setOptions which accepts a object as an argument. This object contains component settings.
this.refs.myChart.setOptions({ colorScheme: 'scheme07', showLegend: 'true', xAxis: { gridLines: { visible: false } }})
Every component also have a method getOptions which returns a object containing the component settings.
let options = this.refs.myChart.getOptions();
Chart Examples
Overview
The following example demonstrates how to create a Chart component.
import React from 'react';import ReactDOM from 'react-dom'; import JqxChart from 'jqwidgets-react/react_jqxchart.js'; class App extends React.Component { render () { let source = { datatype: "csv", datafields: [ { name: 'Date' }, { name: 'S&P 500' }, { name: 'NASDAQ' } ], url: 'sampledata/nasdaq_vs_sp500.txt' }; let dataAdapter = new $.jqx.dataAdapter(source, { async: false, autoBind: true, loadError: (xhr, status, error) => { alert('Error loading "' + source.url + '" : ' + error); } }); let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; let padding = { left: 10, top: 5, right: 10, bottom: 5 }; let titlePadding = { left: 50, top: 0, right: 0, bottom: 10 }; let xAxis = { dataField: 'Date', formatFunction: function (value) { return value.getDate() + '-' + months[value.getMonth()] + '-' + value.getFullYear(); }, type: 'date', baseUnit: 'month', valuesOnTicks: true, minValue: '01-01-2014', maxValue: '01-01-2015', tickMarks: { visible: true, interval: 1, color: '#BCBCBC' }, unitInterval: 1, gridLines: { visible: true, interval: 3, color: '#BCBCBC' }, labels: { angle: -45, rotationPoint: 'topright', offset: { x: 0, y: -25 } } }; let valueAxis = { visible: true, title: { text: 'Daily Closing Price<br>' }, tickMarks: { color: '#BCBCBC' } }; let seriesGroups = [ { type: 'line', series: [ { dataField: 'S&P 500', displayText: 'S&P 500' }, { dataField: 'NASDAQ', displayText: 'NASDAQ' } ] } ]; return ( <JqxChart style={{ width:850, height:500 }} title={"U.S. Stock Market Index Performance"} description={"NASDAQ Composite compared to S&P 500"} showLegend={true} enableAnimations={true} padding={padding} titlePadding={titlePadding} source={dataAdapter} xAxis={xAxis} valueAxis={valueAxis} colorScheme={'scheme04'} seriesGroups={seriesGroups} /> ) }} ReactDOM.render(<App />, document.getElementById('app'));
Set Chart Columns with Local Data
The following example demonstrates the Chart bind to Local Data.
Donut Series
The following example demonstrates how to create a Chart component for ReactJS with two series of donut type (one inner and another one outer).