primefacesprimefaces-extensionsgchart

Implementation of Gchart for dynamic data


I have used Gchart to display data in chart form. For now the chart is static. I need to make the chart dynamic as the chart values may change time and again. My Code is like this:

dynamicChartObj = new GChartModelBuilder().setChartType(GChartType.PIE).build();
dynamicChartObj = new GChartModelBuilder()
                    .setChartType(GChartType.PIE)  
                    .addColumns("Topping", "Slices")
                    .addRow("A", 12)  
                    .addRow("B", 20)
                    .addRow("C", 39)
                    .addRow("D", 45)
                    .build();  
<div id="savChart">
      <pe:gChart value="#{dashboardMB.dynamicChartObj}" width="400" height="400"
      title="Quanity Wise">
      </pe:gChart>
</div>

I need to add rows dynamically. How can i do this?


Solution

  • Finally i figured out the solution for my problem. My solution for the problem is like this:

    GChartModelBuilder chartBuilder = new GChartModelBuilder();
    chartBuilder.setChartType(GChartType.PIE);
    chartBuilder.addColumns("Topping", "Slices");
    HashMap<String, Double> valuesOfChart = prepareRowsOfChart(); 
    for (Map.Entry pair : valuesOfChart.entrySet()) {
        chartBuilder.addRow((String) pair.getKey(), ((double) pair.getValue()));
    }
    chartSavingModel = chartBuilder.build();