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.
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.
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)).
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.