问题描述
在条形图中,我想对每种颜色(或分段)使用超链接。为此,我需要使用Image MAP(使用Jfree图表)。
我的代码通过Jfree图表创建图像,但是我还希望每个颜色代码都具有单独的超链接。
您能否建议我如何根据颜色代码或行键或列键从Jfree图表的图像中获取坐标。
In bar chart, I want to use hyperlinks for every color(or segment). To make this I need to use Image MAP (using Jfree chart).My code is creating image through Jfree chart, but I also want individual hyperlink with every color code.Can you please advice how I can get coordinates from Jfree chart's image based on color code or row key or column key.
公共类演示
{
public class Demo {
public static void main(String arg[]) throws IOException{
DefaultCategoryDataset result = new DefaultCategoryDataset();
result.addValue(20.3, "Apraisal Forms", "Dtest");
result.addValue(19.4, "Resignation Form", "Dtest");
result.addValue(16.5, "HES forms", "Dtest");
result.addValue(8, "Feedback forms", "Dtest");
result.addValue(27.2, "Apraisal Forms", "HR Dept");
result.addValue(5.9, "Resignation Form", "HR Dept");
result.addValue(14.4, "Feedback forms", "HR Dept");
result.addValue(4, "HES forms", "HR Dept");
JFreeChart chart = ChartFactory.createStackedBarChart3D("overview by Departments", "x label", "Message Count", result, PlotOrientation.VERTICAL, true, true, true);
ChartUtilities.saveChartAsJPEG(new File("D:\\chart.jpg"), chart, 500, 300);
}
}
推荐答案
I通过实现 ToolTipTagFragmentGenerator
和 URLTagFragmentGenerator
完成此任务。这是完整的代码:
I got it done by implementing ToolTipTagFragmentGenerator
and URLTagFragmentGenerator
. Here is the full code:
public class Demo {
public static void main(String arg[]) throws IOException {
DefaultCategoryDataset result = new DefaultCategoryDataset();
result.addValue(20.3, "Apraisal Forms", "Dtest");
result.addValue(19.4, "Resignation Form", "Dtest");
result.addValue(16.5, "HES forms", "Dtest");
result.addValue(8, "Feedback forms", "Dtest");
result.addValue(27.2, "Apraisal Forms", "HR Dept");
result.addValue(5.9, "Resignation Form", "HR Dept");
result.addValue(14.4, "Feedback forms", "HR Dept");
result.addValue(4, "HES forms", "HR Dept");
result.addValue(18.4, "Resignation Form", "Admin Dept");
result.addValue(16.1, "HES forms", "Admin Dept");
result.addValue(13.7, "Feedback forms", "Admin Dept");
JFreeChart chart = ChartFactory.createStackedBarChart3D(
"overview by Departments", "x label", "Message Count", result,
PlotOrientation.VERTICAL, true, true, true);
SubCategoryAxis domainAxis = new SubCategoryAxis("Name of Department");
domainAxis.setCategoryMargin(0.60);
CategoryPlot plot = chart.getCategoryPlot();
plot.setDomainAxis(domainAxis);
String map = "";
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ChartUtilities.writeChartAsPNG(out, chart, 600, 400, info);
ToolTipTagFragmentGenerator tooltipConstructor = new ToolTipTagFragmentGenerator() {
public String generateToolTipFragment(String arg0) {
String toolTip = " title = \"value" + arg0 + "\"";
return (toolTip);
}
};
URLTagFragmentGenerator urlConstructor = new URLTagFragmentGenerator() {
public String generateURLFragment(String arg0) {
String address = " href=\"ControllerAddress\\methodName?"
+ arg0 + "\"";
return (address);
}
};
map = ChartUtilities.getImageMap("chart", info, tooltipConstructor,
urlConstructor);
out.close();
} catch (IOException e) {
System.out.println(e);
}
System.out.println(map);
ChartFrame frame1 = new ChartFrame("Bar Chart", chart);
frame1.setVisible(true);
frame1.setSize(600, 350);
}
}
这篇关于如何从Jfree图表的图像获取图像地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!