AndroidPlot自定义关节

标签 android androidplot

我目前试图让我的情节看起来像这样:

desired

但我不知道如何自定义点以在橙色填充周围有黑色描边,所以我的点现在“粘”到线条上了。

actual

或者,至少如何使它看起来像这样(与线条颜色相同的外圆)。

androplot

有什么帮助吗?

最佳答案

我相信底部图像使用自定义 LineAndPointRenderer,这也是您需要用来重现最顶部图像的东西。

这里有一个简单粗暴的例子来说明如何做到这一点。首先创建一个自定义 Formatter,它将保存所需的新格式值:

    /**
     * A LineAndPointFormatter with the addition of paint to be used to "stroke" vertices.
     */
    class MyLineAndPointFormatter extends LineAndPointFormatter{
        private Paint strokePaint;

        /**
         * Some quick and dirty hard-coded params
         */
        public MyLineAndPointFormatter() {
            super(Color.RED, Color.RED, null, null);
            strokePaint = new Paint();
            strokePaint.setColor(Color.BLACK);
            strokePaint.setStrokeWidth(PixelUtils.dpToPix(2));
            strokePaint.setStyle(Paint.Style.STROKE);
            strokePaint.setAntiAlias(true);
        }

        public Paint getStrokePaint() {
            return strokePaint;
        }

        @Override
        public Class<? extends SeriesRenderer> getRendererClass() {
            return MyLineAndPointRenderer.class;
        }

        @Override
        public SeriesRenderer getRendererInstance(XYPlot plot) {
            return new MyLineAndPointRenderer(plot);
        }
    }

接下来,自定义渲染器:

    /**
     * A LineAndPointRenderer that can stroke vertices.
     */
    class MyLineAndPointRenderer extends LineAndPointRenderer<MyLineAndPointFormatter> {

        public MyLineAndPointRenderer(XYPlot plot) {
            super(plot);
        }

        /**
         * Overridden draw method to get the "vertex stroke" effect.  99% of this is copy/pasted from
         * the super class' implementation. 
         * @param canvas
         * @param plotArea
         * @param series
         * @param formatter
         */
        @Override
        protected void drawSeries(Canvas canvas, RectF plotArea, XYSeries series, LineAndPointFormatter formatter) {
            PointF thisPoint;
            PointF lastPoint = null;
            PointF firstPoint = null;
            Paint  linePaint = formatter.getLinePaint();

            Path path = null;
            ArrayList<Pair<PointF, Integer>> points = new ArrayList<Pair<PointF, Integer>>(series.size());
            for (int i = 0; i < series.size(); i++) {
                Number y = series.getY(i);
                Number x = series.getX(i);

                if (y != null && x != null) {
                    thisPoint = ValPixConverter.valToPix(
                            x,
                            y,
                            plotArea,
                            getPlot().getCalculatedMinX(),
                            getPlot().getCalculatedMaxX(),
                            getPlot().getCalculatedMinY(),
                            getPlot().getCalculatedMaxY());
                    points.add(new Pair<PointF, Integer>(thisPoint, i));
                } else {
                    thisPoint = null;
                }

                if(linePaint != null && thisPoint != null) {

                    // record the first point of the new Path
                    if(firstPoint == null) {
                        path = new Path();
                        firstPoint = thisPoint;
                        // create our first point at the bottom/x position so filling
                        // will look good
                        path.moveTo(firstPoint.x, firstPoint.y);
                    }

                    if(lastPoint != null) {
                        appendToPath(path, thisPoint, lastPoint);
                    }

                    lastPoint = thisPoint;
                } else {
                    if(lastPoint != null) {
                        renderPath(canvas, plotArea, path, firstPoint, lastPoint, formatter);
                    }
                    firstPoint = null;
                    lastPoint = null;
                }
            }
            if(linePaint != null && firstPoint != null) {
                renderPath(canvas, plotArea, path, firstPoint, lastPoint, formatter);
            }

            Paint vertexPaint = formatter.getVertexPaint();
            Paint strokePaint = ((MyLineAndPointFormatter)formatter).getStrokePaint();
            PointLabelFormatter plf = formatter.getPointLabelFormatter();
            if (vertexPaint != null || plf != null) {
                for (Pair<PointF, Integer> p : points) {
                    PointLabeler pointLabeler = formatter.getPointLabeler();

                    // if vertexPaint is available, draw vertex:
                    if(vertexPaint != null) {
                        canvas.drawPoint(p.first.x, p.first.y, vertexPaint);
                    }

                    // if stroke is available, draw stroke:
                    if(strokePaint != null) {
                        // you'll probably want to make the radius a configurable parameter
                        // instead of hard-coded like it is here.
                        canvas.drawCircle(p.first.x, p.first.y, 4, strokePaint);
                    }

                    // if textPaint and pointLabeler are available, draw point's text label:
                    if(plf != null && pointLabeler != null) {
                        canvas.drawText(pointLabeler.getLabel(series, p.second), p.first.x + plf.hOffset, p.first.y + plf.vOffset, plf.getTextPaint());
                    }
                }
            }
        }
    }

最后,要在您的 Activity 中使用这些新 fragment :

MyLineAndPointFormatter format = new MyLineAndPointFormatter();
plot.addSeries(series, format);

这是与 SimpleXYPlot 示例一起使用时的样子:

enter image description here

通过加厚线条、选择更好的背景颜色等可能会更漂亮,但你明白了。

关于AndroidPlot自定义关节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283978/

相关文章:

java - AndroidPlot 对数轴?

android - 如何在 androidplot 条形图中绘制不同颜色的条形图?

安卓图表: achartengine or AndroidPlot?

java - 如何在类之间共享变量?

android - 使用 dagger2 更改在应用程序类中初始化的改造 baseurl

java - Android 中线程如何与监听器交互?

AndroidPlot 1.x 更改范围标签格式

android - 在 ubuntu 中找不到 gradlew.bat

java.lang.NoClassDefFoundError : Failed resolution of: Lcom/google/firebase/firestore/QueryListenOptions;

java - 将自定义属性添加到现有 View