public static void calculateLegendInformation ( List < ? extends BaseModel > _Models , float _StartX , float _EndX , Paint _Paint ) { float textMargin = Utils . dpToPx ( 10.f ) ; float lastX = _StartX ; // calculate the legend label positions and check if there is enough space to display the label, // if not the label will not be shown for ( BaseModel model : _Models ) { if ( ! model . isIgnore ( ) ) { Rect textBounds = new Rect ( ) ; RectF legendBounds = model . getLegendBounds ( ) ; _Paint . getTextBounds ( model . getLegendLabel ( ) , 0 , model . getLegendLabel ( ) . length ( ) , textBounds ) ; model . setTextBounds ( textBounds ) ; float centerX = legendBounds . centerX ( ) ; float centeredTextPos = centerX - ( textBounds . width ( ) / 2 ) ; float textStartPos = centeredTextPos - textMargin ; // check if the text is too big to fit on the screen if ( centeredTextPos + textBounds . width ( ) > _EndX - textMargin ) { model . setShowLabel ( false ) ; } else { // check if the current legend label overrides the label before // if the label overrides the label before, the current label will not be shown. // If not the label will be shown and the label position is calculated if ( textStartPos < lastX ) { if ( lastX + textMargin < legendBounds . left ) { model . setLegendLabelPosition ( ( int ) ( lastX + textMargin ) ) ; model . setShowLabel ( true ) ; lastX = lastX + textMargin + textBounds . width ( ) ; } else { model . setShowLabel ( false ) ; } } else { model . setShowLabel ( true ) ; model . setLegendLabelPosition ( ( int ) centeredTextPos ) ; lastX = centerX + ( textBounds . width ( ) / 2 ) ; } } } } }
Calculates the legend positions and which legend title should be displayed or not .