@@ -10,6 +10,7 @@ const dfs: { [key: string]: CSV } = {};
1010
1111let series_counter = 0 ;
1212let default_series : HTMLElement = null ;
13+ let max_plots = 1 ;
1314
1415let first_file = true ;
1516let first_trace = true ;
@@ -97,6 +98,7 @@ const addSeries = () => {
9798 buttonOff ( plot_4_button ) ;
9899 otherOff ( ) ;
99100
101+ series . setAttribute ( 'prev_plot_num' , '0' ) ;
100102 series . setAttribute ( 'plot_num' , plot . toString ( ) ) ;
101103
102104 if ( plot == 1 ) {
@@ -110,8 +112,6 @@ const addSeries = () => {
110112 } else {
111113 otherOn ( ) ;
112114 }
113-
114- updateAxes ( ) ;
115115 }
116116
117117 const plots : Array < number > = [ ] ;
@@ -126,24 +126,29 @@ const addSeries = () => {
126126
127127 plot_1_button . addEventListener ( 'click' , ( ) => {
128128 setPlot ( 1 ) ;
129+ updateAxes ( ) ;
129130 } )
130131
131132 plot_2_button . addEventListener ( 'click' , ( ) => {
132133 setPlot ( 2 ) ;
134+ updateAxes ( ) ;
133135 } )
134136
135137 plot_3_button . addEventListener ( 'click' , ( ) => {
136138 setPlot ( 3 ) ;
139+ updateAxes ( ) ;
137140 } )
138141
139142 plot_4_button . addEventListener ( 'click' , ( ) => {
140143 setPlot ( 4 ) ;
144+ updateAxes ( ) ;
141145 } )
142146
143147 const other_event = ( ) => {
144148 const plot_num = intMin1 ( parseInt ( plot_other . value ) ) ;
145149 plot_other . value = plot_num . toString ( ) ;
146150 setPlot ( plot_num ) ;
151+ updateAxes ( ) ;
147152 }
148153
149154 plot_other . addEventListener ( 'click' , other_event ) ;
@@ -233,7 +238,7 @@ const removeSeries = (series: HTMLElement) => {
233238 const oldIndex = parseInt ( otherSeries . getAttribute ( 'index' ) ) ;
234239 if ( oldIndex > index ) {
235240 otherSeries . setAttribute ( 'index' , ( oldIndex - 1 ) . toString ( ) ) ;
236- updateTrace ( otherSeries , false ) ;
241+ updateTrace ( otherSeries ) ;
237242 }
238243 }
239244
@@ -363,7 +368,7 @@ const intMin1 = (val: number) => {
363368 return val ;
364369}
365370
366- const updateTrace = ( series : HTMLElement , update_axes : boolean = true ) => {
371+ const updateTrace = ( series : HTMLElement ) => {
367372 const file = ( series . querySelector ( '.file-select' ) as HTMLSelectElement ) . value ;
368373 const x_label = ( series . querySelector ( '.x-select' ) as HTMLSelectElement ) . value ;
369374 const y_label = ( series . querySelector ( '.y-select' ) as HTMLSelectElement ) . value ;
@@ -418,10 +423,6 @@ const updateTrace = (series: HTMLElement, update_axes: boolean = true) => {
418423
419424 const index = parseInt ( series . getAttribute ( 'index' ) ) ;
420425 Plotly . restyle ( 'plot' , trace , index ) ;
421-
422- if ( update_axes ) {
423- updateAxes ( ) ;
424- }
425426} ;
426427
427428const updateMarkers = ( series : HTMLElement ) => {
@@ -449,23 +450,35 @@ const updateAxes = () => {
449450
450451 for ( const series of document . querySelectorAll ( '.series' ) as NodeListOf < HTMLElement > ) {
451452 const index = parseInt ( series . getAttribute ( 'index' ) ) ;
453+ const prev_plot_num = parseInt ( series . getAttribute ( 'prev_plot_num' ) ) ;
452454 const plot_num = parseInt ( series . getAttribute ( 'plot_num' ) ) ;
453455 plots . push ( plot_num ) ;
454- Plotly . restyle ( 'plot' , { yaxis : 'y' + plot_num } , index ) ;
456+
457+ if ( prev_plot_num != plot_num ) {
458+ console . log ( "restyle" ) ;
459+ Plotly . restyle ( 'plot' , { yaxis : 'y' + plot_num } , index ) ;
460+ series . setAttribute ( 'prev_plot_num' , plot_num . toString ( ) ) ;
461+ }
455462 }
456463
464+ const num_plots = Math . max ( 1 , Math . max ( ...plots ) ) ;
465+
457466 let layout : Partial < Plotly . Layout > = {
458467 grid : {
459- rows : Math . max ( ... plots ) ,
468+ rows : num_plots ,
460469 columns : 1 ,
461470 }
462471 }
463472
464- for ( const plot of plots ) {
473+ const new_max_plots = Math . max ( max_plots , Math . max ( ...plots ) ) ;
474+
475+ for ( let i = max_plots + 1 ; i <= new_max_plots ; i += 1 ) {
465476 // @ts -ignore
466- layout [ 'yaxis' + plot ] = { title : '' , automargin : true } ;
477+ layout [ 'yaxis' + i ] = { title : '' , automargin : true } ;
467478 }
468479
480+ max_plots = new_max_plots ;
481+
469482 Plotly . relayout ( 'plot' , layout ) ;
470483} ;
471484
0 commit comments