Bar chart with crosshatch pattern

Would anyone happen to have an example of a method for displaying a crosshatch pattern in a bar of a bar chart? I couldn’t find a way to configure this in the designer short of an extension function, but I didn’t have any luck finding anything useful in the JFreeChart API docs. I see this might be possible using java.awt.TexturePaint, but I haven’t had success with this.

Any help or tips would be much appreciated!

This might get you started.

Thanks @PGriffith. I was able to color the bars through configureChart, but I’m having trouble with the TexturePaint class. The API docs show that renderer.setSeriesPaint() accepts a TexturePaint object as a color, but when I am assigning the TexturePaint to the the series color, the chart is not rendering the pattern.

Can you please take a look at my code and let me know where I’m going wrong with this (series 1 is what I’m having trouble with, series 0 paint is working as expected)?


	from java.awt import Color, Graphics2D, TexturePaint
	from java.awt.image import BufferedImage
	from java.awt.geom import Rectangle2D
	plot = chart.getCategoryPlot()
	renderer = plot.getRenderer(1)
	ds = chart.getPlot().getDataset(1)
	hourVal = self.Data.getValueAt(0,1)
	targetVal = self.Data.getValueAt(0,2)
	#paint actual bar color based on performance to target
	plot = chart.getCategoryPlot()
	renderer = plot.getRenderer(1)
	ds = chart.getPlot().getDataset(1)
	otsVal = self.Data.getValueAt(0,1)
	targetVal = self.Data.getValueAt(0,2)

	if hourVal >= targetVal:
		renderer.setSeriesPaint(0, Color.GREEN)
		renderer.setSeriesPaint(0, Color.RED)

	#paint series 1 with textured image
	bufferedImage = BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB)
	g2 = bufferedImage.createGraphics()
	g2.fillRect(0, 0, 5, 5)
	g2.drawLine(0, 0, 5, 5)
	g2.drawLine(5, 5, 0, 0)
	g2.drawLine(0, 5, 5, 0)
	rect = Rectangle2D.Double(0, 0, 5, 5)
	texturePaint = TexturePaint(bufferedImage, rect)
	renderer.setSeriesPaint(1, texturePaint)

Does setting a solid color to series 1 work as expected? That is - is the issue with series 0 vs 1, or is the issue with your TexturePaint creation? If it’s the former, then do you have multiple subplots on your chart? JFreeChart requires you to step through subplots manually, which may be affecting things.

Yes, the solid color works as expected. The issue is only with the TexturePaint creation. There are not multiple subplots being used.

Sorry for delayed response, I thought I might be able to respond to the email I got. Thanks