@@ -29,10 +29,19 @@ import {
2929} from 'ko/bindings/charts/chartUtils' ;
3030import huePubSub from 'utils/huePubSub' ;
3131
32+ const getChartType = options => {
33+ if ( typeof options ?. type === 'function' ) {
34+ const typeResult = options . type ( ) ;
35+ return Array . isArray ( typeResult ) ? typeResult [ 0 ] : typeResult ;
36+ } else if ( typeof console . warn !== 'undefined' ) {
37+ console . warn ( 'Expected chart options type to be a function' ) ;
38+ }
39+ } ;
40+
3241ko . bindingHandlers . barChart = {
3342 init : function ( element , valueAccessor ) {
3443 const _options = ko . unwrap ( valueAccessor ( ) ) ;
35- if ( _options . type && _options . type ( ) === 'line' ) {
44+ if ( getChartType ( _options ) === 'line' ) {
3645 window . setTimeout ( ( ) => {
3746 lineChartBuilder ( element , valueAccessor ( ) , false ) ;
3847 } , 0 ) ;
@@ -46,11 +55,16 @@ ko.bindingHandlers.barChart = {
4655 } ,
4756 update : function ( element , valueAccessor ) {
4857 const _options = ko . unwrap ( valueAccessor ( ) ) ;
49- if ( _options . type && _options . type ( ) !== $ ( element ) . data ( 'type' ) ) {
58+ const chosenChartType = getChartType ( _options ) ;
59+ const renderedChartType = $ ( element ) . data ( 'type' ) ;
60+ const chartTypeHasChanged = chosenChartType !== renderedChartType ;
61+ const chartNotBuilt = ! $ ( element ) . data ( 'chart' ) ;
62+
63+ if ( chartTypeHasChanged || chartNotBuilt ) {
5064 if ( $ ( element ) . find ( 'svg' ) . length > 0 ) {
5165 $ ( element ) . find ( 'svg' ) . remove ( ) ;
5266 }
53- if ( _options . type ( ) === 'line' ) {
67+ if ( chosenChartType === 'line' ) {
5468 window . setTimeout ( ( ) => {
5569 lineChartBuilder ( element , valueAccessor ( ) , false ) ;
5670 } , 0 ) ;
@@ -59,7 +73,7 @@ ko.bindingHandlers.barChart = {
5973 barChartBuilder ( element , valueAccessor ( ) , false ) ;
6074 } , 0 ) ;
6175 }
62- $ ( element ) . data ( 'type' , _options . type ( ) ) ;
76+ $ ( element ) . data ( 'type' , chosenChartType ) ;
6377 }
6478 const _datum = _options . transformer ( _options . datum ) ;
6579 const _chart = $ ( element ) . data ( 'chart' ) ;
0 commit comments