Advertisement
Guest User

Custom JRDataSource

a guest
Jun 21st, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. package com.dbelectronics;
  2.  
  3. import net.sf.jasperreports.engine.JRDataSource;
  4. import net.sf.jasperreports.engine.JRField;
  5. import net.sf.jasperreports.engine.JRException;
  6.  
  7. /*
  8. This example is a custom JRDataSource containing 3 fields:
  9.     - "Banda" wich contains 20 strings (used as labels in the chart)
  10.     - "Calibracion" wich will be created dinamically from a CSV field (like "48,50,53,49")
  11.     - "Verificacion" same as the previous one
  12. */
  13. public class BandasCSVDatasource implements JRDataSource {
  14.    
  15.     String[] labels = {"63","83","100","125","160","200","250","315","400","500","630","800",
  16.             "1.0k","1.2k","1.6k","2.0k","2.5k","3.0k","4.0k","5.0k"};
  17.     String[] calibracion;
  18.     String[] verificacion;
  19.     int index;
  20.  
  21.     public BandasCSVDatasource(String csv1, String csv2) {
  22.         calibracion = csv1.split(",");
  23.         verificacion = csv2.split(",");
  24.         index = -1;
  25.     }
  26.    
  27.     /* Optional method containing your field names */
  28.     public static String[] fieldNames() {
  29.         String[] fieldNames = {"Banda","Verificacion","Calibracion"};
  30.         return fieldNames;
  31.     }
  32.  
  33.     @Override
  34.     public Object getFieldValue(JRField f) throws JRException {
  35.         String fieldName = f.getName();
  36.         Object o = null;
  37.        
  38.         if ( fieldName.equals("Banda") ) {
  39.             o = labels[index];
  40.         } else if ( fieldName.equals("Verificacion") ) {
  41.             o = verificacion[index];      
  42.         } else {
  43.             o = calibracion[index];
  44.         }
  45.         return o;
  46.     }
  47.  
  48.     @Override
  49.     public boolean next() throws JRException {
  50.         index++;
  51.         if (index>=20) {
  52.             return false;
  53.         } else {
  54.             if (index<calibracion.length && index<verificacion.length) {
  55.                 return true;
  56.             } else {
  57.                 return false;
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement