/** * Calculates the current legend size. * * @param renderer the renderer * @param defaultHeight the default height * @param extraHeight the added extra height * @return the legend size */ protected int getLegendSize(DefaultRenderer renderer, int defaultHeight, float extraHeight) { int legendSize = renderer.getLegendHeight(); if (renderer.isShowLegend() && legendSize == 0) { legendSize = defaultHeight; } if (!renderer.isShowLegend() && renderer.isShowLabels()) { legendSize = (int) (renderer.getLabelsTextSize() * 4 / 3 + extraHeight); } return legendSize; }
/** * Draws the chart legend. * * @param canvas the canvas to paint to * @param renderer the series renderer * @param titles the titles to go to the legend * @param left the left X value of the area to draw to * @param right the right X value of the area to draw to * @param y the y value of the area to draw to * @param width the width of the area to draw to * @param height the height of the area to draw to * @param legendSize the legend size * @param paint the paint to be used for drawing * @param calculate if only calculating the legend size * @return the legend height */ protected int drawLegend( Canvas canvas, DefaultRenderer renderer, String[] titles, int left, int right, int y, int width, int height, int legendSize, Paint paint, boolean calculate) { float size = 32; if (renderer.isShowLegend()) { float currentX = left; float currentY = y + height - legendSize + size; paint.setTextAlign(Align.LEFT); paint.setTextSize(renderer.getLegendTextSize()); int sLength = Math.min(titles.length, renderer.getSeriesRendererCount()); for (int i = 0; i < sLength; i++) { final float lineSize = getLegendShapeWidth(i); String text = titles[i]; if (titles.length == renderer.getSeriesRendererCount()) { paint.setColor(renderer.getSeriesRendererAt(i).getColor()); } else { paint.setColor(Color.LTGRAY); } float[] widths = new float[text.length()]; paint.getTextWidths(text, widths); float sum = 0; for (float value : widths) { sum += value; } float extraSize = lineSize + 10 + sum; float currentWidth = currentX + extraSize; if (i > 0 && getExceed(currentWidth, renderer, right, width)) { currentX = left; currentY += renderer.getLegendTextSize(); size += renderer.getLegendTextSize(); currentWidth = currentX + extraSize; } if (getExceed(currentWidth, renderer, right, width)) { float maxWidth = right - currentX - lineSize - 10; if (isVertical(renderer)) { maxWidth = width - currentX - lineSize - 10; } int nr = paint.breakText(text, true, maxWidth, widths); text = text.substring(0, nr) + "..."; } if (!calculate) { drawLegendShape(canvas, renderer.getSeriesRendererAt(i), currentX, currentY, i, paint); drawString(canvas, text, currentX + lineSize + 5, currentY + 5, paint); } currentX += extraSize; } } return Math.round(size + renderer.getLegendTextSize()); }