Search in sources :

Example 6 with Point

use of com.codename1.charts.models.Point in project CodenameOne by codenameone.

the class ResetableTextWatcher method edit.

/**
 * Entry point for using this class
 * @param impl The current running activity
 * @param component Any subclass of com.codename1.ui.TextArea
 * @param inputType One of the TextArea's input-type constants
 */
public static void edit(final AndroidImplementation impl, final Component component, final int inputType) {
    if (impl.getActivity() == null) {
        throw new IllegalArgumentException("activity is null");
    }
    if (component == null) {
        throw new IllegalArgumentException("component is null");
    }
    if (!(component instanceof TextArea)) {
        throw new IllegalArgumentException("component must be instance of TextArea");
    }
    final TextArea textArea = (TextArea) component;
    final String initialText = textArea.getText();
    textArea.putClientProperty("InPlaceEditView.initialText", initialText);
    // The very first time we try to edit a string, let's determine if the
    // system default is to do async editing.  If the system default
    // is not yet set, we set it here, and it will be used as the default from now on
    // We do this because the nativeInstance.isAsyncEditMode() value changes
    // to reflect the currently edited field so it isn't a good way to keep a
    // system default.
    String defaultAsyncEditingSetting = Display.getInstance().getProperty("android.VKBAlwaysOpen", null);
    if (defaultAsyncEditingSetting == null) {
        defaultAsyncEditingSetting = impl.isAsyncEditMode() ? "true" : "false";
        Display.getInstance().setProperty("android.VKBAlwaysOpen", defaultAsyncEditingSetting);
    }
    boolean asyncEdit = "true".equals(defaultAsyncEditingSetting) ? true : false;
    // Check if the form has any setting for asyncEditing that should override
    // the application defaults.
    final Form parentForm = component.getComponentForm();
    if (parentForm == null) {
        com.codename1.io.Log.p("Attempt to edit text area that is not on a form.  This is not supported");
        return;
    }
    if (parentForm.getClientProperty("asyncEditing") != null) {
        Object async = parentForm.getClientProperty("asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Form overriding asyncEdit due to asyncEditing client property: "+asyncEdit);
        }
    }
    if (parentForm.getClientProperty("android.asyncEditing") != null) {
        Object async = parentForm.getClientProperty("android.asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Form overriding asyncEdit due to ios.asyncEditing client property: "+asyncEdit);
        }
    }
    if (parentForm.isFormBottomPaddingEditingMode()) {
        asyncEdit = true;
    }
    // then this will override all other settings.
    if (component.getClientProperty("asyncEditing") != null) {
        Object async = component.getClientProperty("asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Overriding asyncEdit due to field asyncEditing client property: "+asyncEdit);
        }
    }
    if (component.getClientProperty("android.asyncEditing") != null) {
        Object async = component.getClientProperty("android.asyncEditing");
        if (async instanceof Boolean) {
            asyncEdit = ((Boolean) async).booleanValue();
        // Log.p("Overriding asyncEdit due to field ios.asyncEditing client property: "+asyncEdit);
        }
    }
    // if true, then in async mode we are currently editing and are switching to another field
    final boolean isEditedFieldSwitch;
    // If we are already editing, we need to finish that up before we proceed to edit the next field.
    synchronized (editingLock) {
        if (mIsEditing) {
            if (impl.isAsyncEditMode()) {
                // Using isEditedFieldSwitch was causing issues with cursors not showing up.
                // https://github.com/codenameone/CodenameOne/issues/2353
                // https://stackoverflow.com/questions/49004370/focus-behaviour-in-textarea-in-cn1
                // Disabling this feature by default now, but can be re-enabled by setting
                // Display.getInstance().setProperty("android.reuseTextEditorOnSwitch", "true");
                // This editedFieldSwitch feature was added a while back to improve experience on older
                // Android devices where the field switching was going too slow.
                // https://github.com/codenameone/CodenameOne/issues/2012
                // This issue was resolved in this commit (https://github.com/jaanushansen/CodenameOne/commit/f3e53a80704149e4d7cde276d01c1368bcdcfe2c)
                // which was submitted as part of a pull request.  This fix has been the source of several
                // regressions, mostly related to properties not being propagated properly when a text field is changed
                // However, this issue (with the cursor not showing up), doesn't appear to have a simple solution
                // so, I'm disabling this feature for now.
                isEditedFieldSwitch = "true".equals(Display.getInstance().getProperty("android.reuseTextEditorOnSwitch", "false"));
                final String[] out = new String[1];
                TextArea prevTextArea = null;
                if (sInstance != null && sInstance.mLastEditText != null) {
                    prevTextArea = sInstance.mLastEditText.getTextArea();
                }
                if (prevTextArea != null) {
                    final TextArea fPrevTextArea = prevTextArea;
                    final String retVal = sInstance.mLastEditText.getText().toString();
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            Display.getInstance().onEditingComplete(fPrevTextArea, retVal);
                        }
                    });
                }
                InPlaceEditView.setEditedTextField(textArea);
                nextTextArea = null;
            } else {
                isEditedFieldSwitch = false;
                final InPlaceEditView instance = sInstance;
                if (instance != null && instance.mEditText != null && instance.mEditText.mTextArea == textArea) {
                    instance.showTextEditorAgain();
                    return;
                }
                if (!isClosing && sInstance != null && sInstance.mEditText != null) {
                    isClosing = true;
                    impl.getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            instance.endEditing(REASON_UNDEFINED, true, 0);
                        }
                    });
                }
                afterClose = new Runnable() {

                    @Override
                    public void run() {
                        impl.callHideTextEditor();
                        Display.getInstance().editString(component, textArea.getMaxSize(), inputType, textArea.getText());
                    }
                };
                return;
            }
        } else {
            isEditedFieldSwitch = false;
        }
        mIsEditing = true;
        isClosing = false;
        afterClose = null;
    }
    impl.setAsyncEditMode(asyncEdit);
    // textArea.setPreferredSize(prefSize);
    if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
        ((TextField) textArea).setEditable(false);
    }
    final boolean scrollableParent = isScrollableParent(textArea);
    // We wrap the text area so that we can safely pass data across to the
    // android UI thread.
    final TextAreaData textAreaData = new TextAreaData(textArea);
    impl.getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (!isEditedFieldSwitch) {
                releaseEdit();
                if (sInstance == null) {
                    sInstance = new InPlaceEditView(impl);
                    impl.relativeLayout.addView(sInstance);
                }
            // Let's try something new here
            // We'll ALWAYS try resize edit mode (since it just works better)
            // But we'll detect whether the field is still covered by the keyboard
            // and switch to pan mode if necessary.
            }
            if (scrollableParent || parentForm.isFormBottomPaddingEditingMode()) {
                setEditMode(true);
            } else {
                trySetEditMode(true);
            }
            sInstance.startEditing(impl.getActivity(), textAreaData, initialText, inputType, isEditedFieldSwitch);
        }
    });
    final String[] out = new String[1];
    // In order to reuse the code the runs after edit completion, we will wrap it in a runnable
    // For sync edit mode, we will just run onComplete.run() at the end of this method.  For
    // Async mode we add the Runnable to the textarea as a client property, then run it
    // when editing eventually completes.
    Runnable onComplete = new Runnable() {

        public void run() {
            if (!impl.isAsyncEditMode() && textArea instanceof TextField) {
                ((TextField) textArea).setEditable(true);
            }
            textArea.setPreferredSize(null);
            if (sInstance != null && sInstance.mLastEditText != null && sInstance.mLastEditText.mTextArea == textArea) {
                String retVal = sInstance.mLastEditText.getText().toString();
                if (!impl.isAsyncEditMode()) {
                    sInstance.mLastEditText = null;
                    impl.getActivity().runOnUiThread(new Runnable() {

                        public void run() {
                            releaseEdit();
                        }
                    });
                }
                out[0] = retVal;
            } else {
                out[0] = initialText;
            }
            Display.getInstance().onEditingComplete(component, out[0]);
            if (impl.isAsyncEditMode()) {
                impl.callHideTextEditor();
            } else {
                // lock.
                if (sInstance != null) {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (sInstance != null) {
                                com.codename1.io.Util.sleep(5);
                            }
                        }
                    });
                }
            }
            // Release the editing flag
            synchronized (editingLock) {
                mIsEditing = false;
            }
            // as a runnable ... this should take priority over the "nextTextArea" setting
            if (afterClose != null) {
                Display.getInstance().callSerially(afterClose);
            } else if (nextTextArea != null) {
                final TextArea next = nextTextArea;
                nextTextArea = null;
                next.requestFocus();
                Display.getInstance().callSerially(new Runnable() {

                    public void run() {
                        Display.getInstance().editString(next, next.getMaxSize(), next.getConstraint(), next.getText());
                    }
                });
            }
        }
    };
    textArea.requestFocus();
    textArea.repaint();
    if (impl.isAsyncEditMode()) {
        component.putClientProperty("android.onAsyncEditingComplete", onComplete);
        return;
    }
    // Make this call synchronous
    // We set this flag so that waitForEditCompletion can block on it.
    // The flag will be released inside the endEditing method which will
    // allow the method to proceed.
    waitingForSynchronousEditingCompletion = true;
    waitForEditCompletion();
    onComplete.run();
}
Also used : TextArea(com.codename1.ui.TextArea) Form(com.codename1.ui.Form) TextField(com.codename1.ui.TextField)

