@@ -239,6 +239,18 @@ export function render(canvas: HTMLElement, renderInfo: RenderInfo) {
239239 dataset . accumulateValues ( ) ;
240240 }
241241 }
242+ // stack
243+ if ( renderInfo . stack ) {
244+ // Traverse the datasets, and add up the values from each dataset.
245+ let lastDataset = null ;
246+ for ( let dataset of renderInfo . datasets ) {
247+ if ( dataset . getQuery ( ) . usedAsXDataset ) continue ;
248+ if ( lastDataset ) {
249+ dataset . shiftByDataset ( lastDataset ) ;
250+ }
251+ lastDataset = dataset ;
252+ }
253+ }
242254
243255 for ( let lineInfo of renderInfo . line ) {
244256 let ret = renderLineChart ( canvas , renderInfo , lineInfo ) ;
@@ -905,16 +917,23 @@ function renderBar(
905917 // console.log(dataset);
906918 // console.log(barInfo);
907919 // console.log("%d/%d", currBarSet, totalNumOfBarSets);
908-
920+
909921 if ( ! renderInfo || ! barInfo ) return ;
910922
911923 let barGap = 1 ;
912924 let barSetWidth = renderInfo . dataAreaSize . width / dataset . getLength ( ) ;
913925 let barWidth = barSetWidth ;
926+ let currentDiaplayInd = currBarSet ;
927+ let totalDiaplaySet = totalNumOfBarSets ;
914928 if ( barSetWidth - barGap > 0 ) {
915929 barWidth = barSetWidth - barGap ;
916930 }
917- barWidth = barWidth / totalNumOfBarSets ;
931+ if ( ! renderInfo . stack ) {
932+ barWidth = barWidth / totalNumOfBarSets ;
933+ } else {
934+ currentDiaplayInd = 0 ;
935+ totalDiaplaySet = 1 ;
936+ }
918937
919938 let portionLeft = ( currBarSet + 1 ) / totalNumOfBarSets ;
920939
@@ -936,28 +955,28 @@ function renderBar(
936955 . append ( "rect" )
937956 . attr ( "x" , function ( p : DataPoint , i : number ) {
938957 if ( i === 0 ) {
939- let portionVisible = currBarSet + 1 - totalNumOfBarSets / 2.0 ;
958+ let portionVisible = currentDiaplayInd + 1 - totalDiaplaySet / 2.0 ;
940959 if ( portionVisible < 1.0 ) {
941960 return (
942961 chartElements . xScale ( p . date ) -
943962 barSetWidth / 2.0 +
944- currBarSet * barWidth +
963+ currentDiaplayInd * barWidth +
945964 portionVisible * barWidth
946965 ) ;
947966 }
948967 }
949968 return (
950969 chartElements . xScale ( p . date ) -
951970 barSetWidth / 2.0 +
952- currBarSet * barWidth
971+ currentDiaplayInd * barWidth
953972 ) ;
954973 } )
955974 . attr ( "y" , function ( p : DataPoint ) {
956975 return yScale ( Math . max ( p . value , 0 ) ) ;
957976 } )
958977 . attr ( "width" , function ( p : DataPoint , i : number ) {
959978 if ( i === 0 ) {
960- let portionVisible = currBarSet + 1 - totalNumOfBarSets / 2.0 ;
979+ let portionVisible = currentDiaplayInd + 1 - totalDiaplaySet / 2.0 ;
961980 if ( portionVisible < 0.0 ) {
962981 return 0.0 ;
963982 } else if ( portionVisible < 1.0 ) {
@@ -966,7 +985,7 @@ function renderBar(
966985 return barWidth ;
967986 } else if ( i === dataset . getLength ( ) - 1 ) {
968987 let portionVisible =
969- 1.0 - ( currBarSet + 1 - totalNumOfBarSets / 2.0 ) ;
988+ 1.0 - ( currentDiaplayInd + 1 - totalDiaplaySet / 2.0 ) ;
970989 if ( portionVisible < 0.0 ) {
971990 return 0.0 ;
972991 } else if ( portionVisible < 1.0 ) {
@@ -1626,15 +1645,30 @@ function renderBarChart(
16261645 let datasetOnLeftYAxis = [ ] ;
16271646 let datasetOnRightYAxis = [ ] ;
16281647 let xDatasetIds = renderInfo . datasets . getXDatasetIds ( ) ;
1629- for ( let ind = 0 ; ind < barInfo . yAxisLocation . length ; ind ++ ) {
1630- if ( xDatasetIds . includes ( ind ) ) continue ;
1631- let yAxisLocation = barInfo . yAxisLocation [ ind ] ;
1632- if ( yAxisLocation . toLowerCase ( ) === "left" ) {
1633- datasetOnLeftYAxis . push ( ind ) ;
1634- } else if ( yAxisLocation . toLocaleLowerCase ( ) === "right" ) {
1635- // right
1636- datasetOnRightYAxis . push ( ind ) ;
1648+ if ( renderInfo . stack ) {
1649+ for ( let ind = barInfo . yAxisLocation . length - 1 ; ind >= 0 ; ind -- ) {
1650+ if ( xDatasetIds . includes ( ind ) ) continue ;
1651+ let yAxisLocation = barInfo . yAxisLocation [ ind ] ;
1652+ if ( yAxisLocation . toLowerCase ( ) === "left" ) {
1653+ datasetOnLeftYAxis . push ( ind ) ;
1654+ } else if ( yAxisLocation . toLocaleLowerCase ( ) === "right" ) {
1655+ // right
1656+ datasetOnRightYAxis . push ( ind ) ;
1657+ }
16371658 }
1659+
1660+ } else {
1661+ for ( let ind = 0 ; ind < barInfo . yAxisLocation . length ; ind ++ ) {
1662+ if ( xDatasetIds . includes ( ind ) ) continue ;
1663+ let yAxisLocation = barInfo . yAxisLocation [ ind ] ;
1664+ if ( yAxisLocation . toLowerCase ( ) === "left" ) {
1665+ datasetOnLeftYAxis . push ( ind ) ;
1666+ } else if ( yAxisLocation . toLocaleLowerCase ( ) === "right" ) {
1667+ // right
1668+ datasetOnRightYAxis . push ( ind ) ;
1669+ }
1670+ }
1671+
16381672 }
16391673
16401674 let retRenderLeftYAxis = renderYAxis (
0 commit comments