2013-10-27 86 views
0

的「跨」 CategoryMarker我有一個箱形圖(CategoryPlot) with a custom BoxAndWhiskerRenderer`了一系列箱形成的類別。我想,以紀念過渡從一類到另一個垂直線。渲染中的JFreeChart

例如在下面的圖中,線痕的第六類,但是我希望它出現之間的第五和第六類別:

enter image description here

val mark = new CategoryMarker(splitBin) 
    mark.setDrawAsLine(true) 
    plot.addDomainMarker(mark, Layer.BACKGROUND) 

什麼是最好的/最簡單的方法來實現這一目標?

回答

0

因爲我已經有了一個自定義渲染,我迷上到drawDomainMarker方法,檢查自定義子類型的CategoryMarker的存在:

class LeftCategoryMarker(key: Comparable[_], paint: Paint, stroke: Stroke) 
    extends CategoryMarker(key, paint, stroke) { 

    def this(key: Comparable[_]) = this(key, Color.gray, new BasicStroke(1.0f)) 
} 

然後抽籤方法轉移:

override def drawDomainMarker(g2: Graphics2D, plot: CategoryPlot, axis: CategoryAxis, 
           marker: CategoryMarker, dataArea: Rectangle2D): Unit = 
    marker match { 
    case _: LeftCategoryMarker => drawLeftMarker(g2, plot, axis, marker, dataArea) 
    case _ => super.drawDomainMarker(g2, plot, axis, marker, dataArea) 
    } 

private def drawLeftMarker(g2: Graphics2D, plot: CategoryPlot, axis: CategoryAxis, 
          marker: CategoryMarker, dataArea: Rectangle2D): Unit = { 
    val cat  = marker.getKey 
    val dataset = plot.getDataset(plot.getIndexOf(this)) 
    val catIdx = dataset.getColumnIndex(cat) 
    if (catIdx < 0) return 

    val savedComposite = g2.getComposite 
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, marker.getAlpha)) 

    axis.getCategoryMargin 
    val numCat  = dataset.getColumnCount 
    val domainEdge = plot.getDomainAxisEdge 
    val left  = axis.getCategoryStart (catIdx, numCat, dataArea, domainEdge) 
    val gap   = calculateCategoryGapSize(axis , numCat, dataArea, domainEdge) 
    val v   = left - gap/2 
    val line  = if (plot.getOrientation == PlotOrientation.HORIZONTAL) 
    new Line2D.Double(dataArea.getMinX, v, dataArea.getMaxX, v) 
    else 
    new Line2D.Double(v, dataArea.getMinY, v, dataArea.getMaxY) 

    g2.setPaint (marker.getPaint) 
    g2.setStroke(marker.getStroke) 
    g2.draw(line) 

    g2.setComposite(savedComposite) 
} 

並且以下受保護的方法從CategoryAxis需要計算的間隙:

private def calculateCategoryGapSize(axis: CategoryAxis, categoryCount: Int, 
            area: Rectangle2D, e: RectangleEdge): Double = { 
    if (categoryCount == 0) return 0.0 

    val available = if ((e == RectangleEdge.TOP) || (e == RectangleEdge.BOTTOM)) 
    area.getWidth 
    else if ((e == RectangleEdge.LEFT) || (e == RectangleEdge.RIGHT)) 
    area.getHeight 
    else 
    0.0 

    available * axis.getCategoryMargin/(categoryCount - 1) 
} 

enter image description here

+0

(圖像顯示第四和第五類別之間的線;這是故意的;使用的標記是「新的LeftCategoryMarker(4)') –