Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Graphs in Java

  1. #1
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Graphs in Java

    ok, I want to display a graph in a pdf that displays values that I have taken from a database. the graph should be dynamic so that any changes in the database will be reflected in the graph when the program is run. Im currently trying to learn from other code but so far I can

    1.get the data from the database
    2.put it into a table
    3.create a pdf and display the table.

    Now I have all my information im trying to display that data from the table into a graph format. Basically this was like an assignment at work and the code is nearly right I just have to figure the rest out. I understand I have to use JFreeChart?

    But im getting a little stuck. there is also XML and XSL used in this question/project/task I have to do. :?

    I can show any code thats needed....hard to explain it all but in a nut shell I want to display a graph represtenting information I have got.

    Any help be appreciated.thanks

  2. #2
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Graphs in Java

    Anyone any help with this? I can provide more info if needed....heres what I have so far...in my main class..

    Java Code:
    import java.sql.ResultSet;
    
    import java.io.*;
    
    import java.sql.CallableStatement;
    import java.sql.SQLException;
    import java.text.DecimalFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Hashtable;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Properties;
    import java.util.Set;
    import java.util.StringTokenizer;
    import java.util.logging.FileHandler;
    import java.util.logging.Logger;
    
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.transform.Result;
    import javax.xml.transform.Source;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.dom.DOMResult;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    
    import org.w3c.dom.Document;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.data.general.DefaultPieDataset;
    import org.pdfbox.examples.pdmodel.AddJavascript;
    
    
    
    public class GetLipperDetails2 {
    
    	public static void main(String argv[]) throws Exception {
    
    		// String xmlString =
    		// "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><?xml-stylesheet type=\"text/xsl\" href=\"output6.xsl\"?>";
    
    		SQLAccess con = new SQLAccess();
    
    		// Logger logger =
    		// getLogger("com.prudential.maaps.profile.ManagerProfile");
    
    		String symbols = "DRTAX,ICTXX,GTFBX,OPTAX,WBIGX,RIMSX";
    		// String symbols = "DEUBX,HEGFX,FOZXX,VVOAX,FTGTX,FMIEX";
    
    		StringTokenizer st = new StringTokenizer(symbols, ",");
    
    		String type = "";
    
    		XmlBuffer lipperXML = new XmlBuffer();
    		Properties settings = ResourceLoader
    				.loadConfigEnvPropertiesFile("PiagregConfig");
    		String BTLN_PDF_LOCATION = settings.getProperty("BTLN_PDF_LOCATION");
    		// lipperXML.append(xmlString);
    
    		lipperXML.startNode("root");
    
    		while (st.hasMoreElements()) {
    			String s = (String) st.nextElement();
    			String cmd = "EXEC maap_analytics_db.dbo.get_lipper_details_by_fund '"
    					+ s + "','2011-06-30 00:00:00.000'";
    
    			String ticker_symbol = "";
    			// String pfm_date = "";
    
    			// String cmd =
    			// "EXEC maap_analytics_db.dbo.get_lipper_details_by_fund";
    			// String cmd =
    			// "EXEC maap_analytics_db.dbo.get_lipper_details_by_fund '" +
    			// ticker_symbol + "', '"+pfm_date+"'";
    
    			try {
    
    				{
    
    					// String BTLN_PDF_LOCATION_SHARE =
    					// settings.getProperty("BTLN_PDF_LOCATION_SHARE");
    
    					con = new SQLAccess();
    					ResultSet rs = con.executeQuery(cmd);
    
    					String Lipper_Code = "";
    					String fund_symbol = "";
    					String cum_pfm_3m = "";
    					String cum_pfm_ytd = "";
    					String ann_pfm_1yr = "";
    					String ann_pfm_3 = "";
    					String ann_pfm_5 = "";
    					String ann_pfm_10 = "";
    					String si_pfm = "";
    
    					while (rs.next()) {
    
    						type = rs.getString("ticker_symbol").trim();
    
    						Lipper_Code = rs.getString("Lipper_Code");
    						fund_symbol = rs.getString("fund_symbol");
    						ticker_symbol = rs.getString("ticker_symbol");
    
    						cum_pfm_3m = rs.getString("cum_pfm_3m");
    						cum_pfm_ytd = rs.getString("cum_pfm_ytd");
    						ann_pfm_1yr = rs.getString("ann_pfm_1yr");
    
    						ann_pfm_3 = rs.getString("ann_pfm_3");
    						ann_pfm_5 = rs.getString("ann_pfm_5");
    						ann_pfm_10 = rs.getString("ann_pfm_10");
    						si_pfm = rs.getString("si_pfm");
    
    						List<String> newList = new ArrayList<String>();
    						List<String> myList = new ArrayList<String>();
    
    						myList.add(cum_pfm_3m);
    						myList.add(cum_pfm_ytd);
    						myList.add(ann_pfm_1yr);
    
    						for (String test : myList) {
    							double d = Double.parseDouble(test);
    							DecimalFormat df = new DecimalFormat("#.##");
    							String output = df.format(d);
    							newList.add(output);
    
    						}
    
    						double cum_p_double = Double.parseDouble(cum_pfm_3m);
    						double cum_pfm_ytd_double = Double
    								.parseDouble(cum_pfm_ytd);
    						double ann_pfm_1yr_double = Double
    								.parseDouble(ann_pfm_1yr);
    
    					
    
    						// creating xml nodes < >
    						lipperXML.startNode("record");
    						lipperXML.addNode("Lipper_Code", Lipper_Code);
    						lipperXML.addNode("fund_symbol", fund_symbol);
    						lipperXML.addNode("ticker_symbol", ticker_symbol);
    
    						lipperXML.addNode("cum_pfm_3m", newList.get(0));
    						lipperXML.addNode("cum_pfm_ytd", newList.get(1));
    						lipperXML.addNode("ann_pfm_1yr", newList.get(2));
    
    						lipperXML.addNode("ann_pfm_3", ann_pfm_3);
    						lipperXML.addNode("ann_pfm_5", ann_pfm_5);
    						lipperXML.addNode("ann_pfm_10", ann_pfm_10);
    						lipperXML.addNode("si_pfm", si_pfm);
    						lipperXML.endNode("record");
    
    						// String x = lipperXML.toString();
    
    						// lipperXML.append(x);
    						// System.out.println(x);
    						//
    
    					}
    
    				}
    
    			} catch (Exception e) {
    				System.out.println("Exception caught here");
    
    			}
    
    		}
    
    		lipperXML.endNode("root");
    
    		System.out.println("XML File: " + lipperXML.toString());
    		FileWriter fstream = new FileWriter("lipperXML1.xml");
    		BufferedWriter out = new BufferedWriter(fstream);
    		out.write(lipperXML.toString());
    
    		// Close the output stream
    		out.close();
    
    		String[] params = new String[1];
    		String doc_url = settings.getProperty("DOC_URL").trim();
    		params[0] = "FopImagePath|" + doc_url;
    
    		PdfFactory pdf = new PdfFactory();
    
    		byte pdfContent[] = pdf
    				.renderPdf(
    						lipperXML,
    						"C:/Documents and Settings/x172805/Desktop/MutualFundsWorkspace/analytics_outputs2/templates/outputPDF.xsl",
    						params);
    		String fileName = "Lipper_.pdf";
    
    		OutputStream respOutFile = new java.io.FileOutputStream(
    				BTLN_PDF_LOCATION + fileName);
    
    		respOutFile.write(pdfContent);
    		respOutFile.flush();
    		respOutFile.close();
    
    		// /////////////////////////*Adding Graph///////////////////
    
    		/* Initialize the dataset and fill it. */
    		 DefaultPieDataset dataset = new DefaultPieDataset();
    
    		 dataset.setValue("cum_pfm_3m", 9);
    		 dataset.setValue("Projects", 4);
    		 dataset.setValue("Public", 9);
    		
    		/* Create a 3D Pie chart. */
    		JFreeChart chart = ChartFactory.createPieChart3D("Test", dataset,
    		 true, true, false);
    
    		/* Save the chart on the hard drive. */
    		ChartUtilities.saveChartAsJPEG(new File("pie.png"),
    		chart, 500, 280);
    
    		System.out.println();
    
    		String productId = null;
    		String version = "graph/";
    		String ver = version;
    		String PDF_LOCATION = settings.getProperty("BTLN_PDF_LOCATION");
    
    		String location = PDF_LOCATION + "/" + version + "graph"+ fileName;
    
    		Lipper_ManagerProfilePDFCreater managerProfilePDF = new Lipper_ManagerProfilePDFCreater();
    		managerProfilePDF.setTransformerParameter("FopImagePath",
    				settings.getProperty("PDFURL"));
    
    		managerProfilePDF.renderPdf2(lipperXML, location, type);
    		
    		
    		
    		
    		
    		
    	
    	   
    	
    		        
    
    		//CategoryDataset dataset = null;
    		//try {
    		    //dataset = DatasetReader.readCategoryDatasetFromXML(graphDataStream);
    	   // }catch(Exception e){
        	//    e.printStackTrace();
    	   // }
    		
    	   // managerProfilePDF.renderPdf2(lipperXML, location, type); 
    		
    	
    		   }			
    	}

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Netbeans couldn't find XmlBuffer, SQLAccess, PdfFactory and others. Where are those class located?

  4. #4
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Graphs in Java

    Yes there is several other classes created in this project, I didnt want to post them all, SQL Access is my class for connection to the database, XmlBuffer is my class for creating the xmlfile and pdf factory is my class for creating/rendering my pdf, and it uses the xsl file located in the above posts location. all these class's are working. so you can pressume they are fine....My problem is

    A.Creating a graph,
    B.Displaying a graph in a pdf

    Basically from line 210 downwards needs to be edited/deleted/recoded so that it does what I want....thats where im having problems....can you help? everythin above 210 works, runs and creates xml,creates pdf with some info

  5. #5
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Your graph seems to be saving to the file ok, so lines 210 to 227 are ok.

    I'm not sure about the saving to pdf part. What is Lipper_ManagerProfilePDFCreater?

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Where do I get PDF Factory and xmlfile?

  7. #7
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Graphs in Java

    ummm....thats another class part ov the project....its hard to explain..... maybe if you could just disregard the code I have, and just presume that so far I can get my data from the database and store it in a table in my pdf file,,,,, and now I want to do create a graph from that information.

    How would you or anyone do this?

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Graphs in Java

    You can try to find some of open source solutions. Try to google for JGraph or something like that...

  9. #9
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Using apache's pdfBox I was able to save an image to a pdf file. See code below:

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import org.apache.pdfbox.examples.pdmodel.AddImageToPDF;
    import org.apache.pdfbox.exceptions.COSVisitorException;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.PDPage;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.data.general.DefaultPieDataset;
    
    
    public class TestGraph
    {
    
        /**
         * @param args the command line arguments
         * @throws IOException
         */
        public static void main(String[] args) throws IOException, COSVisitorException
        {
    
    
            DefaultPieDataset dataset = new DefaultPieDataset();
    
            dataset.setValue("cum_pfm_3m", 9);
            dataset.setValue("Projects", 4);
            dataset.setValue("Public", 9);
    
            /*
             * Create a 3D Pie chart.
             */
            JFreeChart chart = ChartFactory.createPieChart3D("Test", dataset,
                    true, true, false);
    
            
            ChartUtilities.saveChartAsJPEG(new File("pie.jpg"),
                    chart, 500, 280);
    
            //BufferedImage b = chart.createBufferedImage(500,280);
    
            //File file = new File("pie.jpg");
           // BufferedImage pieBImage= ImageIO.read(file);
          
            PDDocument document = new PDDocument();
            PDPage blankPage = new PDPage();
            document.addPage(blankPage);
    
            document.save("MyPdf.pdf");
    
            AddImageToPDF a = new AddImageToPDF();
    
    
            a.createPDFFromImage("MyPdf.pdf", "pie.jpg", "MyPdf2.pdf");
    
        }
    }

  10. #10
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Quote Originally Posted by Duthie View Post
    ummm....thats another class part ov the project....its hard to explain..... maybe if you could just disregard the code I have, and just presume that so far I can get my data from the database and store it in a table in my pdf file,,,,, and now I want to do create a graph from that information.

    How would you or anyone do this?
    Your original code creates the graph using jfree.chart. I tested that part and it works. I was also able to convert the image file created to a PDF. Isn't that what you wanted to do? It looks like apache's pdfBox will do what you want to do.

  11. #11
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Graphs in Java

    Where do you get the AddImageToPDF class from? yes well I want to add a graph to my pdf

  12. #12
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Its a class in apache's pdfBox (see Apache PDFBox - Download).

  13. #13
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Graphs in Java

    See this is an assignement through work....I dont think I can install anythin unless its installed already, is this download jar files? Can I check if I have it already some how?

    I can download that and test through my home computer tongiht to see if I can understand it better...

    :( long time tryin to get this to work now....I could show you more code from the way its suppose to be done and they way your trying to show me how its done is different I think

  14. #14
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Yes, the jar file is located at Apache PDFBox - Download. The file name is pdfbox-app-1.6.0.jar, its the second file in the list of files to download.

    Does the image to pdf program do part of what you want to do?

    Send more code if you can. If you don't want to post it because it work code, you can email it to me directly (private message). Then we continue with posting on the forum.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Graphs in Java

    Quote Originally Posted by shall View Post
    you can email it to me directly (private message).
    Please do not solicit off-forum communications. The purpose of a forum is to share problems and their possible solutions. Not to tuck them away as a private communication between two members.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    Quote Originally Posted by DarrylBurke View Post
    Please do not solicit off-forum communications. The purpose of a forum is to share problems and their possible solutions. Not to tuck them away as a private communication between two members.

    db
    The only thing I asked for was the code and not a private discussion as you will note I said "Then we <will> continue with posting on the forum."

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Graphs in Java

    Quote Originally Posted by shall View Post
    The only thing I asked for was the code and not a private discussion as you will note I said "Then we <will> continue with posting on the forum."
    Without the code being discussed, much of such discussion might appear out of context.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    Duthie is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Graphs in Java

    Well I can post some more code but il have to be selective. If I can get it working the way I want il repost the source code for sure. which class's or code do you want to see. also there is many many jars in my build path which I am not sure which are for which,

    I have a PDFBox-0.7.3.jar which im guessin does much the same as you mentioned above??

  19. #19
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Graphs in Java

    The one I'm using is pdfbox-app-1.6.0.jar. It looks like your program is creating tables from an xml file. Is that correct? I don't know how to do that with pdfbox.

    Is your program successful in converting the xml file to a table in the pdf file? you should be able to create a graph now.

    What is Lipper_ManagerProfilePDFCreater? What does that class look like? Can you post the code?

  20. #20
    hiahalya is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default Re: Graphs in Java

    i have a problem in producing graphs .actually we are doing a project where the student marks are maintained .here we have to produce graphs to all the marks in different semisters.
    so i need code to produce graphs automatically when we have given inputs ...so please ...i will be glad if some one helped in thatttttt..............hoping soooooo..............


    ....i just want to produce graphs for the data in database and should automatically changed when we change the marks..
    Last edited by hiahalya; 07-16-2012 at 06:48 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with graphs
    By thelgar in forum Advanced Java
    Replies: 6
    Last Post: 01-28-2012, 05:33 PM
  2. How do I make lines and graphs in java?
    By TallOne in forum New To Java
    Replies: 5
    Last Post: 01-17-2012, 03:36 AM
  3. graphs in java
    By divyan in forum Advanced Java
    Replies: 3
    Last Post: 03-04-2011, 02:35 PM
  4. JAVA graphs & charts libraries
    By acornel in forum AWT / Swing
    Replies: 1
    Last Post: 10-24-2010, 08:11 PM
  5. Graphs
    By siddharth_s_b in forum NetBeans
    Replies: 1
    Last Post: 05-22-2008, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •