javajfreechart

JFreeChart - Lines with same starting value not in the same initial position


I am developing a graphic representation of some info. The two lines have the same starting value, so they should be starting in the same position on the chart. But, one of them is starting a lot below the other. I don´t know what I am missing here. Can someone please give a hand? Thanks in advance. (The values / datasets are coming from another method, just asume that the lines have the same initial value)

enter image description here

The snippet of code where the lines (and bars) are rendered.

private JFreeChart createChart(List<SpartaConsolidadoDistribuidora> listCons, int idCenario, String usina) {
    //Cria o Renderer das StackBars.
    GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer();
    KeyToGroupMap map = new KeyToGroupMap("G1");
    
    int g = 1;
    for (int j = 0; j < 5; j++) {
        for (int i = 0; i < contratos.size(); i++) {
            map.mapKeyToGroup(contratos.get(i), "G" + g);
        }
        g++;
    }
    
    renderer.setSeriesToGroupMap(map);
    
    renderer.setItemMargin(0.0);
    Paint p1 = new GradientPaint(0.0f, 0.0f, azul, 0.0f, 0.0f, azul);
    renderer.setSeriesPaint(0, p1);
    renderer.setSeriesPaint(7, p1);
    renderer.setSeriesPaint(14, p1);
    renderer.setSeriesPaint(21, p1);
    renderer.setSeriesPaint(28, p1);
    renderer.setSeriesPaint(35, p1);
    renderer.setSeriesPaint(42, p1);
     
    Paint p2 = new GradientPaint(0.0f, 0.0f, roxo, 0.0f, 0.0f, roxo);
    renderer.setSeriesPaint(1, p2);
    renderer.setSeriesPaint(8, p2);
    renderer.setSeriesPaint(15, p2);
    renderer.setSeriesPaint(22, p2);
    renderer.setSeriesPaint(29, p2);
    renderer.setSeriesPaint(36, p2);
    renderer.setSeriesPaint(43, p2);
    
    Paint p3 = new GradientPaint(0.0f, 0.0f, verde, 0.0f, 0.0f, verde);
    renderer.setSeriesPaint(2, p3);
    renderer.setSeriesPaint(9, p3);
    renderer.setSeriesPaint(16, p3);
    renderer.setSeriesPaint(23, p3);
    renderer.setSeriesPaint(30, p3);
    renderer.setSeriesPaint(37, p3);
    renderer.setSeriesPaint(44, p3);

    Paint p4 = new GradientPaint(0.0f, 0.0f, rosa, 0.0f, 0.0f, rosa);
    renderer.setSeriesPaint(3, p4);
    renderer.setSeriesPaint(10, p4);
    renderer.setSeriesPaint(17, p4);
    renderer.setSeriesPaint(24, p4);
    renderer.setSeriesPaint(31, p4);
    renderer.setSeriesPaint(38, p4);
    renderer.setSeriesPaint(45, p4);
    
    Paint p5 = new GradientPaint(0.0f, 0.0f, amarelo, 0.0f, 0.0f, amarelo);
    renderer.setSeriesPaint(4, p5);
    renderer.setSeriesPaint(11, p5);
    renderer.setSeriesPaint(18, p5);
    renderer.setSeriesPaint(25, p5);
    renderer.setSeriesPaint(32, p5);
    renderer.setSeriesPaint(39, p5);
    renderer.setSeriesPaint(46, p5);
    
    Paint p6 = new GradientPaint(0.0f, 0.0f, azulClaro, 0.0f, 0.0f, azulClaro);
    renderer.setSeriesPaint(5, p6);
    renderer.setSeriesPaint(12, p6);
    renderer.setSeriesPaint(19, p6);
    renderer.setSeriesPaint(26, p6);
    renderer.setSeriesPaint(33, p6);
    renderer.setSeriesPaint(40, p6);
    renderer.setSeriesPaint(47, p6);
    
    Paint p7 = new GradientPaint(0.0f, 0.0f, azulEscuro, 0.0f, 0.0f, azulEscuro);
    renderer.setSeriesPaint(6, p7);
    renderer.setSeriesPaint(13, p7);
    renderer.setSeriesPaint(20, p7);
    renderer.setSeriesPaint(27, p7);
    renderer.setSeriesPaint(34, p7);
    renderer.setSeriesPaint(41, p7);
    renderer.setSeriesPaint(49, p7);

    renderer.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL));
    
    SubCategoryAxis domainAxis = new SubCategoryAxis("");
    
    List<CategoryDataset> listDS = createDatasets(listCons, idCenario);
    
    CategoryPlot plot = new CategoryPlot();
    
    plot.setDataset(listDS.get(0));
    plot.setRenderer(renderer);
    plot.setDomainAxis(domainAxis);
    
    NumberAxis nAxis = new NumberAxis("MWm");
    nAxis.setAutoRange(true);
    nAxis.setTickUnit(new NumberTickUnit(100000));
    plot.setRangeAxis(nAxis);
    plot.setFixedLegendItems(createLegendItems());
    
    plot.setOrientation(PlotOrientation.VERTICAL);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(true);
            
    //Agora crio o segundo renderer - Line - LINE 1 RENDER
    CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setSeriesStroke(0, new BasicStroke(4));
    renderer2.setSeriesPaint(0, preto);
    plot.setDataset(1, listDS.get(1));
    plot.setRenderer(1, renderer2);
    
    final ValueAxis rangeAxis2 = new NumberAxis("");
    rangeAxis2.setTickLabelsVisible(false);
    plot.setRangeAxis(1, rangeAxis2);
    plot.getRangeAxis().setAutoRange(true);
    
    //Agora crio o terceiro renderer - Line - LINE 2 RENDER
    CategoryItemRenderer renderer3 = new LineAndShapeRenderer();
    renderer3.setSeriesStroke(0, new BasicStroke(4));
    renderer3.setSeriesPaint(0, vermelho);
    plot.setDataset(2, listDS.get(2));
    plot.setRenderer(2, renderer3);
    plot.mapDatasetToRangeAxis(2, 1);
    
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    
    final JFreeChart chart = new JFreeChart(plot);
    chart.setTitle(usina);
    
    return chart;
}

