@@ -6,6 +6,7 @@ import React, {PropTypes} from 'react';
66import ReactHighcharts from 'react-highcharts' ;
77import numeral from 'numeral' ;
88import { get , set } from 'lodash' ;
9+ import shallowequal from 'shallowequal' ;
910import { getFormatString } from '../../util/MathUtil.js' ;
1011import { logError } from '../../util/WebUtil.js' ;
1112
@@ -51,6 +52,9 @@ export class Histogram extends React.Component {
5152 * @param {number } props.width - width of the chart in pixels
5253 * @param {number } props.height - height of the chart in pixels
5354 * @param {string } [props.logs] - can have values 'x', 'y', or 'xy'
55+ * @param {Object } [props.xAxis] - Highcharts xAxis properties
56+ * @param {Object } [props.yAxis] = Highcharts yAxis properties
57+ * @param {string } [props.opposite] - can have values 'x', 'y', or 'xy'
5458 * @param {string } [props.binColor='#d1d1d1'] - darker bin color
5559 * @param {string } props.desc - description
5660 * @public
@@ -63,41 +67,39 @@ export class Histogram extends React.Component {
6367 }
6468
6569 shouldComponentUpdate ( nextProps ) {
66- const { series, data, width, height, logs, reversed , desc, binColor} = this . props ;
67- // should rerender only if data or bin color has changed
70+ const { series, data, width, height, logs, xAxis , yAxis , desc, binColor} = this . props ;
71+ // should rerender if data, logs, or bin color has changed
6872 // otherwise just change the existing chart
69- if ( series !== nextProps . series || data !== nextProps . data || binColor !== nextProps . binColor ) { return true ; }
73+ if ( this . error || series !== nextProps . series || data !== nextProps . data || logs !== nextProps . logs || binColor !== nextProps . binColor ) { return true ; }
7074 const chart = this . refs . chart && this . refs . chart . getChart ( ) ;
7175 if ( chart ) {
72- let doUpdate = false ;
73- if ( height !== nextProps . height || width !== nextProps . width ) {
74- chart . setSize ( nextProps . width , nextProps . height , false ) ;
75- return false ;
76- }
76+ try {
77+ let doUpdate = false ;
78+ if ( height !== nextProps . height || width !== nextProps . width ) {
79+ chart . setSize ( nextProps . width , nextProps . height , false ) ;
80+ }
7781
78- if ( desc !== nextProps . desc ) {
79- chart . xAxis [ 0 ] . setTitle ( nextProps . desc , false ) ;
80- doUpdate = true ;
81- }
82- const nreversed = nextProps . reversed ;
83- if ( reversed !== nreversed ) {
84- const yReversed = Boolean ( nreversed && nreversed . indexOf ( 'y' ) > - 1 ) ;
85- const xReversed = Boolean ( nreversed && nreversed . indexOf ( 'x' ) > - 1 ) ;
86- chart . xAxis [ 0 ] . update ( { reversed : xReversed , opposite : yReversed } , false ) ;
87- chart . yAxis [ 0 ] . update ( { reversed : yReversed } , false ) ;
88- doUpdate = true ;
89- }
90- const nlogs = nextProps . logs ;
91- if ( logs !== nextProps . logs ) {
92- const xtype = nlogs && nlogs . indexOf ( 'x' ) > - 1 ? 'logarithmic' : 'linear' ;
93- const ytype = nlogs && nlogs . indexOf ( 'y' ) > - 1 ? 'logarithmic' : 'linear' ;
94- chart . xAxis [ 0 ] . update ( { type : xtype } , false ) ;
95- chart . yAxis [ 0 ] . update ( { type : ytype } , false ) ;
96- doUpdate = true ;
82+ if ( desc !== nextProps . desc ) {
83+ chart . xAxis [ 0 ] . setTitle ( nextProps . desc , false ) ;
84+ doUpdate = true ;
85+ }
86+
87+ if ( ! shallowequal ( xAxis , nextProps . xAxis ) ) {
88+ chart . xAxis [ 0 ] . update ( nextProps . xAxis , false ) ;
89+ doUpdate = true ;
90+ }
91+ if ( ! shallowequal ( yAxis , nextProps . yAxis ) ) {
92+ chart . yAxis [ 0 ] . update ( nextProps . yAxis , false ) ;
93+ doUpdate = true ;
94+ }
95+
96+ if ( doUpdate ) { chart . redraw ( false ) ; }
97+ } catch ( error ) {
98+ this . error = error ;
99+ chart . showLoading ( error ) ;
97100 }
98- if ( doUpdate ) { chart . redraw ( false ) ; }
99- return false ;
100101 }
102+ return false ;
101103 }
102104
103105 /*
@@ -326,14 +328,13 @@ export class Histogram extends React.Component {
326328 }
327329
328330 render ( ) {
331+ this . error = undefined ;
329332
330- const { binColor, data, desc, width, height, logs, reversed } = this . props ;
333+ const { binColor, data, desc, width, height, logs, xAxis , yAxis } = this . props ;
331334 let series = this . props . series ;
332335 if ( ! series ) {
333336 series = [ { data, binColor, name : 'data points' } ] ;
334337 }
335- const yReversed = ( reversed && reversed . indexOf ( 'y' ) > - 1 ? true : false ) ;
336-
337338
338339 let minY = 0 ;
339340 // what should be the minimum y value be?
@@ -415,17 +416,15 @@ export class Histogram extends React.Component {
415416 }
416417 } ,
417418 series : [ ] ,
418- xAxis : {
419+ xAxis : Object . assign ( {
419420 lineColor : '#999' ,
420421 tickColor : '#ccc' ,
421422 title : {
422423 text : desc
423424 } ,
424- opposite : yReversed ,
425- reversed : ( reversed && reversed . indexOf ( 'x' ) > - 1 ? true : false ) ,
426425 type : ( logs && logs . indexOf ( 'x' ) > - 1 ? 'logarithmic' : 'linear' )
427- } ,
428- yAxis : {
426+ } , xAxis ) ,
427+ yAxis : Object . assign ( {
429428 gridLineColor : '#e9e9e9' ,
430429 tickWidth : 1 ,
431430 tickLength : 3 ,
@@ -436,9 +435,8 @@ export class Histogram extends React.Component {
436435 title : {
437436 text : ''
438437 } ,
439- reversed : yReversed ,
440438 type : ( logs && logs . indexOf ( 'y' ) > - 1 ? 'logarithmic' : 'linear' )
441- } ,
439+ } , yAxis ) ,
442440 credits : {
443441 enabled : false // removes a reference to Highcharts.com from the chart
444442 }
@@ -459,17 +457,20 @@ export class Histogram extends React.Component {
459457
460458Histogram . defaultProps = {
461459 desc : 'Sample Distribution' ,
462- binColor : '#d1d1d1'
460+ binColor : '#d1d1d1' ,
461+ xAxis : { } ,
462+ yAxis : { }
463463} ;
464464
465465
466466// when more than one histogram is defined use series
467467Histogram . propTypes = {
468468 series : PropTypes . arrayOf ( PropTypes . object ) , // array of objects with data, binColor, and name properties
469+ xAxis : PropTypes . object ,
470+ yAxis : PropTypes . object ,
469471 width : PropTypes . number ,
470472 height : PropTypes . number ,
471473 logs : PropTypes . oneOf ( [ 'x' , 'y' , 'xy' ] ) ,
472- reversed : PropTypes . oneOf ( [ 'x' , 'y' , 'xy' ] ) ,
473474 desc : PropTypes . string ,
474475 data : PropTypes . arrayOf ( PropTypes . arrayOf ( PropTypes . number ) ) , // array of numbers [0] - nInBin, [1] - binMin, [2] - binMax
475476 binColor ( props , propName , componentName ) {
0 commit comments