Bar chart (report) static line

So, yes, JFreeChart.getPlot() returns an instance of the Plot interface - at the top of the Javadoc for the interface, you can see the known subclasses:

Direct Known Subclasses:
CategoryPlot, CompassPlot, DialPlot, FastScatterPlot, MeterPlot, MultiplePiePlot, PiePlot, PolarPlot, SpiderWebPlot, ThermometerPlot, WaferMapPlot, XYPlot

If you drill into the XYPlot - the most common type of plot you'll see in Ignition, aside from CategoryPlots, which back pie charts and bar charts. XYPlot, meanwhile, lists its direct known subclasses:

Direct Known Subclasses:
CombinedDomainXYPlot, CombinedRangeXYPlot

And then you can see that the CombinedDomainXYPlot has the getSubplots() method.

Or, you can 'back into' things from the other direction:
print type(event.source.chart.plot), which should give you the full classname: org.jfree.chart.plot.CombinedDomainXYPlot, which you can then just look up directly via Google/etc.

The reverse-engineering approach is made a lot more palatable/powerful via the use of @pturmel's inspect.py module, which you can find posted on the forums here.

2 Likes

Thanks for the in-depth response! I think that will help a lot for future tracing. Especially the reminder to use print type(). I think i’ve had a play with the inspect.py module, maybe i’ll have to get more familiar.
It mainly seems to be dealing with Charts that requires more digging into Java than anything else.

Hi,

I hope you can help.

I have used a similar script and it works. the other problem that I have is this only add the marker on the default axis (left axis). i need to put a “marker” on the second axis as well. is there a way of setting it on the second axis (right axis)?

@Lionel_Maree
If you are asking for vertical markings, this can be accomplished by using “addDomainMarker” instead of “addRangeMarker”

Example:

from org.jfree.chart.plot import CategoryMarker;
from org.jfree.ui import Layer
marker = CategoryMarker("Mar")
marker.setPaint(system.gui.color(10, 10, 10, 40))
event.source.parent.getComponent('Bar Chart').chart.plot.addDomainMarker(marker, Layer.BACKGROUND)

The preceding code produces the following result:
Sample Code 2

1 Like

I must probably clarify that I’m using a time series chart instead of a bar chart but should not make a difference or that much of a difference. :slight_smile:

I don’t want vertical lines. the Filtrate turb (Upper) is correct which is referencing to the left y-axis. only thing that I want extra is the Turb Removal Baseline must use the the right y-axis as reference because the range or scaling will be different on each y-axis.
Capture345353

I was able to accomplish this using an intermediary ratio in the script to translate the coordinates from the left to the right. Here is the sample code:

from org.jfree.chart.plot import IntervalMarker
rightAxisLowSetPoint = 40
rightAxisHighSetPoint = 60
leftAxisMin = 0
leftAxisMax = 100
rightAxisMin = 20
rightAxisMax = 80
translatedLowSetPoint = (rightAxisLowSetPoint*leftAxisMax)/rightAxisMax
translatedHighSetPoint = (rightAxisHighSetPoint*leftAxisMax)/rightAxisMax
event.source.parent.getComponent('Chart').chart.plot.addRangeMarker(IntervalMarker(rightAxisLowSetPoint, rightAxisHighSetPoint, system.gui.color(20, 10, 10, 40)))

This is the sample result:

1 Like

I want to do a similar shading, except across a date x-axis of an XY Plot, based on the start/end dates of a date slider. I've tried by CategoryMarker and Marker with no luck:

from org.jfree.chart.plot import Marker;
from org.jfree.ui import Layer

sDate = self.parent.getComponent('Date Range').startDate
eDate = self.parent.getComponent('Date Range').endDate
marker = Marker(startDate, endDate)
marker.setPaint(system.gui.color(10, 10, 10, 40))

chart = event.source.parent.getComponent("Chart").getChart()
chart.getPlot().addDomainMarker(marker, Layer.BACKGROUND)

I see a few things that need to be corrected in your code in order to accomplish this.
• sDate and eDate are assigned date values, but are never used. Instead when the marker is constructed, two unassigned variables startDate and endDate are used.
• For this, I believe that the marker you want is actually IntervalMarker, and I don't believe that the ui layer is needed
• I don't usually see event.source and self in the same script unless this is being fired from a custom method and an event is passed in.
• Also, for an xyPlot, the dates need to be converted to millis

Other than those minor things, the code should run.

Here is the corrected code:

from org.jfree.chart.plot import IntervalMarker
chart = event.source.parent.getComponent("Chart").getChart()#changed self to event.source (if this change was backwards, then reverse it for all instances)
dateRange = event.source.parent.getComponent('Date Range')
startDate = dateRange.startDate.getTime()#changed startDate and endDate to match what's being used to construct the marker
endDate = dateRange.endDate.getTime()#added getTime to convert date to millis
marker = IntervalMarker(startDate, endDate)#switched plot marker to interval marker
marker.setPaint(system.gui.color(10, 10, 10, 40))
chart.plot.addDomainMarker(marker)#Removed layer specification
2 Likes

Yep, you fixed it for me. Thanks so much!

1 Like

Is it possible to add multiple target lines in the bar chart?

I don't know, but probably. Have you tried?

@PGriffith I have tried to add two markers with the code you provided above, but it was not working. I am also not familiar with this.

"Not working" is pretty vague.
Was an error being thrown? Maybe quietly, in the output console?
Was only one of the added markers showing up? If so, which one - the first one you added, or the second?
As the rest of this thread gets into - do you have multiple subplots? Is this a chart in Vision, or Reporting?
What's the exact code you were using to try to add two markers?

Help me help you. The more information you provide, the better.

@PGriffith ,Sorry for the lack of information.

I don't see any error in the console. Dont have subplots.

here is the code that I implemented:

	from org.jfree.chart.plot import ValueMarker
	marker = ValueMarker(0) #point on range axis to label
	label  = 'Avg: '+str(data['average'])[0:5]
	marker.setLabel(label)
	chart.getPlot().addRangeMarker(marker)
	
	marker1 = ValueMarker(-0.1) #point on range axis to label
	label  = 'Avg: '+str(data['average'])[0:5]
	marker1.setLabel(label)
	chart.getPlot().addRangeMarker(marker1)

I am using the bar chart in the report module. Only the first one showed up in the chart.

Because if this is you're exact code you're only adding marker to the chart, you just do so twice.

the last line should be:

chart.getPlot().addRangeMarker(marker1)

I was doing some other tests and forgot to change it back. Changing the second one to marker1 actually gave me the same result.

Are you sure there's no errors in the console window?
configureChart was silently failing with your exact script because I didn't have a data key named average. Once I changed those lines, it worked:
image

def configureChart(data, chart):
	from org.jfree.chart.plot import ValueMarker
	marker = ValueMarker(4) #point on range axis to label
	label  = 'Avg:'
	marker.setLabel(label)
	chart.getPlot().addRangeMarker(marker)
	
	marker1 = ValueMarker(8) #point on range axis to label
	label  = 'Avg:'
	marker1.setLabel(label)
	chart.getPlot().addRangeMarker(marker1)

It is now working for me as well. I am not exactly sure what I messed up. But appreciate the help!

when i add the color parameter the line is not showing, any reasons why? this is on reporting btw

def configureChart(data, chart):
from org.jfree.chart.plot import ValueMarker
from java.awt import Color
red = Color(255, 10, 10)

marker = ValueMarker(0,red) #point on range axis to label
marker.setLabel("")
chart.getPlot().addRangeMarker(marker)