Passing a Custom JR Datasource to a chart


Hey good day, im having a hard time figuring out how to pass data from a custom JR Datasource to a chart, I got the data passed to the report however whenver I try to pass it to a chart it doesnt show anything.

heres the code below for the datasource class:

package MyDataAdapt;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class GoImplementation implements JRDataSource {

    public GoImplementation() {
        // TODO Auto-generated constructor stub
     * For this data adapter the informations are embedded in the code

    private static final  Integer[] lotNum = {33301141,33301143 };
    private static final Integer[] level= {1,3};
    private static final double[] mean= {4.49,457.04};
    private static final double[] SD= {0.30,22.58};
    private static final double[] CV= {6.79,4.94};
    private static final Integer[] N= {54,53};
    private HashMap<String, String> capitalCities = new HashMap<String, String>();
    private static final double[] mean2= {3.92,465.95};
    private static final double[] SD2= {0.35,31.11};
    private static final double[] CV2= {9.04,6.68};
    private static final Integer[] N2= {333,340};
      private static final String[] department = {"GENERAL"," "}; 
      private static final String[] instruementArray = {"ARCHITECT cI4100-I1SR"," "};
      private static final String[] test = {"Human Chronic Gonadotropin (BhCG) {7K78}(mIU/mL"," "};
      private static final String[] method = {"NONE"," "};
      private static final String[] reagentArray = {"ABBOTT"," "};
      private static final String[] month = {"January, 2015"," "};
      private static final String[] shift = {"All"," "};
      private static final String[] qcProduct = {"MULTICHEM IA PLUS(05P76-10)"," "};
      private static final Integer[] days = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
      HashMap<String, Integer> lotp = new HashMap<String, Integer>();
     HashMap<String, Double> meanp = new HashMap<String, Double>();
      HashMap<String, Double> sdp= new HashMap<String, Double>(); 
      HashMap<String,Integer> dayp = new HashMap<String, Integer>();
     * Variable to store how much records were read
    private int counter = -1;

     * Variables to store the number of fields, and their names, in the report
    private HashMap<String, Integer> fieldsNumber = new HashMap<String, Integer>();

     * Method used to know if there are records to read.
    private int lastFieldsAdded = 0;
    public boolean next() throws JRException {
        if (counter<lotNum.length-1) {
            return true;
        return false;

     * This method is called for every field defined in the report. So if i have 2 fields it is called 2 times for every record, and 
     * for each of them it must provide a value.
     * The parameter can be used to understand for which field is requested, in fact it contains the name of the requested field. This 
     * data adapter is done with the goal of return two values, a name and an age. An easy option would be expect a field with the name 
     * "Name" and one with name "Age". But we can do something more flexible, in this case we will enumerate all the fields requested and 
     * and the first two will be assumed to be for name and age, for all the others will be returned an empty string. So we can have a report 
     * with more than two fields, but the name and the age will be returned each time only for the first two. 
     * If this example is too much complex refer to the method getFieldValue2, where is shown the first explained, and simple solution, where we 
     * expect two fields with a precise name.
    public Object getFieldValue(JRField jrField) throws JRException {
        Integer fieldIndex;
        if (fieldsNumber.containsKey(jrField.getName())) {
            fieldIndex = fieldsNumber.get(jrField.getName());
        else {
            fieldsNumber.put(jrField.getName(), lastFieldsAdded);
            fieldIndex = lastFieldsAdded;
            lastFieldsAdded ++;
        if (fieldIndex == 0) return lotNum[counter];
        else if (fieldIndex == 1) return level[counter];
        else if (fieldIndex == 2) return mean[counter];
        else if (fieldIndex == 3) return SD[counter];
        else if (fieldIndex == 4) return CV[counter];
        else if (fieldIndex == 5) return N[counter];

        else if (fieldIndex == 6) return mean2[counter];
        else if (fieldIndex == 7) return SD2[counter];
        else if (fieldIndex == 8) return CV2[counter];
        else if (fieldIndex == 9) return N2[counter];
        else if (fieldIndex == 10) return department[0];
        else if (fieldIndex == 11) return instruementArray[0];
        else if (fieldIndex == 12) return test[0];
        else if (fieldIndex == 13) return method[0];
        else if (fieldIndex == 14) return reagentArray[0];
        else if (fieldIndex == 15) return month[0];
        else if (fieldIndex == 16) return shift[0];
        else if (fieldIndex == 17) return qcProduct[0];
        else if (fieldIndex == 18) return days;
        return "";
     * Example of a simpler getFieldValue, not actually used
    public Object getFieldValue2(JRField jrField) throws JRException {
        if (jrField.getName().equals("lotNum")) return lotNum[counter];
        else if (jrField.getName().equals("level")) return level[counter];
        meanp.put("Mean", 4.49);
        sdp.put("SD", 0.30);
        sdp.put("SD", 22.58);
        return "";
     * Return an instance of the class that implements the custom data adapter.
    public static JRDataSource getDataSource(){
        return new GoImplementation();


bakebego's picture
Joined: Sep 10 2019 - 12:00pm
Last seen: 9 months 6 days ago

The Values I want to pass are the mean, the SD and the days to the report

bakebego - 9 months 4 weeks ago

0 Answers:

No answers yet