EDIT:

As the tip provided by @trashgod in the comments, here is the snippet that did the trick:

NumberAxis nAxis = new NumberAxis("MWm");
    nAxis.setAutoRange(true);
    nAxis.setLabelFont(new Font("Arial Black",0, 18));
    nAxis.setTickUnit(tickUnit);
    nAxis.setTickLabelsVisible(true);
    
    plot.setRangeAxis(nAxis);
    plot.setFixedLegendItems(createLegendItems());
            
    //Agora crio o segundo renderer - Line
    CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
    renderer2.setSeriesStroke(0, new BasicStroke(4));
    renderer2.setSeriesPaint(0, vermelho);
    
    plot.setDataset(1, listDS.get(1));
    plot.setRenderer(1, renderer2);
    
    NumberAxis rangeAxis2 = new NumberAxis("");
    rangeAxis2.setAutoRange(true);
    rangeAxis2.setTickUnit(tickUnit);
    rangeAxis2.setTickUnit(new NumberTickUnit(1000000), false, false);
    rangeAxis2.setAutoTickUnitSelection(true);
    rangeAxis2.setTickLabelsVisible(false);
    
    plot.setRangeAxis(1, rangeAxis2);
    
    //Agora crio o terceiro renderer - Line
    CategoryItemRenderer renderer3 = new LineAndShapeRenderer();
    renderer3.setSeriesStroke(0, new BasicStroke(4));
    renderer3.setSeriesPaint(0, preto);
    
    plot.setDataset(2, listDS.get(2));
    plot.setRenderer(2, renderer3);

Solution

  • In the chart shown, the endpoints look aligned on the domain, but they appear offset on the range by about what I'd expect for a stacked renderer. The exact solution, seen here, modifies a second axis, also seen here. Also consider a CombinedDomainCategoryPlot, mentioned here.