Search in sources :

Example 1 with Rectangle2D

use of com.codename1.ui.geom.Rectangle2D in project CodenameOne by codenameone.

the class EditorFont method getBitmapFont.

/**
 * @return the bitmapFont
 */
public Font getBitmapFont() {
    Font bitmapFont = Font.getBitmapFont(lookupFont);
    if (bitmapFont != null) {
        return bitmapFont;
    }
    BufferedImage image = new BufferedImage(5000, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, bitmapAntialiasing);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.setColor(new Color(0xff0000));
    g2d.setFont(java.awt.Font.decode(lookupFont.split(";")[0]));
    FontMetrics metrics = g2d.getFontMetrics();
    FontRenderContext context = g2d.getFontRenderContext();
    int height = (int) Math.ceil(metrics.getMaxDescent() + metrics.getMaxAscent());
    int baseline = (int) Math.ceil(metrics.getMaxAscent());
    String charsetStr = bitmapCharset;
    int[] offsets = new int[charsetStr.length()];
    int[] widths = new int[offsets.length];
    int currentOffset = 0;
    for (int iter = 0; iter < charsetStr.length(); iter++) {
        offsets[iter] = currentOffset;
        String currentChar = charsetStr.substring(iter, iter + 1);
        g2d.drawString(currentChar, currentOffset, baseline);
        Rectangle2D rect = g2d.getFont().getStringBounds(currentChar, context);
        widths[iter] = (int) Math.ceil(rect.getWidth());
        // occupies more ram
        if (g2d.getFont().isItalic()) {
            currentOffset += metrics.getMaxAdvance();
        } else {
            currentOffset += widths[iter] + 1;
        }
    }
    g2d.dispose();
    BufferedImage shrunk = new BufferedImage(currentOffset, height, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) shrunk.getGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    int[] rgb = new int[shrunk.getWidth() * shrunk.getHeight()];
    shrunk.getRGB(0, 0, shrunk.getWidth(), shrunk.getHeight(), rgb, 0, shrunk.getWidth());
    com.codename1.ui.Image bitmap = com.codename1.ui.Image.createImage(rgb, shrunk.getWidth(), shrunk.getHeight());
    return com.codename1.ui.Font.createBitmapFont(lookupFont, bitmap, offsets, widths, charsetStr);
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) Font(com.codename1.ui.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) com.codename1.ui(com.codename1.ui) FontMetrics(java.awt.FontMetrics) FontRenderContext(java.awt.font.FontRenderContext)

Example 2 with Rectangle2D

use of com.codename1.ui.geom.Rectangle2D in project CodenameOne by codenameone.

the class XYChart method draw.

