Pie Chart: OnPlotHover trying to show/edit Label

55 views
Skip to first unread message

John Smith

unread,
Sep 19, 2014, 1:39:35 PM9/19/14
to gf...@googlegroups.com
Hello,
When I set up my pie chart, I do the normal things (like in the pie chart gflot example). I add GlobalSeriesOptions, PieSeriesOptions and a Label.  I set the Label invisible by using setShow(false).
But I would like to be able to setShow(true) in the onPlotHover method I add so that you only see a PieSeries's label when you are hovering over it.  Here is my code:

plot.addHoverListener( new PlotHoverListener() {
           
@Override
           
public void onPlotHover( Plot plot, PlotPosition position, PlotItem item )
           
{
           plot
.getOptions().getGlobalSeriesOptions().getPieSeriesOptions().getLabel().setShow(true);
           
System.out.println(item.getSeries().getData().getY(0));
           
}
       
}, true );


However, when I hover, the label doesn't show, etc.  I'd also like to change the text in the label, but that isn't working either.

The PlotHoverListener itself is working because the system.out.println line is working...

Thanks in advance for any help!


Nicolas Morel

unread,
Sep 19, 2014, 2:32:02 PM9/19/14
to gf...@googlegroups.com
If you change options, you have to call the redraw method.

One other solution is to get the label element by id and change its css directly.

John Smith

unread,
Sep 19, 2014, 6:10:42 PM9/19/14
to gf...@googlegroups.com
Ok thanks that did help me change something, but that makes all of the labels appear at once.  Is it possible to make only one label (from one pieSeries) appear on its own, or is it all or nothing.
I'm not sure how to get a specific series' label.

Nicolas Morel

unread,
Sep 20, 2014, 3:57:07 AM9/20/14
to gf...@googlegroups.com
Here is a code example I tested with PieExample :

private void showOrHideLabel(Integer seriesIndex) {
    Element child = plot.getElement().getFirstChildElement();
    while (null != child) {
        if (child.getClassName().equals("pieLabel")) {
            int index = Integer.parseInt(child.getId().replaceAll("pieLabel", ""));
            if (null != seriesIndex && index == seriesIndex) {
                child.getStyle().setDisplay(Style.Display.INLINE);
            } else {
                child.getStyle().setDisplay(Style.Display.NONE);
            }
        }
        child = child.getNextSiblingElement();
    }
}

On plot hover/mouseout, call this method with item.getSeriesIndex()/null.

You have to let setShow(true) for the label options and hide the label with css.

I did it in uibinder with :

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui"
  xmlns:p="urn:import:com.googlecode.gflot.client">

  <ui:style>
      @external pieLabel;

       .pie .pieLabel {
           display: none;
       }
  </ui:style>

  <ui:with field='res' type='com.googlecode.gflot.examples.client.resources.Resources' />

  <g:HTMLPanel>
    <p:SimplePlot ui:field="plot" width="300px" height="300px" addStyleNames="{style.pie}"/>
    <p ui:field='hovering'/>
  </g:HTMLPanel>
</ui:UiBinder>




John Smith

unread,
Oct 2, 2014, 5:21:31 PM10/2/14
to gf...@googlegroups.com
Can I see your whole source code for this pie graph example class? (including imports?)
Thanks

Nicolas Morel

unread,
Oct 4, 2014, 4:38:34 AM10/4/14
to gf...@googlegroups.com
Here it is
PieExample.java
PieExample.ui.xml

John Smith

unread,
Oct 6, 2014, 2:50:53 PM10/6/14
to gf...@googlegroups.com
Thanks!
Reply all
Reply to author
Forward
0 new messages