Issue
I am trying to get the x and y coordinates individually when the user clicks on one of the points of XY series charts that JFreeChart provides. Also I am doing it in a servlet using Java programming language. List
are objects that I made from a model package that has classes called Home
, and Away
. Feel free to ask more information about the servlet, as I am providing the code below.
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Object saved in application scopes two list one fore Home and Away
List<Home> forumi = (List<Home>) getServletContext().getAttribute(
"home" );
List<Away> forumi2 = (List<Away>) getServletContext().getAttribute(
"away" );
OutputStream out = response.getOutputStream();
try {
//creating an XY dataset
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("Home");
XYSeries series2 = new XYSeries("Away");
//inputing data to chart
int counter = 1;
for(int i=0; i < forumi.size(); i++) {
series1.add(counter, forumi.get(i).getScores());
++counter;
}
int counter2 = 1;
for(int i=0; i < forumi2.size(); i++) {
series2.add(counter2, forumi2.get(i).getScores());
++counter2;
}
dataset.addSeries(series1);
dataset.addSeries(series2);
/* create chart */
String chartTitle = "2019-2020";
String xAxisLabel = "game";
String yAxisLabel = "Scores";
JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,
xAxisLabel, yAxisLabel, dataset);
//Customize chart
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
// sets thickness for series (using strokes)
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesPaint(1, Color.BLUE);
// sets paint color for each series
renderer.setSeriesStroke(0, new BasicStroke(2.0f));
renderer.setSeriesStroke(1, new BasicStroke(2.0f));
//renderer.setSeriesStroke(1, new BasicStroke(3.0f));
plot.setRenderer((XYItemRenderer) renderer);
chartPanel = new ChartPanel(chart);
//System.out.print(plot.getRangeCrosshairValue());
chart.getPlot().setBackgroundPaint(Color.WHITE);
/* Set the HTTP Response Type */
response.setContentType("image/png");
/* Writes a chart to an output stream in PNG format */
ChartUtilities.writeChartAsPNG(out, chart, 1704, 300);
} catch (Exception e) {
System.err.println(e.toString()); /* Throw exceptions to log files */
} finally {
out.close();/* Close the output stream */
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} ```
Solution
As shown in this example, you can create an HTML image map corresponding to the entities in your chart for use in a servlet context.
Setting tooltips
to true
in your chosen ChartFactory
adds a StandardXYToolTipGenerator
which will show a tool tip on mouse moved events; mouse over entities in scatter.html
to see the effect; you can customize the generator as shown here and here.
Setting urls
to true
in your chosen ChartFactory
adds a StandardXYURLGenerator
which will open the generated URL on mouse clicked events; click on an entity in scatter.html
with a custom generator to see the effect:
chart.getXYPlot().getRenderer().setURLGenerator(new StandardXYURLGenerator(){
@Override
public String generateURL(XYDataset dataset, int series, int item) {
return "http://example.com";
}
});
Your actual generator can return any desired URL for particular values of dataset
, series
and item
.
Answered By - trashgod
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)