简体   繁体   English

如何在条形图上使用单独的范围轴绘制两个数据系列

[英]How to plot two data series on bar chart with separate range axes

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. 我想知道是否有可能在JFreeChart中绘制( XYPlot )数据集,其中针对单独的y轴范围绘制数据集中包含的序列。 The date is clustered by category/timestamp, eg 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() ; 我试图将每个系列提取到其自己的数据集中,以便可以调用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. 它们允许您在渲染之前配置JFreeChart。

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. 任何想法/帮助将不胜感激。

Here is a time series chart within ignition's Report developing environment. 这是点火报告开发环境中的时序图。 在此处输入图片说明

This one is completely setup using the Chart Options and just a tiny bit of JFreeChart scripting. 这是完全使用“图表选项”和一点点的JFreeChart脚本来设置的。 Should be similar to designing a bar chart. 应该类似于设计条形图。 Hope it helps. 希望能帮助到你。

在此处输入图片说明

JFreeChart Script - JFreeChart脚本-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM