单击图表,JFreeChart XY系列坐标



当用户单击JFreeChart提供的XY系列图表的一个点时,我正试图分别获取x和y坐标。此外,我正在使用Java编程语言在servlet中执行此操作。List是我从一个模型包中创建的对象,该模型包具有名为HomeAway的类。请随意询问有关servlet的更多信息,因为我将提供下面的代码。

/**
* @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);
} ```

如本例所示,您可以创建与图表中的实体相对应的HTML图像映射,以便在servlet上下文中使用。

在您选择的ChartFactory中将tooltips设置为true会添加一个StandardXYToolTipGenerator,它将显示鼠标移动事件的工具提示;鼠标悬停在scatter.html中的实体上查看效果;您可以自定义生成器,如这里和这里所示。

在您选择的ChartFactory中将urls设置为true会添加一个StandardXYURLGenerator,它将在鼠标单击事件时打开生成的URL;单击scatter.html中具有自定义生成器的实体以查看效果:

chart.getXYPlot().getRenderer().setURLGenerator(new StandardXYURLGenerator(){
@Override
public String generateURL(XYDataset dataset, int series, int item) {
return "http://example.com";
}
});

实际生成器可以为datasetseriesitem的特定值返回任何所需的URL。

相关内容

  • 没有找到相关文章

最新更新