Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: scatterplot chart

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default scatterplot chart

    Hi,
    can any body suggest me code for (jfreechart optional)scatterplot(dot plot)graph in jsp.
    i try it in java its working fine,but i want it in jsp,the data should be from database,
    iam adding my code
    !----------------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------------------------
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Shape;
    import java.util.*;
    import javax.swing.JPanel;
    import org.jfree.chart.*;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYItemRenderer;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    import org.jfree.util.ShapeUtilities;
    import java.sql.*;

    public class SPlotfinal extends ApplicationFrame {

    public SPlotfinal(String s) {
    super(s);
    JPanel jpanel = createDemoPanel();
    jpanel.setPreferredSize(new Dimension(640, 480));
    add(jpanel);
    }
    public static JPanel createDemoPanel() {
    JFreeChart jfreechart = ChartFactory.createScatterPlot(
    "Dot Plot ", "X", "Y", samplexydataset2(),
    PlotOrientation.VERTICAL, true, true, false);
    Shape cross = ShapeUtilities.createDiagonalCross(3, 1);
    XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
    xyPlot.setDomainCrosshairVisible(true);
    xyPlot.setRangeCrosshairVisible(true);
    XYItemRenderer renderer = xyPlot.getRenderer();
    renderer.setSeriesShape(0, cross);
    renderer.setSeriesPaint(0, Color.red);
    return new ChartPanel(jfreechart);
    }

    private static XYDataset samplexydataset2() {
    int cols = 20;
    int rows = 20;
    double[][] values = new double[cols][rows];
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
    XYSeries series = new XYSeries("GRAPH");
    Random rand = new Random();

    try
    {

    String connectionURL = "jdbc:mysql://localhost:3306/mysql";
    Connection con;
    Class.forName("com.mysql.jdbc.Driver");
    // Get a Connection to the database
    con = DriverManager.getConnection(connectionURL, "root", "root");
    //Add the data into the database
    String sql = "SELECT COUNT FROM GRAPH";
    Statement stm = con.createStatement();
    stm.executeQuery(sql);
    ResultSet rs= stm.getResultSet();

    while (rs.next ()){
    int count=rs.getInt(1);
    System.out.println(count);
    String sql1 = "SELECT XAXIS,YAXIS FROM GRAPH";
    Statement stm1 = con.createStatement();
    stm1.executeQuery(sql1);
    ResultSet rs1= stm1.getResultSet();

    while (rs1.next ()){

    for(int k=0; k<=count; k++)
    {

    double x = rs1.getInt(1);
    double y = rs1.getInt(2);
    series.add(x, y);
    }
    }
    }

    }

    catch(Exception e){
    System.out.println(e);
    }
    xySeriesCollection.addSeries(series);
    return xySeriesCollection;
    }

    public static void main(String args[]) {
    SPlotfinal scatterplotdemo4 = new SPlotfinal("GRAPH-KALYANI");
    scatterplotdemo4.pack();
    RefineryUtilities.centerFrameOnScreen(scatterplotd emo4);
    scatterplotdemo4.setVisible(true);
    }


    }
    ------------------------------------------------------------------------------------------------
    ------------------------------------------------------------------------------------------------
    plz help me
    thanks in advance,


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: scatterplot chart

    Write a servlet and set the header to be an image. Your jsp page wishing to display the graph would call this servlet using the img html tag. The servlet would then need to first set its header to indicate to clients it is an image, then use JFreeChart to draw to to a BufferedImage and output that image. There are many tutorials online for how to accomplish writing the servlet.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: scatterplot chart

    Hi copeg,

    Thanks for the reply.
    Iam new to java,i can't get ur answer clearly.
    in my example, columns names(xaxis,yaxis)are dynamically changes.
    those i will get from another jsp.
    can u suggest me any other solution.

    thank you,

Similar Threads

  1. [SOLVED] JFree chart Exception
    By BerilChandra in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 10th, 2011, 08:52 PM
  2. gui to make a flow chart.
    By Abhinav in forum AWT / Java Swing
    Replies: 1
    Last Post: January 27th, 2010, 02:04 AM
  3. How to create pie or other diagrams in jsp page?
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 06:44 AM