0
votes

I am using Jfreechart API 1.0.8 to generate the TimeSeriesChart(line chart).

when I am generating the chart, i am facing the problem of overlapping. Here I am trying to display the rendered points(graph rendered points), by using XYLineAndShapeRenderer with StandardXYItemLabelGenerator.

When the points are displayed, the data-point is overlapping with the generated line chart (graph). I am taking X-Axis as time, and y-Axis as revenue of organization, and i am using line chart here.

I'm displaying the points as discussed below.

By using "renderer.setBasePositiveItemLabelPosition" method, globally i am setting the position of graph points(data points) inside the xyplot rendered chart while considering the rendered "ItemLabelAnchor".

I am sending my sample code here:

chart = ChartFactory.createTimeSeriesChart("", "", "", newxyseries, false, true, false);

renderer = new XYLineAndShapeRenderer();

renderer = (XYLineAndShapeRenderer) chart.getXYPlot().getRenderer();

renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator("{2}", monthDate, formatSymbol));

renderer.setBaseItemLabelsVisible(true);

renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE3, TextAnchor.TOP_RIGHT));

chart.getXYPlot().setRenderer(renderer);

But when I am generating the graph chart using Ms-office of Excel tools, there is no problem of overlapping of labels, the points are displayed in an effective manner without any overlapping.

2

2 Answers

0
votes

JFreeChart doesn't do any overlap detection for item labels. It would be a great feature to have, but nobody has written the code to do it.

0
votes

enter image description hereyou have to refrain the labels to get overlap by define your own algo or logic because as the graph keep growing sooner the labels will start to get overlap. the key is to make some or alternative labels transparent so that no overlapping occur For example in the following P.O.C, only 35 labels will be drawn but the tick of graphs will remai same just the labels will reduce

CategoryAxis domainAxis = plot.getDomainAxis();
    CategoryLabelPositions pos = domainAxis.getCategoryLabelPositions();
    double size = plot.getCategories().size();
    double occurence = Math.ceil(plot.getCategories().size() / 20);
    double interval = Math.ceil(plot.getCategories().size() / 20);
    for (int i = 1; i <= plot.getCategories().size(); i++) {
        if (plot.getCategories().size() > 35) {
             if(plot.getCategories().size()>35 && plot.getCategories().size()<250) {
                    if(i%8==0){
                     String cat_Name = (String) plot.getCategories().get(i-1);

                    } else{
                        String cat_Names = (String) plot.getCategories().get(i-1);
                        domainAxis.setTickLabelPaint(cat_Names, new Color(0,0,0,0));
                        }
                     }else if(plot.getCategories().size()>250){
        [![enter image description here][1]][1]
            
            if (i == occurence) {
                String cat_Name = (String) plot.getCategories().get(i - 1);

                if (occurence + interval >= size) {
                    // occurence=size;

                } else {
                    occurence = occurence + interval;
                }
            } else {
                String cat_Names = (String) plot.getCategories().get(i - 1);
                domainAxis.setTickLabelPaint(cat_Names, new Color(0, 0, 0, 0));

            }
                     }
        }

Below line will make label transparent

  domainAxis.setTickLabelPaint(cat_Names, new Color(0,0,0,0));