2

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:

Here is the 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:

Chart Screenshot

Any ideas/help would be greatly appreciated.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
pbouill
  • 31
  • 6

1 Answers1

3

Here is a time series chart within ignition's Report developing environment. enter image description here

This one is completely setup using the Chart Options and just a tiny bit of JFreeChart scripting. Should be similar to designing a bar chart. Hope it helps.

enter image description here

JFreeChart Script -

    #Import Java classes - Color & JFreeChart
from java.awt import Color
from org.jfree.chart.plot import CategoryPlot

#get Plot of Current Chart
plot = chart.getPlot()

#Set color of domain and range gridlines
plot.setDomainGridlinePaint(Color.black)
plot.setRangeGridlinePaint(Color.black)
PaulMc
  • 109
  • 11