0

I am using Primefaces Barchart and i want to show the data in the ChartSeries when link to any bar in barchart. My code is as below:

<p:chart type="bar" model="#{dailyreport.horizontalBarChart}"  rendered="#{dailyreport.chartDate != null}"  style="height:900px;width:900px;" widgetVar="bchart">
</p:chart>

and the horizontalBarChart i'm setting Map for Series and the values in the map is like ("00:14",10).

I know about primefaces interactive charts But how to get that particular item data which is clicked in the chart.I managed to do the below:

public void itemSelect(ItemSelectEvent event){
    Integer seriesIndex = event.getSeriesIndex();
    Integer itemIndex = event.getItemIndex();

HorizontalBarChartModel cModel = (HorizontalBarChartModel) ((org.primefaces.component.chart.Chart) event.getSource()).getModel();
List<ChartSeries> cData= cModel.getSeries();

Map<Object, Number> ct =  cData.get(seriesIndex).getData();

}

How to get that particular item from map using itemIndex?

2 Answers2

2

i have done is like this:

HorizontalBarChartModel cModel = (HorizontalBarChartModel) ((org.primefaces.component.chart.Chart) event.getSource()).getModel();

 ChartSeries mySeries = cModel.getSeries().get(event.getSeriesIndex());

 Set<Entry<Object, Number>> mapValues = mySeries.getData().entrySet();

 Entry<Object,Number>[] test = new Entry[mapValues.size()];

 mapValues.toArray(test);
System.out.prinlnt("Key"+test[event.getItemIndex].getKey());
System.out.prinlnt("Value"+test[event.getItemIndex].getValue());
  • Correct, that indeed works to (and as I posted in my answer, it is a plain java issue). I'll file an issue in the PF issue list to add a getData(index) to the chartmodel... – Kukeltje Feb 02 '15 at 16:45
1

The problem (which is now basically a plain java one ;-)) is that the getData() on the ChartSeries is backed by a LinkedHashMap, which does not have the option to get a value based on an index (even though the LinkedHashMap is maintains the insertion order. So you have to maintain a mapping between the key and index yourself while populating the model and keep that e.g. in a bean as well. Another option to not have to do this each time is to extend the ChartSeries like this:

package some.package;

import java.util.ArrayList;
import java.util.Map;

import org.primefaces.model.chart.ChartSeries;

public class MyChartSeries extends ChartSeries {

    private ArrayList<Object> indexKeyMapper = new ArrayList<>();

    public void setData(Map<Object, Number> data) {
        throw new RuntimeException("No, sorry");
    }

    public void set(Object x, Number y) {
        getData().put(x, y);
        indexKeyMapper.add(x);
    }

    public Object getKey(int index) {
        return indexKeyMapper.get(index);
    }

    public Number getData(int index) {
        return getData().get(indexKeyMapper.get(index));
    }

}

And then you can use a getData(event.getItemIndex()) on the retrieved series (do not forget to cast to MyChartSeries).

(this works, I just tested)

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • ya i know that this but how to get that particular item that was clicked by the user. – Noman Khurram Feb 02 '15 at 15:02
  • How can i get map entry with item index? – Noman Khurram Feb 02 '15 at 15:06
  • well, that is just as easy... It is all in the api... You get to the series with `ChartSeries mySeries = model.getSeries().get(event.getSeriesIndex())` and then with `mySeries.getData()` you get to the LinkedHashMap. And using the `event.getItemIndex()` on this linked hasmap like in [here](http://stackoverflow.com/questions/10387290/how-to-get-position-of-key-value-in-linkedhashmap-using-its-key) you have the value... Thanks for telling us btw that whaty I first posted was already known – Kukeltje Feb 02 '15 at 15:15
  • Keep in mind that you need to track the index vs hashmap key yourself when populating the model. You can then get the key based on the index and then do a `mySeries.getData().get(key)`. Maybe extending the ChartSeries to do this by itself can work also (no time to give that a try though) – Kukeltje Feb 02 '15 at 15:32
  • Thank you very much for your help and for your precious time. – Noman Khurram Feb 02 '15 at 16:07
  • I enhanced my answer with what I posted above... A fully working examle – Kukeltje Feb 02 '15 at 16:13
  • 1
    If this answer solved your problem, please click on the green check mark next to this answer. Also [see how accepting an answer works](http://stackoverflow.com/help/someone-answers) @NomanKhurram – kolossus Feb 03 '15 at 03:57