Example 7 with Point

use of com.codename1.charts.models.Point in project CodenameOne by codenameone.

the class ScatterChart method drawSeries.

/**
 * The graphical representation of a series.
 *
 * @param canvas the canvas to paint to
 * @param paint the paint to be used for drawing
 * @param points the array of points to be used for drawing the series
 * @param seriesRenderer the series renderer
 * @param yAxisValue the minimum value of the y axis
 * @param seriesIndex the index of the series currently being drawn
 * @param startIndex the start index of the rendering points
 */
@Override
public void drawSeries(Canvas canvas, Paint paint, List<Float> points, XYSeriesRenderer renderer, float yAxisValue, int seriesIndex, int startIndex) {
    paint.setColor(renderer.getColor());
    final float stroke = paint.getStrokeWidth();
    if (renderer.isFillPoints()) {
        paint.setStyle(Style.FILL);
    } else {
        paint.setStrokeWidth(renderer.getPointStrokeWidth());
        paint.setStyle(Style.STROKE);
    }
    int length = points.size();
    // switch on ENUM's generates reflection code that screws up J2ME
    PointStyle ps = renderer.getPointStyle();
    if (ps == PointStyle.X) {
        paint.setStrokeWidth(renderer.getPointStrokeWidth());
        for (int i = 0; i < length; i += 2) {
            drawX(canvas, paint, points.get(i), points.get(i + 1));
        }
    } else {
        if (ps == PointStyle.CIRCLE) {
            for (int i = 0; i < length; i += 2) {
                drawCircle(canvas, paint, points.get(i), points.get(i + 1));
            }
        } else {
            if (ps == PointStyle.TRIANGLE) {
                float[] path = new float[6];
                for (int i = 0; i < length; i += 2) {
                    drawTriangle(canvas, paint, path, points.get(i), points.get(i + 1));
                }
            } else {
                if (ps == PointStyle.SQUARE) {
                    for (int i = 0; i < length; i += 2) {
                        drawSquare(canvas, paint, points.get(i), points.get(i + 1));
                    }
                } else {
                    if (ps == PointStyle.DIAMOND) {
                        float[] path = new float[8];
                        for (int i = 0; i < length; i += 2) {
                            drawDiamond(canvas, paint, path, points.get(i), points.get(i + 1));
                        }
                    } else {
                        if (ps == PointStyle.POINT) {
                            for (int i = 0; i < length; i += 2) {
                                canvas.drawPoint(points.get(i), points.get(i + 1), paint);
                            }
                        }
                    }
                }
            }
        }
    }
    /*switch (renderer.getPointStyle()) {
    case X:
      paint.setStrokeWidth(renderer.getPointStrokeWidth());
      for (int i = 0; i < length; i += 2) {
        drawX(canvas, paint, points.get(i), points.get(i + 1));
      }
      break;
    case CIRCLE:
      for (int i = 0; i < length; i += 2) {
        drawCircle(canvas, paint, points.get(i), points.get(i + 1));
      }
      break;
    case TRIANGLE:
      float[] path = new float[6];
      for (int i = 0; i < length; i += 2) {
        drawTriangle(canvas, paint, path, points.get(i), points.get(i + 1));
      }
      break;
    case SQUARE:
      for (int i = 0; i < length; i += 2) {
        drawSquare(canvas, paint, points.get(i), points.get(i + 1));
      }
      break;
    case DIAMOND:
      path = new float[8];
      for (int i = 0; i < length; i += 2) {
        drawDiamond(canvas, paint, path, points.get(i), points.get(i + 1));
      }
      break;
    case POINT:
      for (int i = 0; i < length; i += 2) {
        canvas.drawPoint(points.get(i), points.get(i + 1), paint);
      }
      break;
    }*/
    paint.setStrokeWidth(stroke);
}
Also used : Paint(com.codename1.charts.compat.Paint)

