I was wondering if it is possible to plot (XYPlot
) a dataset in JFreeChart where the series contained in the dataset are plotted against separate y-axis ranges. The date is clustered by category/timestamp, e.g. if my dataset was this:
Timestamp Val1 Val2
2019-04-26 0.6 603
2019-04-25 2.1 1040
2019-04-24 4.1 255
It is impractical to plot both value series on the same range axis.
I've attempted extracting each series into its own dataset, so that I can call plot.mapDataSetToRangeAxis()
; but when I add multiple datasets to the plot, the bars tend to render on top of each other. Perhaps I'm missing something simple?
There are a few posts that address separate elements of what I'm looking for, but think I need something that combines these two:
- JFreeChart - XYBarChart Show Separate Bars for Each Series
- Setting different y-axis for two series with JFreeChart
Here is the python code I'm currently using—inside inductive automation/ignition's reporting module; they allow you configure the JFreeChart prior to rendering.
def configureChart(chart):
from org.jfree.chart.axis import DateAxis
from org.jfree.data.xy import XYSeries, XYSeriesCollection
from org.jfree.chart.renderer.xy import ClusteredXYBarRenderer
from java.awt import Color
from java.text import NumberFormat
class mins_to_str(NumberFormat):
def format(self,*args,**kwargs):
r = ''
number = args[0]
hrs = number//60
mins = number%60
r = '%02i:%02i' %(hrs,mins)
if len(args)>1:
toAppendTo = args[1]
pos = args[2].getField()
r = toAppendTo.insert(pos,r)
return r
plt = chart.getPlot()
renderer = ClusteredXYBarRenderer
xax = DateAxis()
plt.setDomainAxis(xax)
for i in range(plt.getDatasetCount()):
d = plt.getDataset(i)
dsc = XYSeriesCollection()
series = XYSeries(d.getSeriesKey(0))
print('SERIES [%s]' %series)
for r in range(d.getItemCount(0)):
xv = d.getXValue(0,r)
yv = d.getYValue(0,r)
print(' X: %s (%s)' %(xv,type(xv)))
print(' Y: %s (%s)' %(yv,type(yv)))
series.add(xv,yv)
dsc.addSeries(series)
plt.setDataset(i,dsc) # assuming all of my series need to be in the same dsc for this to work...
plt.setRenderer(i,renderer)
if i > 0:
plt.mapDatasetToRangeAxis(i,1)
else:
plt.mapDatasetToRangeAxis(i,0)
plt.getRangeAxis(0).setNumberFormatOverride(mins_to_str())
Currently, I'm getting this:
Any ideas/help would be greatly appreciated.