2016-10-04 55 views

回答

0

(这个答案解决了绘制各个点的内圈的问题,如上图所示)

通常情况下,这将是一个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); 

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

+0

谢谢尼克,我会试试这个。 –

+0

Nick,我尝试了第二种解决方案,但在MyLineAndPointFormatter super(context,xmlcfg)的构造函数中出错;同样在@override public SeriesRenderer doGetRendererInstance(XYPlot plot)中也出现“无法解析符号”{ return new MyLineAndPointRenderer(plot); }“方法不会覆盖超类的方法” –

+0

道歉 - 当我写这个时,我在开发分支上。该方法的正确名称是'SeriesRenderer getRendererInstance(XYPlot plot)'。我已经更新了上面的代码来反映这一点。 – Nick