Example 8 with Point

use of com.codename1.charts.models.Point in project CodenameOne by codenameone.

the class XYChart method drawChartValuesText.

/**
 * The graphical representation of the series values as text.
 *
 * @param canvas the canvas to paint to
 * @param series the series to be painted
 * @param renderer the series renderer
 * @param paint the paint to be used for drawing
 * @param points the array of points to be used for drawing the series
 * @param seriesIndex the index of the series currently being drawn
 * @param startIndex the start index of the rendering points
 */
protected void drawChartValuesText(Canvas canvas, XYSeries series, XYSeriesRenderer renderer, Paint paint, List<Float> points, int seriesIndex, int startIndex) {
    if (points.size() > 2) {
        // there are more than one point
        // record the first point's position
        float previousPointX = points.get(0);
        float previousPointY = points.get(1);
        for (int k = 0; k < points.size(); k += 2) {
            if (k == 2) {
                // not
                if (Math.abs(points.get(2) - points.get(0)) > renderer.getDisplayChartValuesDistance() || Math.abs(points.get(3) - points.get(1)) > renderer.getDisplayChartValuesDistance()) {
                    // first point
                    drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex)), points.get(0), points.get(1) - renderer.getChartValuesSpacing(), paint, 0);
                    // second point
                    drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + 1)), points.get(2), points.get(3) - renderer.getChartValuesSpacing(), paint, 0);
                    previousPointX = points.get(2);
                    previousPointY = points.get(3);
                }
            } else if (k > 2) {
                // are not too close, display
                if (Math.abs(points.get(k) - previousPointX) > renderer.getDisplayChartValuesDistance() || Math.abs(points.get(k + 1) - previousPointY) > renderer.getDisplayChartValuesDistance()) {
                    drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k / 2)), points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0);
                    previousPointX = points.get(k);
                    previousPointY = points.get(k + 1);
                }
            }
        }
    } else {
        // if only one point, display it
        for (int k = 0; k < points.size(); k += 2) {
            drawText(canvas, getLabel(renderer.getChartValuesFormat(), series.getY(startIndex + k / 2)), points.get(k), points.get(k + 1) - renderer.getChartValuesSpacing(), paint, 0);
        }
    }
}
Also used : Point(com.codename1.charts.models.Point) Paint(com.codename1.charts.compat.Paint)

