我正在使用JFreeChart制作折线图。我想在我的折线图上设置工具提示,但我无法在Servlet上执行此操作。那么请帮我在Servlet上设置工具提示吗?
XYSeriesCollection dataset = new XYSeriesCollection();
for (int i = 0; i < 2; i++) {
XYSeries series = new XYSeries("Name"+i);
for (int j = 0; j < 10; j++) {
series.add(i+j,j+i*j);
}
dataset.addSeries(series);
}
JFreeChart chart= ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y", dataset, PlotOrientation.VERTICAL, true, true,false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setShapesFilled(true);
Vector<String> ToolTips1 = new Vector<String>();
for (int j = 0; j < 10; j++)
{
ToolTips1.add("First: "+j);
}
Vector<String> ToolTips2 = new Vector<String>();
for (int j = 0; j < 10; j++)
{
ToolTips2.add("Second: "+j);
}
CustomXYToolTipGenerator generator1 = new CustomXYToolTipGenerator();
generator1.addToolTipSeries(ToolTips1);
CustomXYToolTipGenerator generator2 = new CustomXYToolTipGenerator();
generator2.addToolTipSeries(ToolTips2);
renderer.setSeriesToolTipGenerator(0, generator1);
renderer.setSeriesToolTipGenerator(1, generator2);
renderer.setBaseShape(new Ellipse2D.Double(-3.0, -3.0, 8.0, 8.0));
plot.setRenderer(renderer);
response.setContentType("image/png");
RenderedImage chartImage=chart.createBufferedImage(500,500);
ImageIO.write(chartImage,"png",out);
答案 0 :(得分:3)
public static JFreeChart createAreaChart(java.lang.String title,
java.lang.String categoryAxisLabel,
java.lang.String valueAxisLabel,
CategoryDataset dataset,
PlotOrientation orientation,
boolean legend,
boolean tooltips,
boolean urls)
使用默认设置创建面积图。此方法返回的图表对象使用CategoryPlot实例作为绘图,域轴为CategoryAxis,范围轴为NumberAxis,渲染器为AreaRenderer。
参数:
<强>返回:强>
面积图。
答案 1 :(得分:1)
XYToolTipGenerator xyToolTipGenerator = new XYToolTipGenerator()
{
public String generateToolTip(XYDataset dataset, int series, int item)
{
Number x1 = dataset.getX(series, item);
Number y1 = dataset.getY(series, item);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("<html><p style='color:#0000ff;'>Serie: '%s'</p>", dataset.getSeriesKey(series)));
stringBuilder.append(String.format("X:'%d'<br/>", x1.intValue()));
stringBuilder.append(String.format("Y:'%d'", y1.intValue()));
stringBuilder.append("</html>");
return stringBuilder.toString();
}
};
XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) plot.getRenderer();
render.setBaseToolTipGenerator(xyToolTipGenerator);