Search in sources :

Example 6 with XYChart

use of com.codename1.charts.views.XYChart in project CodenameOne by codenameone.

the class ChartComponent method pointerDragged.

@Override
public void pointerDragged(int[] x, int[] y) {
    if (x.length > 1) {
        if (!zoomEnabled) {
            super.pointerDragged(x, y);
            return;
        }
        // Pinch zoom
        if (chart instanceof XYChart) {
            XYChart xyChart = (XYChart) chart;
            double[] panLimits = xyChart.getRenderer().getPanLimits();
            if (zoomStart == null) {
                zoomStart = new Point((x[0] + x[1]) / 2, (y[0] + y[1]) / 2);
                zoomDistStartX = Math.abs(x[0] - x[1]);
                zoomDistStartY = Math.abs(y[0] - y[1]);
                zoomStartBBox = getBBox();
                if (initialZoomBBox == null) {
                    initialZoomBBox = zoomStartBBox.translateScreenCoords(0, 0);
                }
            } else {
                int dx = Math.abs(x[0] - x[1]);
                int dy = Math.abs(y[0] - y[1]);
                if (dx == 0)
                    dx = 1;
                if (dy == 0)
                    dy = 1;
                double zoomX = zoomDistStartX / dx;
                double zoomY = zoomDistStartY / dy;
                BBox newBounds = zoomStartBBox.scaleScreenCoords((float) zoomX, (float) zoomY);
                double minX = newBounds.minX;
                double minY = newBounds.minY;
                double maxX = newBounds.maxX;
                double maxY = newBounds.maxY;
                if (panLimits != null) {
                    // Make sure that this zoom doesn't exceed the maxZoomIn value
                    if (minX < panLimits[0]) {
                        maxX = maxX + panLimits[0] - minX;
                        minX = panLimits[0];
                    }
                    if (minY < panLimits[2]) {
                        maxY = maxY + panLimits[2] - minY;
                        minY = panLimits[2];
                    }
                    if (maxX > panLimits[1]) {
                        minX = minX + panLimits[1] - maxX;
                        maxX = panLimits[1];
                    }
                    if (maxY > panLimits[3]) {
                        minY = minY + panLimits[3] - maxY;
                        maxY = panLimits[3];
                    }
                }
                double[] zoomLimits = xyChart.getRenderer().getZoomLimits();
                if (zoomLimits != null && zoomLimits[0] != 0) {
                    if (maxX - minX < zoomLimits[0]) {
                        maxX = xyChart.getRenderer().getXAxisMax();
                        minX = xyChart.getRenderer().getXAxisMin();
                    }
                }
                if (zoomLimits != null && zoomLimits[1] != 0) {
                    if (maxX - minX > zoomLimits[1]) {
                        maxX = xyChart.getRenderer().getXAxisMax();
                        minX = xyChart.getRenderer().getXAxisMin();
                    }
                }
                if (zoomLimits != null && zoomLimits[2] != 0) {
                    if (maxY - minY < zoomLimits[2]) {
                        maxY = xyChart.getRenderer().getYAxisMax();
                        minY = xyChart.getRenderer().getYAxisMin();
                    }
                }
                if (zoomLimits != null && zoomLimits[3] != 0) {
                    if (maxY - minY > zoomLimits[3]) {
                        maxY = xyChart.getRenderer().getYAxisMax();
                        minY = xyChart.getRenderer().getYAxisMin();
                    }
                }
                if (!xyChart.getRenderer().isZoomXEnabled()) {
                    minX = xyChart.getRenderer().getXAxisMin();
                    maxX = xyChart.getRenderer().getXAxisMax();
                }
                if (!xyChart.getRenderer().isZoomYEnabled()) {
                    minY = xyChart.getRenderer().getYAxisMin();
                    maxY = xyChart.getRenderer().getYAxisMax();
                }
                xyChart.getRenderer().setRange(new double[] { minX, maxX, minY, maxY });
                chartBoundsChanged();
                this.repaint();
            }
        } else {
            if (zoomStart == null) {
                zoomStart = new Point((x[0] + x[1]) / 2, (y[0] + y[1]) / 2);
                zoomTransformStart = Transform.makeIdentity();
                if (transform != null) {
                    zoomTransformStart.concatenate(transform);
                }
                int dx = Math.abs(x[0] - x[1]) / 2;
                int dy = Math.abs(y[0] - y[1]) / 2;
                zoomDistStart = Math.sqrt(dx * dx + dy * dy);
            } else {
                int dx = Math.abs(x[0] - x[1]) / 2;
                int dy = Math.abs(y[0] - y[1]) / 2;
                double zoomDist = Math.sqrt(dx * dx + dy * dy);
                if (zoomDist == 0) {
                    zoomDist = 1;
                }
                transform = Transform.makeIdentity();
                transform.translate(zoomStart.getX(), zoomStart.getY());
                transform.scale((float) (zoomDist / zoomDistStart), (float) (zoomDist / zoomDistStart));
                transform.translate(-zoomStart.getX(), -zoomStart.getY());
                transform.concatenate(zoomTransformStart);
                this.repaint();
            }
        }
    } else {
        if (!isPanEnabled()) {
            super.pointerDragged(x, y);
            return;
        }
        if (chart instanceof XYChart) {
            XYChart xyChart = (XYChart) chart;
            double[] panLimits = xyChart.getRenderer().getPanLimits();
            if (dragStartBBox == null) {
                dragStart = new Point(x[0], y[0]);
                dragStartBBox = getBBox();
            } else {
                float tx = x[0] - dragStart.getX();
                float ty = y[0] - dragStart.getY();
                BBox newBounds = dragStartBBox.translateScreenCoords(-tx, ty);
                double minX = newBounds.minX;
                double minY = newBounds.minY;
                double maxX = newBounds.maxX;
                double maxY = newBounds.maxY;
                if (panLimits != null) {
                    if (minX < panLimits[0]) {
                        maxX = maxX + panLimits[0] - minX;
                        minX = panLimits[0];
                    }
                    if (minY < panLimits[2]) {
                        maxY = maxY + panLimits[2] - minY;
                        minY = panLimits[2];
                    }
                    if (maxX > panLimits[1]) {
                        minX = minX + panLimits[1] - maxX;
                        maxX = panLimits[1];
                    }
                    if (maxY > panLimits[3]) {
                        minY = minY + panLimits[3] - maxY;
                        maxY = panLimits[3];
                    }
                }
                if (!xyChart.getRenderer().isPanXEnabled()) {
                    minX = xyChart.getRenderer().getXAxisMin();
                    maxX = xyChart.getRenderer().getXAxisMax();
                }
                if (!xyChart.getRenderer().isPanYEnabled()) {
                    minY = xyChart.getRenderer().getYAxisMin();
                    maxY = xyChart.getRenderer().getYAxisMax();
                }
                xyChart.getRenderer().setRange(new double[] { minX, maxX, minY, maxY });
                chartBoundsChanged();
            }
        } else {
            if (dragStart == null) {
                dragStart = new Point(x[0], y[0]);
                dragTransformStart = Transform.makeIdentity();
                if (transform != null) {
                    dragTransformStart.concatenate(transform);
                }
            } else {
                transform = Transform.makeIdentity();
                transform.translate(x[0] - dragStart.getX(), y[0] - dragStart.getY());
                transform.concatenate(dragTransformStart);
                this.repaint();
            }
        }
    }
    // To change body of generated methods, choose Tools | Templates.
    super.pointerDragged(x, y);
}
Also used : XYChart(com.codename1.charts.views.XYChart) Point(com.codename1.charts.models.Point) Point(com.codename1.charts.models.Point)

