ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  2 label problems : overlapp on data-labels and coordinates for a TextBox
Posted by Alexandre on Nov-24-2011 16:45
Attachments:
Hi,

Given the attached chart, I would:
- make the labels associated with the bubbles do not overlap - automatically / no manual poitions changes ("Australie" et "Pays-Bas" to the approximatives coordinates x = 100 and y = 6) ;
- retrieve the coordinates (x,y) of the first point of my line component "Net Cost Lehman CDS" in order to place the text by inference and not as hard as is currently the case.

The code is below.

Thank you in advance,
Regards,
Alexandre



// ---------------- Code ----------------
package test ;

import java.awt.*;

import test.graphics.AbstractGraphicObject;
import ChartDirector.*;

public class Bubble {

public Image createChart() {
      // Chart & PlotArea
      XYChart chart = new XYChart(540, 480);
      chart.setPlotArea(70, 65, 400, 350, -1, -1, Chart.Transparent, 0xc0c0c0, -1);
      chart.addTitle("Couvertures de d?faillance (CDS)", "Times New Roman Bold", 12) ;

      // x-axi
      chart.xAxis().setLinearScale(0, 200) ;
      chart.xAxis().setTitle("Dette public / PIB", "Arial Bold Italic", 10);
      chart.xAxis().setWidth(2);

      // y-axis
      chart.yAxis().setTitle("Encours des positions nettes sur CDS (Md?)", "Arial Bold Italic", 10);
      chart.yAxis().setWidth(2);

      // bubbles-layer
      ScatterLayer layer = chart.addScatterLayer(
        new double[]{41, 42, 50, 59, 77, 91, 99, 100, 161, 50, 63, 70, 119},
        new double[]{2.5, 1.5, 1, 2.8, 6, 6, 7, 4, 5, 16, 19, 20, 24},
        "couverture", Chart.SolidSphereShape, 20, 0x85ff3333
      );
      layer.setSymbolScale(new double[]{20, 10, 15, 40, 30, 25, 25, 40, 40, 80, 50, 80, 90}) ;

      //XXX Bubbles-labels : How make for no overlapp labels ?
      layer.addExtraField(new String[]{"Finlande", "Slov?nie", "Autriche", "Slovaquie", "Australie", "Pays-Bas", "Belgique", "Portugal", "Gr?ce", "Allemagne", "Espagne", "France", "Italie"});
      layer.setDataLabelFormat("{field0}");
      TextBox textbox = layer.setDataLabelStyle("Arial Bold", 8);
      textbox.setAlignment(Chart.Left);
      textbox.setPos(4, 0);

      // Line-layer
      LineLayer layer2 = chart.addLineLayer(new double[]{5, 5}, 0x6b1010, "Net Cost Lehman CDS");
      layer2.setXData(new double[]{0, 200}) ;
      layer2.setLineWidth(2) ;
      layer2.moveFront() ;

      //XXX How deduce coordinates ?
      chart.addText(180, 340, "Net Cost Lehman CDS", "Times New Roman Bold Italic", 8, 0x6b1010).setAlignment(Chart.TopRight);

      return chart.makeImage() ;
    }
}
// --------------------------------------------------------
Bubles.png

  Re: 2 label problems : overlapp on data-labels and coordinates for a TextBox
Posted by Peter Kwan on Nov-25-2011 00:28
Hi Alexandre,

Unluckily, ChartDirector cannot automatically position the labels so that they do not overlap in any case. However, ChartDirector allows you to develop code to position the labels, so potentially you can write some code to automatically position the labels so that they do not overlap.

After adding the scatter layer, you can call BaseChart.layout. Thereafter, you can use XYChart.getXCoor and XYChart.getYCoor to determine the pixel coordinates of any point in the chart. For example:

chart.layout();

int xCoor = chart.getXCoor(myXCoor);
int yCoor = chart.getYCoor(myYCoor);

In this way, you can determine the pixel coordinates of your bubbles. You can then use BaseChart.addText to add the label text in any place you like.

Note that if you have sufficient large number of labels crowded on a small place, they will always overlap. It is because it is not possible to put arbitrarily large number of visible labels on a finite region without overlapping. So the best one can do is to minimize the chance of overlapping. It would be logically impossible to completely avoid overlapping.

Because of the impossibility to completely avoid overlapping in the general case, another common solution is to simply add a background color to the text label. In this case, if the labels overlap, the top label will still be clearly visible due having a background. The background color can be completely opaque or partially transparent. For example:

textbox.setBackground(0x7fffffff);

For the "line component", if your intention is to draw a horizontal line across the chart, you can use a Mark instead of a line layer. The code is like:

//add mark line at y = 5
Mark m = chart.yAxis().addMark(5, 0x6b1010, "Net Cost Lehman CDS");

//put the mark label at the top-left corner of the mark line
m.setLabelStyle("Times New Roman Bold Italic", 8, 0x6b1010).setAlignment(Chart.TopLeft);

If for some reasons, you must use a line layer (eg. if your line is not horizontal or vertical or is not a straight line), you can add the label to the first data point of your line, like:

layer2.addCustomDataLabel(0, 0, "Net Cost Lehman CDS", "Times New Roman Bold Italic", 8, 0x6b1010).setAlignment(Chart.BottomRight);

Hope this can help.

Regards
Peter Kwan

  Re: 2 label problems : overlapp on data-labels and coordinates for a TextBox
Posted by Alexandre on Nov-25-2011 17:16
Hi Peter,

Thanks for "Mark". It is is obviously the solution.

For "Bubble labels", I'll try to find an appropriate algorithm that allows to rotate around the bubble if necessary.

If I find something relevant, I post it to the rest of this post in case others would like.

Thanks,
Best regards,
Alexandre.