如何在 AndroidPlot 中显示 XYPlot 的 X 轴和 Y 轴

How to display X and Y axis for XYPlot in AndroidPlot

背景

我正在为 Android 开发一个应用程序,它使用 AndroidPlot 将数据绘制成折线图。由于数据的性质,它可以平移和缩放很重要。我在 bitbucket 上使用 AndroidPlot 的示例代码 panning and zooming,修改为允许在 X 和 Y 方向平移和缩放。

除了没有 X 和 Y 轴线外,一切正常。在没有它们的情况下查看数据是非常令人迷惑的。网格有帮助,但不能保证网格线实际上会落在轴上。

为了解决这个问题,我尝试添加两个系列,一个只落在 X 轴上,另一个落在 Y 轴上。问题是,如果一个缩小得太远,轴就会结束,它就变成了显然我已经应用了 'hack'.

问题

是否可以将 X 和 Y 轴线添加到 Android绘图中?还是我的悲伤黑客必须这样做?

编辑 添加标签

我明白了。这不是微不足道的,需要与合作者共同努力,并且占用了我们很多时间。

从我的问题中提到的示例开始,我必须扩展 XYPlot(我称之为 GraphView)并覆盖 onPreInit 方法。请注意,我有两个 PointF,minXY 和 maxXY,它们在我覆盖的 XYPlot 中定义并在我缩放或滚动时进行操作。

@Override
protected void onPreInit() {
    super.onPreInit();

    final Paint axisPaint = new Paint();
    axisPaint.setColor(getResources().getColor(R.color.MY_AXIS_COLOR));
    axisPaint.setStrokeWidth(3); //or whatever stroke width you want

    XYGraphWidget oldWidget = getGraphWidget();
    XYGraphWidget widget = new XYGraphWidget(getLayoutManager(),
            this,
            new SizeMetrics(
                    oldWidget.getHeightMetric(),
                    oldWidget.getWidthMetric())) {
        //We now override XYGraphWidget methods
        RectF mGridRect;
        @Override
        protected void doOnDraw(Canvas canvas, RectF widgetRect)
                throws PlotRenderException {
            //In order to draw the x axis, we must obtain gridRect. I believe this is the only
            //way to do so as the more convenient routes have private rather than protected access.
            mGridRect = new RectF(widgetRect.left + ((isRangeAxisLeft())?getRangeLabelWidth():1),
                    widgetRect.top + ((isDomainAxisBottom())?1:getDomainLabelWidth()),
                    widgetRect.right - ((isRangeAxisLeft())?1:getRangeLabelWidth()),
                    widgetRect.bottom - ((isDomainAxisBottom())?getDomainLabelWidth():1));
            super.doOnDraw(canvas, widgetRect);
        }
        @Override
        protected void drawGrid(Canvas canvas) {
            super.drawGrid(canvas);
            if(mGridRect == null) return;
            //minXY and maxXY are PointF's defined elsewhere. See my comment in the answer.
            if(minXY.y <= 0 && maxXY.y >= 0) { //Draw the x axis
                RectF paddedGridRect = getGridRect();
                //Note: GraphView.this is the extended XYPlot instance.
                XYStep rangeStep = XYStepCalculator.getStep(GraphView.this, XYAxisType.RANGE,
                        paddedGridRect, getCalculatedMinY().doubleValue(),
                        getCalculatedMaxY().doubleValue());
                double rangeOriginF = paddedGridRect.bottom;
                float yPix = (float) (rangeOriginF + getRangeOrigin().doubleValue() * rangeStep.getStepPix() /
                        rangeStep.getStepVal());
                //Keep things consistent with drawing y axis even though drawRangeTick is public
                //drawRangeTick(canvas, yPix, 0, getRangeLabelPaint(), axisPaint, true);
                canvas.drawLine(mGridRect.left, yPix, mGridRect.right, yPix, axisPaint);
            }
            if(minXY.x <= 0 && maxXY.x >= 0) { //Draw the y axis
                RectF paddedGridRect = getGridRect();
                XYStep domianStep = XYStepCalculator.getStep(GraphView.this, XYAxisType.DOMAIN,
                        paddedGridRect, getCalculatedMinX().doubleValue(),
                        getCalculatedMaxX().doubleValue());
                double domainOriginF = paddedGridRect.left;
                float xPix = (float) (domainOriginF - getDomainOrigin().doubleValue() * domianStep.getStepPix() /
                        domianStep.getStepVal());
                //Unfortunately, drawDomainTick has private access in XYGraphWidget
                canvas.drawLine(xPix, mGridRect.top, xPix, mGridRect.bottom, axisPaint);
            }
        }
    };
    widget.setBackgroundPaint(oldWidget.getBackgroundPaint());
    widget.setMarginTop(oldWidget.getMarginTop());
    widget.setMarginRight(oldWidget.getMarginRight());
    widget.setPositionMetrics(oldWidget.getPositionMetrics());
    getLayoutManager().remove(oldWidget);
    getLayoutManager().addToTop(widget);
    setGraphWidget(widget);
    //More customizations can go here
}

就是这样。我当然希望它内置于 AndroidPlot 中;当它在 AndroidPlot 更新中中断时试图修复它会很麻烦...