Chart Component Blank Space

I am trying to learn JFreeChart within the Cart component in Ignition. I have figured out most of what i wanted but i can’t seem to get the last piece of the puzzle. I am trying to remove the space between the top of the component and the beginning of the chart.image

I am using the following code to remove the space from the left and right sides but it doesn’t work for top and bottom.

	from org.jfree.ui import RectangleInsets	
	plot = chart.getPlot()
	plot.setInsets(RectangleInsets(0, 0, 0, 0))

Any suggestions would be greatly appreciated.

Try setPadding() with your RectangleInsets.

When I change “setInsets” to “setPadding” the script doesn’t work. Here is the entire code that i put on the “configureChart” extension function.

	from org.jfree.chart.renderer.category import StandardBarPainter	
	plot = chart.getPlot()
	renderer = plot.getRenderer()
	renderer.setBarPainter(StandardBarPainter())
	renderer.setShadowVisible(0)
	from java.awt import Color
	from java.awt import Font
	from org.jfree.chart.labels import StandardCategoryItemLabelGenerator
	from org.jfree.chart.labels import ItemLabelPosition
	from org.jfree.chart.labels import ItemLabelAnchor
	from org.jfree.ui import TextAnchor
	from org.jfree.ui import RectangleInsets	
	class myLabelGenerator(StandardCategoryItemLabelGenerator):
		def generateLabel(self,dataset,series, category):	
			value = dataset.getValue(series,category)	
			if value == 0:
				return None
			else:
				return str(int(value))
	plot = chart.getPlot()
	plot.setOutlinePaint(None)
	plot.setInsets(RectangleInsets(0, 0, 0, 0))
	renderer = plot.getRenderer()
	renderer.setBaseItemLabelFont(Font("Dialog", Font.BOLD, 16))
	renderer.setBaseItemLabelPaint(Color.WHITE)
	renderer.setItemLabelGenerator(myLabelGenerator())
#	renderer.setSeriesItemLabelPaint(1, Color.WHITE)
	renderer.setItemLabelsVisible(True)
	renderer.setBasePositiveItemLabelPosition(ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER))

On a side note, as i am still new to the JFreeChart stuff feel free to comment on anything in the code that you might think could be done better. I have just been finding and adapting examples.

Hmm, setPadding might be from a newer version of JFreeChart than we’re bundling. Not sure why it’s not working - might well be a JFreeChart bug, to be honest.

Also, your code looks fine, although you can improve the readability a bit by organizing things to keep imports at the top, then class definitions, then the actual code:

from java.awt import Color, Font
from org.jfree.chart.labels import ItemLabelAnchor, ItemLabelPosition, StandardCategoryItemLabelGenerator
from org.jfree.chart.renderer.category import StandardBarPainter
from org.jfree.ui import RectangleInsets, TextAnchor

class myLabelGenerator(StandardCategoryItemLabelGenerator):
    def generateLabel(self, dataset, series, category):
        value = dataset.getValue(series,category)
        if value == 0:
            return None
        else:
            return str(int(value))

plot = chart.getPlot()
renderer = plot.getRenderer()
renderer.setBarPainter(StandardBarPainter())
renderer.setShadowVisible(0)
plot.setOutlinePaint(None)
plot.setInsets(RectangleInsets(0, 0, 0, 0))
renderer = plot.getRenderer()
renderer.setBaseItemLabelFont(Font("Dialog", Font.BOLD, 16))
renderer.setBaseItemLabelPaint(Color.WHITE)
renderer.setItemLabelGenerator(myLabelGenerator())
#	renderer.setSeriesItemLabelPaint(1, Color.WHITE)
renderer.setItemLabelsVisible(True)
renderer.setBasePositiveItemLabelPosition(ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER))

yeah, once i get it working i will pretty up the code and comment it. also i just noticed that setPadding is not in the list of methods for the plot class. Would i need to convert it to a JFreeChart class?

No - the setPadding method is on the root chart object (which is an instance of the JFreeChart class) - you run setPadding directly on the chart. The setInsets method is on the root Plot class, which is the root interface you get when you call chart.getPlot(). Try just chart.setPadding(RectangleInsets(0,0,0,0)).

I tried that change with no luck. i also commented everything else out in case something was over ridding it. but that didnt work.

Here is the template if it helps.

Jfreechart_question.proj (7.0 KB)

The classic Chart component is a subclass of the JFreeChart ChartPanel. You might want to call the self.setInsets() method.

Thanks for the suggestion. I tried adding,

self.setInsets(RectangleInsets(0, 0, 0, 0))

and

self.setPadding(RectangleInsets(0, 0, 0, 0))

but i get the same result as the chart.setPadding()

Have you tried setting zero margins on your plot’s range (domain?) axis?

1 Like

Yes. the margins are both set to 0. which is how i got the chart to go from left to right properly. however, there arent any similar settings for the other axis to fill out the area in that direction.

So i am going to work around the initial issue but i have found another.

I cant seem to get rid of the vertical dashed lines behind the bar. Is there an easy way to do this?

Found out how to manually set this in the extension function.

	domainAxis = plot.getDomainAxis()
	domainAxis.setUpperMargin(0)
	domainAxis.setLowerMargin(0)

It worked. Thank you.

2 Likes