因爲我已經有了一個自定義渲染,我迷上到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)
}
(圖像顯示第四和第五類別之間的線;這是故意的;使用的標記是「新的LeftCategoryMarker(4)') –