在jFreeChart直方图中配置两个范围轴

时间:2018-09-24 07:00:25

标签: java histogram jfreechart

我在jFreeChart直方图中有两个系列。通常情况下,一个较小,然后另一个较小,我们想缩小较小的尺寸,因此它与较大的系列相似,我想验证是否可以使用jFreeChart的常用API和两个单独的范围轴来实现,大概一个在右边,另一个在左边。

enter image description here

1 个答案:

答案 0 :(得分:1)

官方示例非常有帮助。这是我基于它们的解决方案,这要归功于垃圾桶的建议。

enter image description here

public void generateChart(List<FmsData> data, Valve valve) {

    HistogramDataset aggDs = createAggDataset(data);
    chart = createChart(aggDs, valve.getNumber());

    HistogramDataset valveDs = createValveDataset(data,valve.getNumber());
    XYPlot plot = chart.getXYPlot();

    // configure the second dataset
    plot.setDataset(0, aggDs);
    plot.setDataset(1, valveDs);
    plot.setRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
    plot.mapDatasetToRangeAxis(0, 0);
    plot.mapDatasetToRangeAxis(1, 1);

    NumberAxis valveRangeAxis = new NumberAxis();
    plot.setRangeAxis(1, valveRangeAxis);

    XYBarRenderer renderer2 = new XYBarRenderer();
    plot.setRenderer(1, renderer2);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    renderer2.setDrawBarOutline(false);
    // flat bars look best...
    renderer2.setBarPainter(new StandardXYBarPainter());
    renderer2.setShadowVisible(false);
    // end config second dataset