Menu

pixel location for LabelItem

2012-08-08
2012-09-13
  • Pete Boysen

    Pete Boysen - 2012-08-08

    I need to find the pixel location of a point to place a LabelItem on the graph. I see that in the Javascript there is a chart.getXAxis().translate(pt) but it is not part of the GWT api. Can you add it?

     
  • Pete Boysen

    Pete Boysen - 2012-08-08

    As a workaround I tried:
    private native int getXPixel(JavaScriptObject chart,int pt) /-{
    try {
    return chart.plotLeft+chart.xAxis[0].translate(pt);
    } catch(e) {
    alert(e.message);
    }
    }-
    /;

    and called getXPixel(chart.getNativeChart(),pt);

    The problem is that I am trying to position a LabelItem based a point location. The above only works after the chart is rendered but the LabelItems don't appear if I call chart.setLabelItems after chart is rendered. What am I missing?

    Also the above returns a number like 173.13455 when I expected an integer.

     
  • Philippe Lhoste

    Philippe Lhoste - 2012-09-13

    Indeed, I can confirm that LabelItems added after rendering are not displayed, even after a chart.redraw().

    I think we can have a reasonable simulation of them with a function I found somewhere, using chart.renderer.text().

    Funnily, I can't find translate() in the Highcharts reference, but I found a forum post (with jsFiddle example) using it, and indeed it is in the sources.
    After some experimentations (new to JSNI stuff), I came up with a reasonable workaround.

    private static native JavaScriptObject renderText(
          JavaScriptObject chart, String text,
          int plotX, int plotY
    ) /*-{
       return chart.renderer.text(text, plotX, plotY).add();
    }-*/;
    private static native double getXPos(JavaScriptObject chart, Number pt)
    /*-{
       return chart.plotLeft + chart.xAxis[0].translate(pt);
    }-*/;
    private static native double getYPos(JavaScriptObject chart, Number pt)
    /*-{
       return chart.plotTop + chart.plotHeight - chart.yAxis[0].translate(pt);
    }-*/;
    

    and in onClick() of chart.setClickEventHandler()'s anonymous class, I do:

            Point[] points = chartSeries.getPoints();
            JavaScriptObject nc = chart.getNativeChart();
    
            int posX = (int) getXPos(nc, points[count].getX());
            int posY = (int) getYPos(nc, points[count].getY());
    
            renderText(nc, "Point " + count, posX, posY);
    

    You might want to add some offset to posX and posY to put them farther of the marker.

    Note: at first, I tried int as return value, but the DevMode of GWT warned that it had to convert the Double return value to int...

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.