/**
 * The graphical representation of the XY chart.
 *
 * @param canvas the canvas to paint to
 * @param x the top left x value of the view to draw to
 * @param y the top left y value of the view to draw to
 * @param width the width of the view to draw to
 * @param height the height of the view to draw to
 * @param paint the paint
 */
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
    paint.setAntiAlias(mRenderer.isAntialiasing());
    int legendSize = getLegendSize(mRenderer, height / 5, mRenderer.getAxisTitleTextSize());
    int[] margins = mRenderer.getMargins();
    int left = x + margins[1] + (int) mRenderer.getAxisTitleTextSize() + (mRenderer.isShowLabels() ? (int) mRenderer.getLabelsTextSize() : 0);
    int top = y + margins[0];
    int right = x + width - margins[3];
    int sLength = mDataset.getSeriesCount();
    String[] titles = new String[sLength];
    for (int i = 0; i < sLength; i++) {
        titles[i] = mDataset.getSeriesAt(i).getTitle();
    }
    if (mRenderer.isFitLegend() && mRenderer.isShowLegend()) {
        legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
    }
    int bottom = y + height - margins[2] - legendSize - (int) mRenderer.getAxisTitleTextSize() - (mRenderer.isShowLabels() ? (int) mRenderer.getLabelsTextSize() : 0);
    if (mScreenR == null) {
        mScreenR = new Rectangle();
    }
    mScreenR.setBounds(left, top, right - left, bottom - top);
    drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
    if (paint.getTypeface() == null || (mRenderer.getTextTypeface() != null && paint.getTypeface().equals(mRenderer.getTextTypeface())) || !paint.getTypeface().toString().equals(mRenderer.getTextTypefaceName()) || paint.getTypeface().getStyle() != mRenderer.getTextTypefaceStyle()) {
        if (mRenderer.getTextTypeface() != null) {
            paint.setTypeface(mRenderer.getTextTypeface());
        } else {
            paint.setTypeface(Font.createSystemFont(mRenderer.getTextTypefaceName(), mRenderer.getTextTypefaceStyle(), Font.SIZE_SMALL));
        }
    }
    Orientation or = mRenderer.getOrientation();
    if (or == Orientation.VERTICAL) {
        right -= legendSize;
        bottom += legendSize - 20;
    }
    int angle = or.getAngle();
    boolean rotate = angle == 90;
    mScale = (float) (height) / width;
    mTranslate = Math.abs(width - height) / 2;
    if (mScale < 1) {
        mTranslate *= -1;
    }
    mCenter = new Point((x + width) / 2, (y + height) / 2);
    if (rotate) {
        transform(canvas, angle, false);
    }
    int maxScaleNumber = -Integer.MAX_VALUE;
    for (int i = 0; i < sLength; i++) {
        maxScaleNumber = Math.max(maxScaleNumber, mDataset.getSeriesAt(i).getScaleNumber());
    }
    maxScaleNumber++;
    if (maxScaleNumber < 0) {
        return;
    }
    double[] minX = new double[maxScaleNumber];
    double[] maxX = new double[maxScaleNumber];
    double[] minY = new double[maxScaleNumber];
    double[] maxY = new double[maxScaleNumber];
    boolean[] isMinXSet = new boolean[maxScaleNumber];
    boolean[] isMaxXSet = new boolean[maxScaleNumber];
    boolean[] isMinYSet = new boolean[maxScaleNumber];
    boolean[] isMaxYSet = new boolean[maxScaleNumber];
    for (int i = 0; i < maxScaleNumber; i++) {
        minX[i] = mRenderer.getXAxisMin(i);
        maxX[i] = mRenderer.getXAxisMax(i);
        minY[i] = mRenderer.getYAxisMin(i);
        maxY[i] = mRenderer.getYAxisMax(i);
        isMinXSet[i] = mRenderer.isMinXSet(i);
        isMaxXSet[i] = mRenderer.isMaxXSet(i);
        isMinYSet[i] = mRenderer.isMinYSet(i);
        isMaxYSet[i] = mRenderer.isMaxYSet(i);
        if (mCalcRange.get(i) == null) {
            mCalcRange.put(i, new double[4]);
        }
    }
    double[] xPixelsPerUnit = new double[maxScaleNumber];
    double[] yPixelsPerUnit = new double[maxScaleNumber];
    for (int i = 0; i < sLength; i++) {
        XYSeries series = mDataset.getSeriesAt(i);
        int scale = series.getScaleNumber();
        if (series.getItemCount() == 0) {
            continue;
        }
        if (!isMinXSet[scale]) {
            double minimumX = series.getMinX();
            if (minimumX != MathHelper.NULL_VALUE) {
                minX[scale] = minX[scale] == MathHelper.NULL_VALUE ? minimumX : Math.min(minX[scale], minimumX);
                mCalcRange.get(scale)[0] = minX[scale];
            } else {
                minX[scale] = 0;
                mCalcRange.get(scale)[0] = 0;
            }
        }
        if (!isMaxXSet[scale]) {
            double maximumX = series.getMaxX();
            if (maximumX != MathHelper.NULL_VALUE) {
                maxX[scale] = maxX[scale] == MathHelper.NULL_VALUE ? maximumX : Math.max(maxX[scale], maximumX);
                mCalcRange.get(scale)[1] = maxX[scale];
            } else {
                maxX[scale] = minX[scale] + 1;
                mCalcRange.get(scale)[1] = maxX[scale];
            }
        }
        if (!isMinYSet[scale]) {
            double minimumY = series.getMinY();
            if (minimumY != MathHelper.NULL_VALUE) {
                minY[scale] = minY[scale] == MathHelper.NULL_VALUE ? minimumY : Math.min(minY[scale], (float) minimumY);
                mCalcRange.get(scale)[2] = minY[scale];
            } else {
                minY[scale] = 0;
                mCalcRange.get(scale)[2] = 0;
            }
        }
        if (!isMaxYSet[scale]) {
            double maximumY = series.getMaxY();
            if (maximumY != MathHelper.NULL_VALUE) {
                maxY[scale] = maxY[scale] == MathHelper.NULL_VALUE ? maximumY : Math.max(maxY[scale], (float) maximumY);
                mCalcRange.get(scale)[3] = maxY[scale];
            } else {
                maxY[scale] = minY[scale] + 1;
                mCalcRange.get(scale)[3] = maxY[scale];
            }
        }
    }
    for (int i = 0; i < maxScaleNumber; i++) {
        if (maxX[i] - minX[i] != 0) {
            xPixelsPerUnit[i] = (right - left) / (maxX[i] - minX[i]);
        }
        if (maxY[i] - minY[i] != 0) {
            yPixelsPerUnit[i] = (float) ((bottom - top) / (maxY[i] - minY[i]));
        }
        // the X axis on multiple scales was wrong without this fix
        if (i > 0) {
            xPixelsPerUnit[i] = xPixelsPerUnit[0];
            minX[i] = minX[0];
            maxX[i] = maxX[0];
        }
    }
    boolean hasValues = false;
    // use a linked list for these reasons:
    // 1) Avoid a large contiguous memory allocation
    // 2) We don't need random seeking, only sequential reading/writing, so
    // linked list makes sense
    clickableAreas = new HashMap<Integer, List<ClickableArea>>();
    for (int i = 0; i < sLength; i++) {
        XYSeries series = mDataset.getSeriesAt(i);
        int scale = series.getScaleNumber();
        if (series.getItemCount() == 0) {
            continue;
        }
        hasValues = true;
        XYSeriesRenderer seriesRenderer = (XYSeriesRenderer) mRenderer.getSeriesRendererAt(i);
        // int originalValuesLength = series.getItemCount();
        // int valuesLength = originalValuesLength;
        // int length = valuesLength * 2;
        List<Float> points = new ArrayList<Float>();
        List<Double> values = new ArrayList<Double>();
        float yAxisValue = Math.min(bottom, (float) (bottom + yPixelsPerUnit[scale] * minY[scale]));
        LinkedList<ClickableArea> clickableArea = new LinkedList<ClickableArea>();
        clickableAreas.put(i, clickableArea);
        synchronized (series) {
            SortedMap<Double, Double> range = series.getRange(minX[scale], maxX[scale], seriesRenderer.isDisplayBoundingPoints());
            int startIndex = -1;
            for (Double value : range.keySet()) {
                double xValue = value;
                Double rValue = range.get(value);
                double yValue = rValue.doubleValue();
                if (startIndex < 0 && (!isNullValue(yValue) || isRenderNullValues())) {
                    startIndex = series.getIndexForKey(xValue);
                }
                // points.add((float) (left + xPixelsPerUnit[scale]
                // * (value.getKey().floatValue() - minX[scale])));
                // points.add((float) (bottom - yPixelsPerUnit[scale]
                // * (value.getValue().floatValue() - minY[scale])));
                values.add(value);
                values.add(rValue);
                if (!isNullValue(yValue)) {
                    points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
                    points.add((float) (bottom - yPixelsPerUnit[scale] * (yValue - minY[scale])));
                } else if (isRenderNullValues()) {
                    points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
                    points.add((float) (bottom - yPixelsPerUnit[scale] * (-minY[scale])));
                } else {
                    if (points.size() > 0) {
                        drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex);
                        ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(points, values, yAxisValue, i, startIndex);
                        clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
                        points.clear();
                        values.clear();
                        startIndex = -1;
                    }
                    clickableArea.add(null);
                }
            }
            int count = series.getAnnotationCount();
            if (count > 0) {
                paint.setColor(seriesRenderer.getAnnotationsColor());
                paint.setTextSize(seriesRenderer.getAnnotationsTextSize());
                paint.setTextAlign(seriesRenderer.getAnnotationsTextAlign());
                Rectangle2D bound = new Rectangle2D();
                for (int j = 0; j < count; j++) {
                    float xS = (float) (left + xPixelsPerUnit[scale] * (series.getAnnotationX(j) - minX[scale]));
                    float yS = (float) (bottom - yPixelsPerUnit[scale] * (series.getAnnotationY(j) - minY[scale]));
                    paint.getTextBounds(series.getAnnotationAt(j), 0, series.getAnnotationAt(j).length(), bound);
                    if (xS < (xS + bound.getWidth()) && yS < canvas.getHeight()) {
                        drawString(canvas, series.getAnnotationAt(j), xS, yS, paint);
                    }
                }
            }
            if (points.size() > 0) {
                drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex);
                ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(points, values, yAxisValue, i, startIndex);
                clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
            }
        }
    }
    // draw stuff over the margins such as data doesn't render on these areas
    drawBackground(mRenderer, canvas, x, bottom, width, height - bottom, paint, true, mRenderer.getMarginsColor());
    drawBackground(mRenderer, canvas, x, y, width, margins[0], paint, true, mRenderer.getMarginsColor());
    if (or == Orientation.HORIZONTAL) {
        drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
        drawBackground(mRenderer, canvas, right, y, margins[3], height - y, paint, true, mRenderer.getMarginsColor());
    } else if (or == Orientation.VERTICAL) {
        drawBackground(mRenderer, canvas, right, y, width - right, height - y, paint, true, mRenderer.getMarginsColor());
        drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
    }
    boolean showLabels = mRenderer.isShowLabels() && hasValues;
    boolean showGridX = mRenderer.isShowGridX();
    boolean showTickMarks = mRenderer.isShowTickMarks();
    // boolean showCustomTextGridX = mRenderer.isShowCustomTextGridX();
    boolean showCustomTextGridY = mRenderer.isShowCustomTextGridY();
    if (showLabels || showGridX) {
        List<Double> xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels()));
        Map<Integer, List<Double>> allYLabels = getYLabels(minY, maxY, maxScaleNumber);
        int xLabelsLeft = left;
        if (showLabels) {
            paint.setColor(mRenderer.getXLabelsColor());
            paint.setTextSize(mRenderer.getLabelsTextSize());
            paint.setTextAlign(mRenderer.getXLabelsAlign());
        // if (mRenderer.getXLabelsAlign() == Align.LEFT) {
        // xLabelsLeft += mRenderer.getLabelsTextSize() / 4;
        // }
        }
        drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]);
        drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY);
        if (showLabels) {
            paint.setColor(mRenderer.getLabelsColor());
            for (int i = 0; i < maxScaleNumber; i++) {
                int axisAlign = mRenderer.getYAxisAlign(i);
                Double[] yTextLabelLocations = mRenderer.getYTextLabelLocations(i);
                for (Double location : yTextLabelLocations) {
                    if (minY[i] <= location && location <= maxY[i]) {
                        float yLabel = (float) (bottom - yPixelsPerUnit[i] * (location.doubleValue() - minY[i]));
                        String label = mRenderer.getYTextLabel(location, i);
                        paint.setColor(mRenderer.getYLabelsColor(i));
                        paint.setTextAlign(mRenderer.getYLabelsAlign(i));
                        if (or == Orientation.HORIZONTAL) {
                            if (axisAlign == Align.LEFT) {
                                if (showTickMarks) {
                                    canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint);
                                }
                                drawText(canvas, label, left - mRenderer.getYLabelsPadding(), yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle());
                            } else {
                                if (showTickMarks) {
                                    canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, paint);
                                }
                                drawText(canvas, label, right - mRenderer.getYLabelsPadding(), yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle());
                            }
                            if (showCustomTextGridY) {
                                paint.setColor(mRenderer.getGridColor(i));
                                canvas.drawLine(left, yLabel, right, yLabel, paint);
                            }
                        } else {
                            if (showTickMarks) {
                                canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint);
                            }
                            drawText(canvas, label, right + 10, yLabel - mRenderer.getYLabelsVerticalPadding(), paint, mRenderer.getYLabelsAngle());
                            if (showCustomTextGridY) {
                                paint.setColor(mRenderer.getGridColor(i));
                                canvas.drawLine(right, yLabel, left, yLabel, paint);
                            }
                        }
                    }
                }
            }
        }
        if (showLabels) {
            paint.setColor(mRenderer.getLabelsColor());
            float size = mRenderer.getAxisTitleTextSize();
            paint.setTextSize(size);
            paint.setTextAlign(Align.CENTER);
            if (or == Orientation.HORIZONTAL) {
                drawText(canvas, mRenderer.getXTitle(), x + width / 2, bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding() + size, paint, 0);
                for (int i = 0; i < maxScaleNumber; i++) {
                    int axisAlign = mRenderer.getYAxisAlign(i);
                    if (axisAlign == Align.LEFT) {
                        drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90);
                    } else {
                        drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90);
                    }
                }
                paint.setTextSize(mRenderer.getChartTitleTextSize());
                drawText(canvas, mRenderer.getChartTitle(), x + width / 2, y + mRenderer.getChartTitleTextSize(), paint, 0);
            } else if (or == Orientation.VERTICAL) {
                drawText(canvas, mRenderer.getXTitle(), x + width / 2, y + height - size + mRenderer.getXLabelsPadding(), paint, -90);
                drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0);
                paint.setTextSize(mRenderer.getChartTitleTextSize());
                drawText(canvas, mRenderer.getChartTitle(), x + size, top + height / 2, paint, 0);
            }
        }
    }
    if (or == Orientation.HORIZONTAL) {
        drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), width, height, legendSize, paint, false);
    } else if (or == Orientation.VERTICAL) {
        transform(canvas, angle, true);
        drawLegend(canvas, mRenderer, titles, left, right, y + (int) mRenderer.getXLabelsPadding(), width, height, legendSize, paint, false);
        transform(canvas, angle, false);
    }
    if (mRenderer.isShowAxes()) {
        paint.setColor(mRenderer.getXAxisColor());
        canvas.drawLine(left, bottom, right, bottom, paint);
        paint.setColor(mRenderer.getYAxisColor());
        boolean rightAxis = false;
        for (int i = 0; i < maxScaleNumber && !rightAxis; i++) {
            rightAxis = mRenderer.getYAxisAlign(i) == Align.RIGHT;
        }
        if (or == Orientation.HORIZONTAL) {
            canvas.drawLine(left, top, left, bottom, paint);
            if (rightAxis) {
                canvas.drawLine(right, top, right, bottom, paint);
            }
        } else if (or == Orientation.VERTICAL) {
            canvas.drawLine(right, top, right, bottom, paint);
        }
    }
    if (rotate) {
        transform(canvas, angle, true);
    }
}
Also used : XYSeries(com.codename1.charts.models.XYSeries) Rectangle(com.codename1.ui.geom.Rectangle) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Rectangle2D(com.codename1.ui.geom.Rectangle2D) Point(com.codename1.charts.models.Point) Orientation(com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint) LinkedList(java.util.LinkedList) XYSeriesRenderer(com.codename1.charts.renderers.XYSeriesRenderer)

