Saturday 8 August 2015

Jasper Report Chart Customizer class


                                      Jasper Report Chart Customizer class


Jasper Report Chart Customizer class:-Chart customizer class are nothing but java classes that can be used to modify or change the appearance of your  jasper report chart.For ex: you can change style of your line from solid to dashed,change the color of line from black to yellow,white etc.

Here is the sample implementation of customizer class,I have used XY plot series graph where I have to display lines of different color and different style of line of each data.

For implementation you need to extend class JRAbstractChartCustomizer.This class contains customize method which accepts JFreeChart and JRChart as arguments.Inside customize method you can customize your graph according to the way you want.

 import java.awt.BasicStroke;  
 import java.awt.Color;  
 import java.awt.Shape;  
 import java.awt.Stroke;  
 import java.awt.geom.Ellipse2D;  
 import java.util.ArrayList;  
 import java.util.HashSet;  
 import java.util.List;  
 import java.util.Set;  
 import net.sf.jasperreports.engine.JRAbstractChartCustomizer;  
 import net.sf.jasperreports.engine.JRChart;  
 import org.jfree.chart.JFreeChart;  
 import org.jfree.chart.plot.XYPlot;  
 import org.jfree.chart.renderer.xy.XYItemRenderer;  
 import org.jfree.data.xy.XYDataset;  
 public class KPIChartCustomizer extends JRAbstractChartCustomizer {  
      // different styles of line strokes  
      Stroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,  
                BasicStroke.JOIN_MITER, 10.0f, new float[] { 10.0f }, 0.0f);  
      Stroke dotted = new BasicStroke(1f, BasicStroke.CAP_ROUND,  
                BasicStroke.JOIN_ROUND, 1f, new float[] { 2f }, 0f);  
      Stroke solid = new BasicStroke(1.7f);  
      Stroke medium = new BasicStroke(1f);  
      Stroke thin = new BasicStroke(0.4f);  
      Stroke solidDashedWithCapButt = new BasicStroke(1.5f, BasicStroke.CAP_BUTT,  
                BasicStroke.JOIN_MITER, 10.0f, new float[] { 10.0f }, 0.0f);  
      Stroke solidDashedWithCapSquare = new BasicStroke(1.5f,  
                BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,  
                new float[] { 10.0f }, 0.0f);  
      float[] dash2 = { 1f, 1f, 1f };  
      float[] dash4 = { 4f, 4f, 1f };  
      Stroke smallDots = new BasicStroke(1, BasicStroke.CAP_BUTT,  
                BasicStroke.JOIN_ROUND, 1.0f, dash2, 2f);  
      Stroke dashedAndDots = new BasicStroke(1, BasicStroke.CAP_BUTT,  
                BasicStroke.JOIN_ROUND, 1.0f, dash4, 2f);  
      Stroke[] basicStrokeArr = { solid, dashed, dotted, smallDots, medium,  
                dashedAndDots, thin };  
      @Override  
      public void customize(JFreeChart freeChart, JRChart jrChart) {  
           XYPlot plot = (XYPlot) freeChart.getPlot();  
           XYItemRenderer renderer = plot.getRenderer();  
           int seriesCount = plot.getSeriesCount();  
           XYDataset dataSet = plot.getDataset();  
           Set<String> nameStr = new HashSet<String>();  
           Set<String> reportTypeStr = new HashSet<String>();  
           boolean oneKpiAndMultipleAPFlag = false;  
           List<Color> listOfColors = new ArrayList<Color>();  
           String[] dnNames = new String[nameStr.size()];  
           nameStr.toArray(dnNames);  
           String[] reportTypes = new String[reportTypeStr.size()];  
           reportTypeStr.toArray(reportTypes);  
           for (int i = 0; i < seriesCount; i++) {  
                String name = (String) dataSet.getSeriesKey(i);  
                Color seriesColor = getColor(name, reportTypes);  
                renderer.setSeriesPaint(i, seriesColor);  
                Stroke lineStroke = getLineStroke(name, dnNames);  
                renderer.setSeriesStroke(i, lineStroke);  
                Shape shape = new Ellipse2D.Double(-0.75, -0.75, 2, 2);  
                renderer.setSeriesShape(i, shape);  
           }  
      }  
      /**  
       * return type of line stroke .  
       *   
       * @param name  
       * @param reportTypes  
       * @return  
       */  
      private Stroke getLineStroke(String name, String[] reportTypes) {  
           Stroke lineStroke = null;  
           if (name.contains(reportTypes[0])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[0];  
           } else if (name.contains(reportTypes[1])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[1];  
           } else if (name.contains(reportTypes[2])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[2];  
           } else if (name.contains(reportTypes[3])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[3];  
           } else if (name.contains(reportTypes[4])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[4];  
           } else if (name.contains(reportTypes[5])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[5];  
           } else if (name.contains(reportTypes[6])) {  
                // colour yellow  
                lineStroke = basicStrokeArr[6];  
           }  
           return lineStroke;  
      }  
      private Color getColor(String name, String[] dnNames) {  
           Color seriesColor = null;  
           if (name.contains(dnNames[0])) {  
                // colour blue  
                seriesColor = Color.BLUE;  
           } else if (name.contains(dnNames[1])) {  
                // colour Red  
                seriesColor = Color.RED;  
           } else if (name.contains(dnNames[2])) {  
                // colour green  
                seriesColor = Color.GREEN;  
           } else if (name.contains(dnNames[3])) {  
                // colour magenta  
                seriesColor = Color.MAGENTA;  
           } else if (name.contains(dnNames[4])) {  
                // colour black  
                seriesColor = Color.BLACK;  
           } else if (name.contains(dnNames[5])) {  
                // colour cyan  
                seriesColor = Color.CYAN;  
           } else if (name.contains(dnNames[6])) {  
                // colour light gray  
                seriesColor = Color.LIGHT_GRAY;  
           } else if (name.contains(dnNames[7])) {  
                // colour yellow  
                seriesColor = Color.YELLOW;  
           } else if (name.contains(dnNames[8])) {  
                // colour pink  
                seriesColor = Color.PINK;  
           }  
           return seriesColor;  
      }  
 }  

