| 1 |
/** |
| 2 |
* External dependencies |
| 3 |
*/ |
| 4 |
import { Chart } from 'react-google-charts'; |
| 5 |
|
| 6 |
import merge from 'merge'; |
| 7 |
|
| 8 |
import { compact, formatDate, isValidJSON } from '../utils.js'; |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress dependencies |
| 12 |
*/ |
| 13 |
const { startCase } = lodash; |
| 14 |
|
| 15 |
const { __ } = wp.i18n; |
| 16 |
|
| 17 |
const { |
| 18 |
Component, |
| 19 |
Fragment |
| 20 |
} = wp.element; |
| 21 |
|
| 22 |
const { |
| 23 |
Button, |
| 24 |
Dashicon, |
| 25 |
Toolbar, |
| 26 |
Tooltip |
| 27 |
} = wp.components; |
| 28 |
|
| 29 |
const { BlockControls } = wp.editor; |
| 30 |
|
| 31 |
class ChartRender extends Component { |
| 32 |
constructor() { |
| 33 |
super( ...arguments ); |
| 34 |
} |
| 35 |
|
| 36 |
render() { |
| 37 |
|
| 38 |
let chart; |
| 39 |
|
| 40 |
let data = formatDate( JSON.parse( JSON.stringify( this.props.chart ) ) ); |
| 41 |
|
| 42 |
if ( 0 <= [ 'gauge', 'table', 'timeline' ].indexOf( this.props.chart['visualizer-chart-type']) ) { |
| 43 |
chart = startCase( this.props.chart['visualizer-chart-type']); |
| 44 |
} else { |
| 45 |
chart = `${ startCase( this.props.chart['visualizer-chart-type']) }Chart`; |
| 46 |
} |
| 47 |
|
| 48 |
return ( |
| 49 |
<div className={ this.props.className }> |
| 50 |
|
| 51 |
{ ( null !== this.props.chart ) && |
| 52 |
<Fragment> |
| 53 |
|
| 54 |
<BlockControls key="toolbar-controls"> |
| 55 |
<Toolbar |
| 56 |
className='components-toolbar' |
| 57 |
> |
| 58 |
<Tooltip text={ __( 'Edit Chart' ) }> |
| 59 |
<Button |
| 60 |
className="components-icon-button components-toolbar__control edit-pie-chart" |
| 61 |
onClick={ this.props.editChart } |
| 62 |
> |
| 63 |
<Dashicon icon={ 'edit' } /> |
| 64 |
</Button> |
| 65 |
</Tooltip> |
| 66 |
</Toolbar> |
| 67 |
</BlockControls> |
| 68 |
|
| 69 |
<Chart |
| 70 |
chartType={ chart } |
| 71 |
rows={ data['visualizer-data'] } |
| 72 |
columns={ data['visualizer-series'] } |
| 73 |
options={ |
| 74 |
isValidJSON( this.props.chart['visualizer-settings'].manual ) ? |
| 75 |
merge( compact( this.props.chart['visualizer-settings']), JSON.parse( this.props.chart['visualizer-settings'].manual ) ) : |
| 76 |
compact( this.props.chart['visualizer-settings']) |
| 77 |
} |
| 78 |
height="500px" |
| 79 |
/> |
| 80 |
|
| 81 |
</Fragment> |
| 82 |
} |
| 83 |
|
| 84 |
</div> |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
export default ChartRender; |
| 90 |
|