Search code examples
jfreechartlabelrenderer

jfreechart multiple renderers tooltip not working


enter image description hereI have a figure with 2 sets of graphs (detections and localizations). First set (localizations) is red ,orange,pink and second set (detections) is blue,black,cyan. I have created a renderer for each set in order to set the colors. I have set the tooltip to true but when I mouseover on the second set (detection) I can't see the labels. I can see labels only for first set on mouseover(see picture) but not for the other set. Here is my code:

    JFreeChart avg_chart = ChartFactory.createTimeSeriesChart(
            "Average detections and localizations" ,
            "" ,
            "" ,
            null ,
            true , true , false);
    avg_chart.setBackgroundPaint(Color.WHITE);

    final XYPlot plot = avg_chart.getXYPlot( );
    plot.setDataset(0,this.dataset_local);
    plot.setDataset(1,this.dataset_detect);
    plot.setRangeAxis(0,new NumberAxis("Localizations"));
    plot.setRangeAxis(1,new NumberAxis("Detections"));

    plot.mapDatasetToRangeAxis(0, 0);
    plot.mapDatasetToRangeAxis(1, 1);

    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    XYLineAndShapeRenderer renderer1 = (XYLineAndShapeRenderer) plot.getRenderer(0);//localization
    renderer1.setSeriesPaint( 0 , Color.RED );
    renderer1.setSeriesPaint( 1 , Color.MAGENTA );
    renderer1.setSeriesPaint( 2 , Color.orange );
    renderer1.setBaseItemLabelsVisible(true);

    XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(true, false); //detection ****************
    renderer2.setSeriesPaint( 0 , Color.BLUE);
    renderer2.setSeriesPaint( 1 , Color.BLACK );
    renderer2.setSeriesPaint( 2 , Color.CYAN );
    renderer2.setBaseItemLabelsVisible(true);

    plot.setRenderer(0,renderer1);
    plot.setRenderer(1,renderer2);

    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("dd/MM/yyyy"));
    return avg_chart;

}

I have tried XYLineAndShapeRenderer renderer2 = (XYLineAndShapeRenderer) plot.getRenderer(1) but it gives be a nulll exception.


Solution

  • ChartFactory.createTimeSeriesChart() adds an XYToolTipGenerator to renderer1 for you when tooltips is true. You probably just need to use it with renderer2:

    renderer2.setBaseToolTipGenerator(renderer1.getBaseToolTipGenerator());
    

    Or you can add a new one to renderer2:

    XYToolTipGenerator toolTipGenerator2 = StandardXYToolTipGenerator.getTimeSeriesInstance();
    renderer2.setBaseToolTipGenerator(toolTipGenerator2);