Example 7 with XYChart

use of com.codename1.charts.views.XYChart in project CodenameOne by codenameone.

the class ChartComponent method getBBox.

private BBox getBBox() {
    if (chart instanceof XYChart) {
        XYChart xyChart = (XYChart) chart;
        double minX = xyChart.getRenderer().getXAxisMin();
        double maxX = xyChart.getRenderer().getXAxisMax();
        double minY = xyChart.getRenderer().getYAxisMin();
        double maxY = xyChart.getRenderer().getYAxisMax();
        return getBBox(minX, maxX, minY, maxY, getAbsoluteX() + getStyle().getPaddingLeft(false) + xyChart.getRenderer().getMargins()[1], getAbsoluteY() + getStyle().getPaddingTop() + xyChart.getRenderer().getMargins()[0], getAbsoluteX() + getWidth() - getStyle().getPaddingRight(false) - xyChart.getRenderer().getMargins()[3], getAbsoluteY() + getHeight() - getStyle().getPaddingBottom() - xyChart.getRenderer().getMargins()[2]);
    }
    return null;
}
Also used : XYChart(com.codename1.charts.views.XYChart)

Example 8 with XYChart

use of com.codename1.charts.views.XYChart in project CodenameOne by codenameone.

the class ChartComponent method setPanEnabled.

/**
 * @param panEnabled the panEnabled to set
 */
public void setPanEnabled(boolean panEnabled) {
    this.panEnabled = panEnabled;
    if (chart instanceof XYChart) {
        XYChart xyChart = (XYChart) chart;
        xyChart.getRenderer().setPanEnabled(panEnabled);
    }
}
Also used : XYChart(com.codename1.charts.views.XYChart)

Aggregations

XYChart (com.codename1.charts.views.XYChart)7 Paint (com.codename1.charts.compat.Paint)1 Point (com.codename1.charts.models.Point)1 XYMultipleSeriesRenderer (com.codename1.charts.renderers.XYMultipleSeriesRenderer)1 Rectangle (com.codename1.ui.geom.Rectangle)1 Shape (com.codename1.ui.geom.Shape)1