Example 3 with Rectangle2D

use of com.codename1.ui.geom.Rectangle2D in project CodenameOne by codenameone.

the class PieChart method draw.

/**
 * The graphical representation of the pie chart.
 *
 * @param canvas the canvas to paint to
 * @param x the top left x value of the view to draw to
 * @param y the top left y value of the view to draw to
 * @param width the width of the view to draw to
 * @param height the height of the view to draw to
 * @param paint the paint
 */
@Override
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
    paint.setAntiAlias(mRenderer.isAntialiasing());
    paint.setStyle(Style.FILL);
    paint.setTextSize(mRenderer.getLabelsTextSize());
    int legendSize = getLegendSize(mRenderer, height / 5, 0);
    int left = x;
    int top = y;
    int right = x + width;
    int sLength = mDataset.getItemCount();
    double total = 0;
    String[] titles = new String[sLength];
    for (int i = 0; i < sLength; i++) {
        total += mDataset.getValue(i);
        titles[i] = mDataset.getCategory(i);
    }
    if (mRenderer.isFitLegend()) {
        legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
    }
    int bottom = y + height - legendSize;
    drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
    float currentAngle = mRenderer.getStartAngle();
    int mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
    int radius = (int) (mRadius * 0.35 * mRenderer.getScale());
    if (autoCalculateCenter || mCenterX == NO_VALUE) {
        mCenterX = (left + right) / 2;
    }
    if (autoCalculateCenter || mCenterY == NO_VALUE) {
        mCenterY = (bottom + top) / 2;
    }
    // Hook in clip detection after center has been calculated
    mPieMapper.setDimensions(radius, mCenterX, mCenterY);
    boolean loadPieCfg = !mPieMapper.areAllSegmentPresent(sLength);
    if (loadPieCfg) {
        mPieMapper.clearPieSegments();
    }
    float shortRadius = radius * 0.9f;
    float longRadius = radius * 1.1f;
    Rectangle2D oval = PkgUtils.makeRect(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
    List<Rectangle2D> prevLabelsBounds = new ArrayList<Rectangle2D>();
    for (int i = 0; i < sLength; i++) {
        SimpleSeriesRenderer seriesRenderer = mRenderer.getSeriesRendererAt(i);
        boolean gradient = false;
        GradientDrawable gradientDrawable = null;
        if (seriesRenderer.isGradientEnabled()) {
            gradient = true;
            gradientDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { seriesRenderer.getGradientStartColor(), seriesRenderer.getGradientStopColor() });
            paint.setColor(seriesRenderer.getGradientStartColor());
        } else {
            paint.setColor(seriesRenderer.getColor());
        }
        float value = (float) mDataset.getValue(i);
        float angle = (float) (value / total * 360);
        if (seriesRenderer.isHighlighted()) {
            double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));
            float translateX = (float) (radius * 0.1 * Math.sin(rAngle));
            float translateY = (float) (radius * 0.1 * Math.cos(rAngle));
            oval.translate(translateX, translateY);
            if (gradient) {
                canvas.drawArcWithGradient(oval, currentAngle, angle, true, paint, gradientDrawable);
            } else {
                canvas.drawArc(oval, currentAngle, angle, true, paint);
            }
            oval.translate(-translateX, -translateY);
        } else {
            if (gradient) {
                canvas.drawArcWithGradient(oval, currentAngle, angle, true, paint, gradientDrawable);
            } else {
                canvas.drawArc(oval, currentAngle, angle, true, paint);
            }
        }
        paint.setColor(seriesRenderer.getColor());
        // paint.setShader(null);
        drawLabel(canvas, mDataset.getCategory(i), mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius, longRadius, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, true, false);
        if (mRenderer.isDisplayValues()) {
            drawLabel(canvas, getLabel(mRenderer.getSeriesRendererAt(i).getChartValuesFormat(), mDataset.getValue(i)), mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius / 2, longRadius / 2, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, false, true);
        }
        // Save details for getSeries functionality
        if (loadPieCfg) {
            mPieMapper.addPieSegment(i, value, currentAngle, angle);
        }
        currentAngle += angle;
    }
    prevLabelsBounds.clear();
    drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
    drawTitle(canvas, x, y, width, paint);
}
Also used : SimpleSeriesRenderer(com.codename1.charts.renderers.SimpleSeriesRenderer) Rectangle2D(com.codename1.ui.geom.Rectangle2D) ArrayList(java.util.ArrayList) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint) GradientDrawable(com.codename1.charts.compat.GradientDrawable)

