1

I want plot a line chart using Trinidad 1.2. I have a class called AcidenteTO which consists in int indicating the year and a array of int indicating the values for each month. In ChartModel used to populate my chart, I passed a List of AcidenteTO's.

public class AcidenteTO implements Serializable {

    // -------------[ Atributos ]----------------------

    private int ano; // year

    private int[] quantidades = new int[12]; // quantities for month


    // Getters & Setters as usual
}

public class AcidentesChartModel extends ChartModel {

    private final List<AcidenteTO> acidentes;

    // Indicate the labels for each year
    private final List<String> anosLabel; 

    private List<List<Double>> yValues;

    private final Double maxYValue;

    // Month labels
    private final List<String> mesesLabels =
        Arrays.asList("Janeiro", "Fevereiro", "Mar&ccedil;o", "Abril", "Maio",
                      "Junho", "Julho", "Agosto", "Setembro", "Outubro",
                      "Novembro", "Dezembro");

    AcidentesChartModel(List<AcidenteTO> acidentes) {
        this.acidentes = acidentes;

        yValues = new ArrayList<List<Double>>(this.acidentes.size());
        List<String> anosLabel = new ArrayList<String>(this.acidentes.size());
        this.anosLabel = new ArrayList<String>(this.acidentes.size());
        double maxY = 0D;

        for (AcidenteTO a : this.acidentes) {
            maxY = preencherDados(a, maxY);
        }

        this.maxYValue = maxY;
    }

    private double preencherDados(AcidenteTO a, double maxY) {
        List<Double> mesesValues = new ArrayList<Double>(12); // Monthly values for each year
        for (int i = 0; i < 12; i++) {
            mesesValues.add((double)a.getQuantidades()[i]);
            if (a.getQuantidades()[i] > maxY) {
                maxY = a.getQuantidades()[i];
            }
        }
        this.anosLabel.add(String.valueOf(a.getAno()));
        this.yValues.add(mesesValues);

        return maxY;
    }

    public List<String> getSeriesLabels() {
        return this.anosLabel;
    }

    public List<String> getGroupLabels() {
        return this.mesesLabels;
    }

    public List<List<Double>> getYValues() {
        return this.yValues;
    }

    public Double getMaxYValue() {
        return this.maxYValue;
    }

}

What I was expecting is that as for each Year I have 12 data to plot, it would be plot all 12 values for each year. However what actually happened was the following: If I pass a List of 3 AcidenteTO's, the generated plot shows data until 3rd month. If I pass 4 AcidenteTO's List, the plot shows data until 4th month. 7 AcidenteTO's List, until 7th month (see picture below), and so on.

Ploting 7 months because I have 7 years

I could not found no example in the net showing how to use a ChartModel to create a Trinidad line chart. Just this one for pie charts. Therefore I was not able to discover where is my mistake.

Thanks,

Rafael Afonso.

Rafael Afonso
  • 595
  • 1
  • 10
  • 28
  • Hi Rafael, did the Trinidad Community solve this Issue:TRINIDAD-2276 https://issues.apache.org/jira/i#browse/TRINIDAD-2276?jql=project%20%3D%20TRINIDAD%20AND%20text%20~%20%22chart%22 My last info was that the trinidad charts won't work with Java7. Are you on a lower Java-Version? – lkdg Apr 01 '14 at 07:07
  • Yes. I am using Java 6. More exactly, I am running over Java 1.6.0_45 and JDeveloper 11.1.7.0. – Rafael Afonso Apr 01 '14 at 11:11
  • If your target will become Java7 you will get a problem by using trinidad charts. Thats why I switched to jfree charts. – lkdg Apr 01 '14 at 11:52
  • @Ikdg: I think that I am going to give up Trinidad Chart and follow your idea about JFree. Dou you know any guide how to use JFree Charts with JSF? – Rafael Afonso Apr 01 '14 at 12:43
  • A good starting point for the charts: http://www.java2s.com/Code/Java/Chart/CatalogChart.htm a *.png is produced by JFree, so you need a servlet to get them as an image on your *.jsp or whatever site. – lkdg Apr 01 '14 at 13:58
  • Yes, I have found his example yet. Thanks! – Rafael Afonso Apr 01 '14 at 14:14

1 Answers1

0

Ever considered using Highcharts to generate graphs in your Trinidad project? We do and it works like a charm! Highcharts has way more possibilities to generate graphs and add interactivity then <tr:chart>. See these examples for pie charts and other graphs in Highcharts.

The downside is that you don't have a JSF component to work with. Creating a static graph will be easy, but you might want to write a few lines of code to pass your data to the Highcharts Javascript to get things a bit more interesting. Trinidad's ExtendedRenderKitService comes in handy there:

ExtendedRenderKitService service = 
  Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
service.addScript(facesContext, "alert('foo');");

Edit

I just found this, haven't tried it yet, but I will test it: Java API for generating highchart JSON.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • In the end, I used [JFreechart](http://www.jfree.org/) to generate my charts and convert to JPEG to show in my page. Thanks for your suggestion, anyway! – Rafael Afonso Apr 03 '14 at 12:10