In order to use customizer class,You need to mention customizer class in your jrxml file also.

For example :

<summary>  
           <band height="443">  
                <timeSeriesChart>  
                     <chart isShowLegend="true" customizerClass="com.ipaccess.nos.business.pm.impl.KPIChartCustomizer">  
                          <reportElement mode="Opaque" x="0" y="0" width="766" height="443" isPrintInFirstWholeBand="true"/>  
                          <chartTitle position="Top">  
                               <font size="14" isBold="true" pdfFontName="Courier-Bold" isPdfEmbedded="true"/>  
                               <titleExpression><![CDATA[$P{REPORT_NAME}]]></titleExpression>  
                          </chartTitle>  
                          <chartSubtitle/>  
                          <chartLegend position="Bottom">  
                               <font size="8"/>  
                          </chartLegend>  
                     </chart>  
           </band>  
      </summary>  





Enjoy reading :)

Error : “The user specified as a definer does not exist” when importing mysqldump


                      Change the Definer of Mysql Stored Procedures

Sometimes when we try to import mysqldump into our local machine through command line terminal,we end up getting following error

mysqldump: Got error: 1449: The user specified as a definer ('user@%') does not exist when using LOCK TABLES

This error comes because the database we are trying to import contains procedures and functions that are defined by the user 'user'.

You can monitor your procedure characteristics like database name,type,creator,creation time and character set information by using this command:

         SHOW procedure status;

after executing this command, you can see some procedures creator name is ('user@%').

So you need to change it to 'root@%' or 'root'@'localhost',you should update definer for procedures using this command in Mysql terminal,

         UPDATE 'mysql'.'proc' p SET definer='root@%' WHERE definer='user@%';

keep in mind one thing this command will update definer for all procedures.

Other way to achieve this is create user name 'user' in your database and grant all permission to it.

        GRANT all on *.* to 'user@%' identified by 'password' with grant option;

You can also view grants on any user using this command,

        SHOW GRANTS for 'user@%';




I hope this solution helps you.Enjoy coding :)