Example 9 with Point

use of com.codename1.charts.models.Point 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 10 with Point

use of com.codename1.charts.models.Point in project CodenameOne by codenameone.

the class InteractionDialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param rect the screen rectangle to which the popup should point
 */
public void showPopupDialog(Rectangle rect) {
    disposed = false;
    if (getUIID().equals("Dialog")) {
        setUIID("PopupDialog");
        if (getTitleComponent().getUIID().equals("DialogTitle")) {
            getTitleComponent().setUIID("PopupDialogTitle");
        }
        getContentPane().setUIID("PopupContentPane");
    }
    Component contentPane = getContentPane();
    Label title = getTitleComponent();
    UIManager manager = getUIManager();
    String dialogTitle = title.getText();
    // preferred size logic of the dialog won't work with large title borders
    if ((dialogTitle != null || dialogTitle.length() == 0) && manager.isThemeConstant("hideEmptyTitleBool", false)) {
        boolean b = getTitle().length() > 0;
        titleArea.setVisible(b);
        getTitleComponent().setVisible(b);
        if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
            getTitleComponent().setPreferredSize(new Dimension(0, 0));
            getTitleComponent().getStyle().setBorder(null);
            titleArea.setPreferredSize(new Dimension(0, 0));
            if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
                getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
                int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
                int titlePT = getTitleComponent().getStyle().getPaddingTop();
                byte[] pu = getContentPane().getStyle().getPaddingUnit();
                if (pu == null) {
                    pu = new byte[4];
                }
                pu[0] = Style.UNIT_TYPE_PIXELS;
                getContentPane().getStyle().setPaddingUnit(pu);
                int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
                getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
            }
        }
    }
    // allows a text area to recalculate its preferred size if embedded within a dialog
    revalidate();
    Style contentPaneStyle = getStyle();
    boolean restoreArrow = false;
    if (manager.isThemeConstant(getUIID() + "ArrowBool", false)) {
        Image t = manager.getThemeImageConstant(getUIID() + "ArrowTopImage");
        Image b = manager.getThemeImageConstant(getUIID() + "ArrowBottomImage");
        Image l = manager.getThemeImageConstant(getUIID() + "ArrowLeftImage");
        Image r = manager.getThemeImageConstant(getUIID() + "ArrowRightImage");
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setImageBorderSpecialTile(t, b, l, r, rect);
            restoreArrow = true;
        }
    }
    calcPreferredSize();
    int prefHeight = getPreferredH();
    int prefWidth = getPreferredW();
    if (contentPaneStyle.getBorder() != null) {
        prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
        prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
    }
    Form f = Display.getInstance().getCurrent();
    int availableHeight = getLayeredPane(f).getParent().getHeight();
    int availableWidth = getLayeredPane(f).getParent().getWidth();
    int width = Math.min(availableWidth, prefWidth);
    int x = 0;
    int y = 0;
    boolean showPortrait = Display.getInstance().isPortrait();
    // if we don't have enough space then disregard device orientation
    if (showPortrait) {
        if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
            showPortrait = false;
        }
    } else {
        if (availableHeight / 2 > availableWidth - rect.getWidth()) {
            showPortrait = true;
        }
    }
    if (showPortrait) {
        if (width < availableWidth) {
            int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealX > 0) {
                // if the idealX is too far to the right just align to the right
                if (idealX + width > availableWidth) {
                    x = availableWidth - width;
                } else {
                    x = idealX;
                }
            }
        }
        if (rect.getY() < availableHeight / 2) {
            // popup downwards
            y = rect.getY();
            int height = Math.min(prefHeight, availableHeight - y);
            show(y, Math.max(0, availableHeight - height - y), x, Math.max(0, availableWidth - width - x));
        } else {
            // popup upwards
            int height = Math.min(prefHeight, rect.getY() - getLayeredPane(f).getAbsoluteY());
            y = rect.getY() - height - getLayeredPane(f).getAbsoluteY();
            show(y, Math.max(0, getLayeredPane(f).getComponentForm().getHeight() - rect.getY()), x, Math.max(0, availableWidth - width - x));
        }
    } else {
        int height = Math.min(prefHeight, availableHeight);
        if (height < availableHeight) {
            int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealY > 0) {
                // if the idealY is too far up just align to the top
                if (idealY + height > availableHeight) {
                    y = availableHeight - height;
                } else {
                    y = idealY;
                }
            }
        }
        if (prefWidth > rect.getX()) {
            // popup right
            x = rect.getX() + rect.getSize().getWidth();
            if (x + prefWidth > availableWidth) {
                x = availableWidth - prefWidth;
            }
            width = Math.min(prefWidth, availableWidth - x);
            show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
        } else {
            // popup left
            width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
            x = rect.getX() - width;
            show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
        }
    }
/*if(restoreArrow) {
            contentPaneStyle.getBorder().clearImageBorderSpecialTile();
        }*/
}
Also used : Form(com.codename1.ui.Form) Label(com.codename1.ui.Label) UIManager(com.codename1.ui.plaf.UIManager) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component) Image(com.codename1.ui.Image) Border(com.codename1.ui.plaf.Border)

Aggregations

Point (com.codename1.ui.geom.Point)11 Point (com.codename1.charts.models.Point)10 Coord (com.codename1.maps.Coord)9 Paint (com.codename1.charts.compat.Paint)7 Component (com.codename1.ui.Component)5 TextArea (com.codename1.ui.TextArea)3 Dimension (com.codename1.ui.geom.Dimension)3 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Rectangle (com.codename1.ui.geom.Rectangle)3 Border (com.codename1.ui.plaf.Border)3 ArrayList (java.util.ArrayList)3 SeriesSelection (com.codename1.charts.models.SeriesSelection)2 Container (com.codename1.ui.Container)2 EncodedImage (com.codename1.ui.EncodedImage)2 Form (com.codename1.ui.Form)2 Image (com.codename1.ui.Image)2 Label (com.codename1.ui.Label)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 Style (com.codename1.ui.plaf.Style)2