private void computeChartSteps ( ) { //if special type if ( chartType == GraphPanelChart . CHART_PERCENTAGE ) { minYVal = 0 ; maxYVal = 100 ; return ; } //try to find the best range... //first, avoid special cases where maxY equal or close to minY if ( maxYVal - minYVal < 0.1 ) { maxYVal = minYVal + 1 ; } //real step double step = ( maxYVal - minYVal ) / gridLinesCount ; int pow = - 1 ; double factor = - 1 ; boolean found = false ; double testStep ; //find a step close to the real one while ( ! found ) { pow ++ ; for ( double f = 0 ; f < 10 ; f ++ ) { testStep = Math . pow ( 10 , pow ) * f ; if ( testStep >= step ) { factor = f ; found = true ; break ; } } } //first proposal double foundStep = Math . pow ( 10 , pow ) * factor ; //we shift to the closest lower minval to align with the step minYVal = minYVal - minYVal % foundStep ; //check if step is still good with minY trimmed. If not, use next factor. if ( minYVal + foundStep * gridLinesCount < maxYVal ) { foundStep = Math . pow ( 10 , pow ) * ( factor + ( pow > 0 ? 0.5 : 1 ) ) ; } //last visual optimization: find the optimal minYVal double trim = 10 ; while ( ( minYVal - minYVal % trim ) + foundStep * gridLinesCount >= maxYVal && minYVal > 0 ) { minYVal = minYVal - minYVal % trim ; trim = trim * 10 ; } //final calculation maxYVal = minYVal + foundStep * gridLinesCount ; }
compute minY and step value to have better readable charts