Example 4 with Rectangle2D

use of com.codename1.ui.geom.Rectangle2D in project CodenameOne by codenameone.

the class AbstractChart method drawLabel.

/**
 * Draws a text label.
 *
 * @param canvas the canvas
 * @param labelText the label text
 * @param renderer the renderer
 * @param prevLabelsBounds the previous rendered label bounds
 * @param centerX the round chart center on X axis
 * @param centerY the round chart center on Y axis
 * @param shortRadius the short radius for the round chart
 * @param longRadius the long radius for the round chart
 * @param currentAngle the current angle
 * @param angle the label extra angle
 * @param left the left side
 * @param right the right side
 * @param color the label color
 * @param paint the paint
 * @param line if a line to the label should be drawn
 * @param display display the label anyway
 */
protected void drawLabel(Canvas canvas, String labelText, DefaultRenderer renderer, List<Rectangle2D> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line, boolean display) {
    if (renderer.isShowLabels() || display) {
        paint.setColor(color);
        double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));
        double sinValue = Math.sin(rAngle);
        double cosValue = Math.cos(rAngle);
        int x1 = Math.round(centerX + (float) (shortRadius * sinValue));
        int y1 = Math.round(centerY + (float) (shortRadius * cosValue));
        int x2 = Math.round(centerX + (float) (longRadius * sinValue));
        int y2 = Math.round(centerY + (float) (longRadius * cosValue));
        float size = renderer.getLabelsTextSize();
        float extra = Math.max(size / 2, 10);
        paint.setTextAlign(Component.LEFT);
        if (x1 > x2) {
            extra = -extra;
            paint.setTextAlign(Component.RIGHT);
        }
        float xLabel = x2 + extra;
        float yLabel = y2;
        float width = right - xLabel;
        if (x1 > x2) {
            width = xLabel - left;
        }
        labelText = getFitText(labelText, width, paint);
        float widthLabel = paint.measureText(labelText);
        boolean okBounds = false;
        while (!okBounds && line) {
            boolean intersects = false;
            int length = prevLabelsBounds.size();
            for (int j = 0; j < length && !intersects; j++) {
                Rectangle2D prevLabelBounds = prevLabelsBounds.get(j);
                if (prevLabelBounds.intersects(xLabel, yLabel, widthLabel, size)) {
                    intersects = true;
                    yLabel = (float) Math.max(yLabel, prevLabelBounds.getY() + prevLabelBounds.getHeight());
                }
            }
            okBounds = !intersects;
        }
        if (line) {
            y2 = (int) (yLabel - size / 2);
            canvas.drawLine(x1, y1, x2, y2, paint);
            canvas.drawLine(x2, y2, x2 + extra, y2, paint);
        } else {
            paint.setTextAlign(Component.CENTER);
        }
        canvas.drawText(labelText, xLabel, yLabel, paint);
        if (line) {
            prevLabelsBounds.add(PkgUtils.makeRect(xLabel, yLabel, xLabel + widthLabel, yLabel + size));
        }
    }
}
Also used : Rectangle2D(com.codename1.ui.geom.Rectangle2D) Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 5 with Rectangle2D

