在 android 图中自定义标签和点

Customizing Label and points in android plot

我正在使用 android plot 开发应用程序,我正在尝试美化它,我希望它像图片中那样

但我越来越像下面

(这个答案解决了为上面的示例图片中的各个点绘制内圆)

通常这是一个 2 或 3 行的工作,只需重新添加具有正确配置的 LineAndPointFormatter 实例的系列就可以解决问题,但最近的版本中存在一个错误,该错误会阻止同一系列被添加超过一旦使用相同的渲染器,无论是否提供不同的格式化程序。 (将在 1.2.3 版本中修复)

如果您不介意拥有系列数据的完整副本,您仍然可以相当轻松地做到这一点:

        XYSeries series2b = new SimpleXYSeries(
                Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");

        LineAndPointFormatter series2bFormat = new LineAndPointFormatter(null, Color.WHITE, null, null);

        // this adjusts the size of the inner circle:
        series2bFormat.getVertexPaint().setStrokeWidth(PixelUtils.dpToPix(7));

        ...

        // make sure this line comes AFTER the addSeries call for the copied series:
        plot.addSeries(series2b, series2bFormat);

上述实现的一个限制是它会绘制一个重复的图例图标(也将在即将发布的 1.2.3 版本中解决)。避免重复图标的另一种方法是自定义扩展 LineAndPointRenderer 以及自定义 LineAndPointFormatter:

    // a custom renderer to draw an second smaller circle for each rendered vertex
    static class MyLineAndPointRenderer extends LineAndPointRenderer<MyLineAndPointFormatter> {


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

        @Override
        protected void renderPoints(Canvas canvas, RectF plotArea, XYSeries series, List<PointF> points,
                LineAndPointFormatter formatter) {
            // draw points as normal:
            super.renderPoints(canvas, plotArea, series, points, formatter);

            // now draw our inner vertices on top of the previously drawn vertices:
            for (PointF p : points) {
                Paint paint = ((MyLineAndPointFormatter) formatter).getInnerPointPaint();
                canvas.drawPoint(p.x, p.y, paint);
            }
        }
    }

    // defines the format for our custom renderer:
    static class MyLineAndPointFormatter extends LineAndPointFormatter {

        Paint innerPointPaint = new Paint();

        {
            // setup innPointPaint to draw a white circle a little smaller than the 
            // typical circle drawn for each vertex:
            innerPointPaint.setAntiAlias(true);
            innerPointPaint.setStrokeCap(Paint.Cap.ROUND);
            innerPointPaint.setColor(Color.WHITE);
            innerPointPaint.setStrokeWidth(PixelUtils.dpToPix(7));
        }

        public MyLineAndPointFormatter(Context context, int xmlCfgId) {
            super(context, xmlCfgId);
        }

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

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

        public Paint getInnerPointPaint() {
            return innerPointPaint;
        }
    }

然后,在将系列添加到图中时使用上面的而不是 LineAndPointFormatter:

MyLineAndPointFormatter series1Format =
                new MyLineAndPointFormatter(this, R.xml.my_format);

虽然最后一种方法代码较多,但也是最有效和可扩展的。