2015-07-02 27 views
1

背景如何AndroidPlot

我正在開發,使用AndroidPlot地塊數據作爲線圖的Android顯示X和Y軸爲XYPlot。由於數據的性質,重要的是它是可移動的和可縮放的。我在bitbucket上使用AndroidPlot的示例代碼panning and zooming,修改後允許在X和Y方向上進行平移和縮放。

除了沒有X軸和Y軸線之外,一切都按需要工作。如果沒有它們,查看數據會非常迷茫。網格有幫助,但不能保證網格線實際上會落在軸上。

爲了彌補這一點,我嘗試添加兩個系列,一個落在X軸上,另一個落在Y上。問題在於,如果縮小得太遠,軸簡單地結束,並且變得明顯我已經應用了'黑客'。

問題

是否有可能加入的X和Y軸的線AndroidPlot?或者我的悲傷黑客必須做什麼?

編輯 添加標籤

回答

0

我想通了。這不是微不足道的,與合作者共同努力,並吸了我們許多小時的時間。

從我的問題中提到的示例開始,我必須擴展XYPlot(我稱之爲GraphView)並覆蓋onPreInit方法。請注意,我有兩個PointF's,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更新中斷時...

+0

我目前面臨完全相同的問題。這是你最終使用的解決方案,還是以另一種方式解決? – Joakim

+0

不幸的是,這是我一起去的。如果你發現更好的東西,我很想知道! –