use of com.codename1.ui.geom.Rectangle2D in project CodenameOne by codenameone.

the class DoughnutChart method draw.

/**
 * The graphical representation of the doughnut chart.
 *
 * @param canvas the canvas to paint to
 * @param x the top left x value of the view to draw to
 * @param y the top left y value of the view to draw to
 * @param width the width of the view to draw to
 * @param height the height of the view to draw to
 * @param paint the paint
 */
@Override
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
    paint.setAntiAlias(mRenderer.isAntialiasing());
    paint.setStyle(Style.FILL);
    paint.setTextSize(mRenderer.getLabelsTextSize());
    int legendSize = getLegendSize(mRenderer, height / 5, 0);
    int left = x;
    int top = y;
    int right = x + width;
    int cLength = mDataset.getCategoriesCount();
    String[] categories = new String[cLength];
    for (int category = 0; category < cLength; category++) {
        categories[category] = mDataset.getCategory(category);
    }
    if (mRenderer.isFitLegend()) {
        legendSize = drawLegend(canvas, mRenderer, categories, left, right, y, width, height, legendSize, paint, true);
    }
    int bottom = y + height - legendSize;
    drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
    mStep = SHAPE_WIDTH * 3 / 4;
    int mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
    double rCoef = 0.35 * mRenderer.getScale();
    double decCoef = 0.2 / cLength;
    int radius = (int) (mRadius * rCoef);
    if (autoCalculateCenter || mCenterX == NO_VALUE) {
        mCenterX = (left + right) / 2;
    }
    if (autoCalculateCenter || mCenterY == NO_VALUE) {
        mCenterY = (bottom + top) / 2;
    }
    float shortRadius = radius * 0.9f;
    float longRadius = radius * 1.1f;
    List<Rectangle2D> prevLabelsBounds = new ArrayList<Rectangle2D>();
    for (int category = 0; category < cLength; category++) {
        int sLength = mDataset.getItemCount(category);
        double total = 0;
        String[] titles = new String[sLength];
        for (int i = 0; i < sLength; i++) {
            total += mDataset.getValues(category)[i];
            titles[i] = mDataset.getTitles(category)[i];
        }
        float currentAngle = mRenderer.getStartAngle();
        Rectangle2D oval = PkgUtils.makeRect(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
        for (int i = 0; i < sLength; i++) {
            paint.setColor(mRenderer.getSeriesRendererAt(i).getColor());
            float value = (float) mDataset.getValues(category)[i];
            float angle = (float) (value / total * 360);
            canvas.drawArc(oval, currentAngle, angle, true, paint);
            drawLabel(canvas, mDataset.getTitles(category)[i], mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius, longRadius, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, true, false);
            currentAngle += angle;
        }
        radius -= (int) mRadius * decCoef;
        shortRadius -= mRadius * decCoef - 2;
        if (mRenderer.getBackgroundColor() != 0) {
            paint.setColor(mRenderer.getBackgroundColor());
        } else {
            paint.setColor(ColorUtil.WHITE);
        }
        paint.setStyle(Style.FILL);
        oval = PkgUtils.makeRect(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
        canvas.drawArc(oval, 0, 360, true, paint);
        radius -= 1;
    }
    prevLabelsBounds.clear();
    drawLegend(canvas, mRenderer, categories, left, right, y, width, height, legendSize, paint, false);
    drawTitle(canvas, x, y, width, paint);
}
Also used : Rectangle2D(com.codename1.ui.geom.Rectangle2D) ArrayList(java.util.ArrayList) Paint(com.codename1.charts.compat.Paint)

Aggregations

Paint (com.codename1.charts.compat.Paint)5 Rectangle2D (com.codename1.ui.geom.Rectangle2D)5 Point (com.codename1.charts.models.Point)4 ArrayList (java.util.ArrayList)3 Font (com.codename1.ui.Font)2 GradientDrawable (com.codename1.charts.compat.GradientDrawable)1 XYSeries (com.codename1.charts.models.XYSeries)1 SimpleSeriesRenderer (com.codename1.charts.renderers.SimpleSeriesRenderer)1 Orientation (com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation)1 XYSeriesRenderer (com.codename1.charts.renderers.XYSeriesRenderer)1 com.codename1.ui (com.codename1.ui)1 Rectangle (com.codename1.ui.geom.Rectangle)1 Color (java.awt.Color)1 FontMetrics (java.awt.FontMetrics)1 Graphics2D (java.awt.Graphics2D)1 FontRenderContext (java.awt.font.FontRenderContext)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1