2014-06-18 45 views

回答

5

我相信底部圖像使用自定義的LineAndPointRenderer,這也是你需要用來重現最上面的圖像。

下面是如何做到這一點的一個快速和骯髒的例子。首先創建一個自定義格式將保存所需的新格式值:

/** 
    * 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()); 
        } 
       } 
      } 
     } 
    } 

最後,用在你的活動,這些新作品:

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

下面是它與SimpleXYPlot示例一起使用時的外觀:

enter image description here

它可以通過加粗線條,選擇更好的背景顏色等更漂亮,但你明白了。

+0

它工作得很好。正是我需要的。謝謝。 – Zkart