カスタム レンダラーが既にあるので、メソッドにフックして、次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))
}
次に、 draw メソッドは次のように分岐します。
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)
}