Plotting points from in x-y chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ankita
Posts: 1
Joined: Thu Mar 16, 2017 4:49 pm
antibot: No, of course not.

Plotting points from in x-y chart

Post by Ankita » Thu Mar 16, 2017 5:00 pm

I am using Tomcat 8, oracle 10g and java 1.8
i am trying to plot some points (x and y coordinates) from database.
I have written the following code. I am able to compile my code. but am unable to run it in my server using google chrome.


This is my code

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import javax.swing.*;
import java.awt.*;
import org.jfree.chart.ChartUtilities;

public class ChartViewer extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
try
{res.setContentType("text/html");
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("production");
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","1408");
Statement stmt=con.createStatement();

String q1="select * from LOCATION";
ResultSet rs=stmt.executeQuery(q1);

while(rs.next()){
int x=rs.getInt(2);

int y=rs.getInt(3);
series.add(x,y) ;

}
con.close();
dataset.addSeries(series);

JFreeChart chart = ChartFactory.createScatterPlot(
"LOCATION", // title
"X", "Y", // axis labels
dataset, // dataset
PlotOrientation.VERTICAL,
true, // legend? yes
true, // tooltips? yes
false // URLs? no
);
//ChartPanel chartPanel = new ChartPanel(chart);
//chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
//setContentPane(chartPanel);
ChartUtilities.saveChartAsJPEG(new File("C:/imagechart.jpg"), chart, 500, 250);
PrintWriter pw=res.getWriter();
pw.println("<img src='file:///C:/imagechart.jpg'>");


}
catch(Exception e){
System.out.println(e);
}
}


}

and this is my web.xml
<web-app>
<servlet>
<servlet-name>ChartViewer</servlet-name>
<servlet-class>ChartViewer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChartViewer</servlet-name>
<url-pattern>/chartviewer</url-pattern>
</servlet-mapping>
</web-app>

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Plotting points from in x-y chart

Post by david.gilbert » Tue Mar 21, 2017 5:46 pm

Your code will try to write the image to the c: on the server (not sure that will succeed). Then, the (incomplete) HTML you return to the client is referencing the c: on the client machine...it won't find any image there.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked