JFreeChart:鼠标悬停时带有工具提示的自定义图表
JFreeChart: custom chart with tooltip on mouseover
我需要显示动态添加数据的 XyLineChart。我使用图表定制器通过一些额外的逻辑从数据库中读取数据并将其添加到图表中。但是我无法在鼠标悬停在图表上的每个数据点时创建工具提示。以下是我的定制程序代码。
鼠标悬停时创建工具提示的正确方法是什么?
public class MyChartCustomizer extends JRAbstractChartCustomizer{
@Override
public void customize(JFreeChart chart, JRChart jrChart) {
XyPlot plot= chart.getXyPlot;
XYSeriesCollection ds = (XYSeriesCollection) plot.getDataset();
XYSeries x1 = new XYSeries("C 1", true, true);
x1.add(10,20);
XYBarRenderer ren = (XYBarRenderer) plot.getRenderer();
plot.setRenderer(ren);
ren.setSeriesToolTipGenerator(0, new XYToolTipGenerator() {
@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return "C 1";
}
});
ren.setToolTipGenerator(new XYToolTipGenerator() {
@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return "C 1";
}
});
chart.fireChartChanged();
}
}
}
谢谢 Petter 和 Trashgod 帮助我找到解决这个问题的方法。
实际问题是,如果我们使用定制器添加新数据,那么新数据点会显示在图表上,但不会生成相关的工具提示,并且用于工具提示的地图不会在 html 代码上更新。因为我必须根据要求使用 jasper 服务器,所以我实现了以下工作,其他方式将使用 jfreechart API 简单地生成带有地图的图表图像并显示在 jsp 页面上(不需要报告设计.. ..)..
以下是我实现它的方式。
我使用以下代码生成了与 jasperserver/jasper studio 创建图表相同的图表。这给了我内部创建的相同图表,我为工具提示创建地图并将其作为参数传递给浏览器并使用 javascript 函数插入新地图 html 代码和图表图像。
XYSeriesCollection xyDataSet = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYLineChart(
cur_chart.getTitle(),
cur_chart.getxLabel(), cur_chart.getyLabel(),
xyDataSet,
PlotOrientation.VERTICAL,
true,
true,
false);
String chartId = null;
for ( Object tt : chart.getSubtitles()){
if (tt instanceof TextTitle){
chartId= ((TextTitle) tt).getText();
}
}
XYPlot plot = chart.getXYPlot();
//following code to set font size and color is required so that same chart with matching tooltip pixels can we generated.
LegendItemCollection legends = plot.getLegendItems();
List<JRSeriesColor> colors = new ArrayList<JRSeriesColor>();
System.out.println("Customizer: "+ chartId);
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
Color trans = new Color(0xFF, 0xFF, 0xFF, 0);
chart.setBackgroundPaint(trans);
plot .setBackgroundPaint(trans);
chart.getLegend().setBackgroundPaint(trans);
chart.setTitle(cur_chart.getTitle());
Font font3 = new Font("Dialog", Font.PLAIN, 10);
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);
plot.getDomainAxis().setLabelPaint(Color.BLACK);
plot.getRangeAxis().setLabelPaint(Color.BLACK);
//some more code to add real time data to XyDataset,
ToolTipTagFragmentGenerator tooltipConstructor = new ToolTipTagFragmentGenerator() {
public String generateToolTipFragment(String arg0) {
String toolTip = " title = \"" + arg0.replace("\"", "") + "\"";
return (toolTip);
}
};
URLTagFragmentGenerator urlConstructor = new URLTagFragmentGenerator() {
public String generateURLFragment(String arg0) {
String address = " href=\"ControllerAddress\methodName?"
+ arg0.replace("\"", "") + "\"";
return (address);
}
};
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
// BufferedImage bi chart.createBufferedImage(272, 178, info);
TextTitle tt = new TextTitle("chart1");
tt.setFont(font3);
chart.addSubtitle(tt);
ChartUtilities.saveChartAsPNG(new File("/tmp/test.png"), chart, 500, 250, info);
String map = ChartUtilities.getImageMap(cur_chart.getName(), info, tooltipConstructor, urlConstructor);
我需要显示动态添加数据的 XyLineChart。我使用图表定制器通过一些额外的逻辑从数据库中读取数据并将其添加到图表中。但是我无法在鼠标悬停在图表上的每个数据点时创建工具提示。以下是我的定制程序代码。
鼠标悬停时创建工具提示的正确方法是什么?
public class MyChartCustomizer extends JRAbstractChartCustomizer{
@Override
public void customize(JFreeChart chart, JRChart jrChart) {
XyPlot plot= chart.getXyPlot;
XYSeriesCollection ds = (XYSeriesCollection) plot.getDataset();
XYSeries x1 = new XYSeries("C 1", true, true);
x1.add(10,20);
XYBarRenderer ren = (XYBarRenderer) plot.getRenderer();
plot.setRenderer(ren);
ren.setSeriesToolTipGenerator(0, new XYToolTipGenerator() {
@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return "C 1";
}
});
ren.setToolTipGenerator(new XYToolTipGenerator() {
@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return "C 1";
}
});
chart.fireChartChanged();
}
}
}
谢谢 Petter 和 Trashgod 帮助我找到解决这个问题的方法。 实际问题是,如果我们使用定制器添加新数据,那么新数据点会显示在图表上,但不会生成相关的工具提示,并且用于工具提示的地图不会在 html 代码上更新。因为我必须根据要求使用 jasper 服务器,所以我实现了以下工作,其他方式将使用 jfreechart API 简单地生成带有地图的图表图像并显示在 jsp 页面上(不需要报告设计.. ..).. 以下是我实现它的方式。
我使用以下代码生成了与 jasperserver/jasper studio 创建图表相同的图表。这给了我内部创建的相同图表,我为工具提示创建地图并将其作为参数传递给浏览器并使用 javascript 函数插入新地图 html 代码和图表图像。
XYSeriesCollection xyDataSet = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYLineChart(
cur_chart.getTitle(),
cur_chart.getxLabel(), cur_chart.getyLabel(),
xyDataSet,
PlotOrientation.VERTICAL,
true,
true,
false);
String chartId = null;
for ( Object tt : chart.getSubtitles()){
if (tt instanceof TextTitle){
chartId= ((TextTitle) tt).getText();
}
}
XYPlot plot = chart.getXYPlot();
//following code to set font size and color is required so that same chart with matching tooltip pixels can we generated.
LegendItemCollection legends = plot.getLegendItems();
List<JRSeriesColor> colors = new ArrayList<JRSeriesColor>();
System.out.println("Customizer: "+ chartId);
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
Color trans = new Color(0xFF, 0xFF, 0xFF, 0);
chart.setBackgroundPaint(trans);
plot .setBackgroundPaint(trans);
chart.getLegend().setBackgroundPaint(trans);
chart.setTitle(cur_chart.getTitle());
Font font3 = new Font("Dialog", Font.PLAIN, 10);
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);
plot.getDomainAxis().setLabelPaint(Color.BLACK);
plot.getRangeAxis().setLabelPaint(Color.BLACK);
//some more code to add real time data to XyDataset,
ToolTipTagFragmentGenerator tooltipConstructor = new ToolTipTagFragmentGenerator() {
public String generateToolTipFragment(String arg0) {
String toolTip = " title = \"" + arg0.replace("\"", "") + "\"";
return (toolTip);
}
};
URLTagFragmentGenerator urlConstructor = new URLTagFragmentGenerator() {
public String generateURLFragment(String arg0) {
String address = " href=\"ControllerAddress\methodName?"
+ arg0.replace("\"", "") + "\"";
return (address);
}
};
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
// BufferedImage bi chart.createBufferedImage(272, 178, info);
TextTitle tt = new TextTitle("chart1");
tt.setFont(font3);
chart.addSubtitle(tt);
ChartUtilities.saveChartAsPNG(new File("/tmp/test.png"), chart, 500, 250, info);
String map = ChartUtilities.getImageMap(cur_chart.getName(), info, tooltipConstructor, urlConstructor);