如何从Jfree图表的图像中获取图像映射

时间:2014-02-07 12:17:49

标签: java html5 charts jasper-reports jfreechart

在条形图中,我想为每种颜色(或段)使用超链接。为此,我需要使用Image MAP(使用Jfree图表)。 我的代码是通过Jfree图表创建图像,但我也想要每个颜色代码的单独超链接。 能否请您根据颜色代码或行键或列键从Jfree图表的图像中获取坐标。

公共课演示 {

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);
      }
    }

1 个答案:

答案 0 :(得分:1)

我是通过实施ToolTipTagFragmentGeneratorURLTagFragmentGenerator来完成的。这是完整的代码:

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);
    }
}