Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jasper reports 图像作为iReport图表中的类别标签_Jasper Reports_Jfreechart - Fatal编程技术网

Jasper reports 图像作为iReport图表中的类别标签

Jasper reports 图像作为iReport图表中的类别标签,jasper-reports,jfreechart,Jasper Reports,Jfreechart,我正在尝试创建一个带有类别标签(x轴标签)图像的条形图。对于默认条形图,类别表达式的结果用作标签(并且必须是字符串)。我如何自定义它以显示图像?我已经编写了一个scriptlet来用BuffereImage填充变量,现在我只需要一种使用它们的方法。我可以用图表定制器类来实现这一点吗?有更简单/更好的方法吗?正如您所说,您可以通过自己的customizer类来实现这一点。以下是一个简单的示例,可根据您的需要进行增强: @Slf4j public class CategoryAxisWithIma

我正在尝试创建一个带有类别标签(x轴标签)图像的条形图。对于默认条形图,类别表达式的结果用作标签(并且必须是字符串)。我如何自定义它以显示图像?我已经编写了一个scriptlet来用BuffereImage填充变量,现在我只需要一种使用它们的方法。我可以用图表定制器类来实现这一点吗?有更简单/更好的方法吗?

正如您所说,您可以通过自己的customizer类来实现这一点。以下是一个简单的示例,可根据您的需要进行增强:

@Slf4j 
public class CategoryAxisWithImagesCustomizer extends JRAbstractChartCustomizer {
    public class CategoryAxisWithImages extends CategoryAxis {
        public CategoryAxisWithImages(String label) {
            super(label);
        }

        @Override
        protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
            // enter max width and height of your images or you can do it dynamically
            return new Rectangle2D.Double(0, 0, 32, 32);
        }

        @Override
        protected AxisState drawCategoryLabels(Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state, PlotRenderingInfo plotState) {
            if (!isTickLabelsVisible()) {
                return state;
            }

            List ticks = refreshTicks(g2, state, plotArea, edge);
            state.setTicks(ticks);
            for (int i = 0; i < ticks.size(); i++) {
                double x = getCategoryMiddle(i, ticks.size(), dataArea, edge);
                double y = state.getCursor() + getCategoryLabelPositionOffset();

                int value = (int) ((CategoryPlot) getPlot()).getDataset().getColumnKey(i);
                String imagePath = "logo_" + value + ".png";
                try {
                    InputStream imageStream = getClass().getResourceAsStream(imagePath);
                    // you can of course load images using different way - here I'm using index value from the dataset
                    BufferedImage image = ImageIO.read(imageStream);
                    g2.drawImage(image, (int) (x - image.getWidth() / 2d), (int) (y), image.getWidth(), image.getHeight(), Color.black, null);
                } catch (IOException e) {
                    log.error("Cannot load image {}", imagePath);
                }
            }
            state.cursorDown(state.getMax() + getCategoryLabelPositionOffset());
            return state;
        }
    }

    @Override
    public void customize(JFreeChart chart, JRChart jasperChart) {
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis categoryAxis = new CategoryAxisWithImages(plot.getDomainAxis().getLabel());
        plot.setDomainAxis(categoryAxis);
    }
}
@Slf4j
带有ImagesCustomizer的公共类CategoryAxis扩展了JRactChartCustomizer{
带有图像的公共类CategoryAxis扩展了CategoryAxis{
公共类别AxisWithImages(字符串标签){
超级(标签);
}
@凌驾
受保护的矩形2D GetLabelnClosure(图形2D g2,矩形边){
//输入图像的最大宽度和高度,也可以动态输入
返回新的矩形2d.Double(0,0,32,32);
}
@凌驾
受保护的AxisState绘图类别标签(Graphics2D g2、矩形2D绘图区、矩形2D数据区、矩形边缘、AxisState状态、绘图渲染信息绘图状态){
如果(!isTickLabelsVisible()){
返回状态;
}
列表标记=刷新标记(g2、状态、绘图区域、边缘);
state.setTicks(ticks);
对于(int i=0;i
在JasperReport JRXML中,您可以像这样注册该定制器类:

    <barChart>
        <chart customizerClass="cz.trask.experiment.jr.CategoryAxisWithImagesCustomizer"
               isShowLegend="false">
            <reportElement x="0" y="0" width="550" height="348" uuid="a2cbb6b5-a76d-469b-8e8e-daa533260f20"/>
            <chartTitle/>
            <chartSubtitle/>
            <chartLegend/>
        </chart>
        ...
    </barChart>

...
我的例子的结果是:

正如您所说,您可以通过自己的customizer类来实现这一点。以下是一个简单的示例,可根据您的需要进行增强:

@Slf4j 
public class CategoryAxisWithImagesCustomizer extends JRAbstractChartCustomizer {
    public class CategoryAxisWithImages extends CategoryAxis {
        public CategoryAxisWithImages(String label) {
            super(label);
        }

        @Override
        protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
            // enter max width and height of your images or you can do it dynamically
            return new Rectangle2D.Double(0, 0, 32, 32);
        }

        @Override
        protected AxisState drawCategoryLabels(Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, AxisState state, PlotRenderingInfo plotState) {
            if (!isTickLabelsVisible()) {
                return state;
            }

            List ticks = refreshTicks(g2, state, plotArea, edge);
            state.setTicks(ticks);
            for (int i = 0; i < ticks.size(); i++) {
                double x = getCategoryMiddle(i, ticks.size(), dataArea, edge);
                double y = state.getCursor() + getCategoryLabelPositionOffset();

                int value = (int) ((CategoryPlot) getPlot()).getDataset().getColumnKey(i);
                String imagePath = "logo_" + value + ".png";
                try {
                    InputStream imageStream = getClass().getResourceAsStream(imagePath);
                    // you can of course load images using different way - here I'm using index value from the dataset
                    BufferedImage image = ImageIO.read(imageStream);
                    g2.drawImage(image, (int) (x - image.getWidth() / 2d), (int) (y), image.getWidth(), image.getHeight(), Color.black, null);
                } catch (IOException e) {
                    log.error("Cannot load image {}", imagePath);
                }
            }
            state.cursorDown(state.getMax() + getCategoryLabelPositionOffset());
            return state;
        }
    }

    @Override
    public void customize(JFreeChart chart, JRChart jasperChart) {
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis categoryAxis = new CategoryAxisWithImages(plot.getDomainAxis().getLabel());
        plot.setDomainAxis(categoryAxis);
    }
}
@Slf4j
带有ImagesCustomizer的公共类CategoryAxis扩展了JRactChartCustomizer{
带有图像的公共类CategoryAxis扩展了CategoryAxis{
公共类别AxisWithImages(字符串标签){
超级(标签);
}
@凌驾
受保护的矩形2D GetLabelnClosure(图形2D g2,矩形边){
//输入图像的最大宽度和高度,也可以动态输入
返回新的矩形2d.Double(0,0,32,32);
}
@凌驾
受保护的AxisState绘图类别标签(Graphics2D g2、矩形2D绘图区、矩形2D数据区、矩形边缘、AxisState状态、绘图渲染信息绘图状态){
如果(!isTickLabelsVisible()){
返回状态;
}
列表标记=刷新标记(g2、状态、绘图区域、边缘);
state.setTicks(ticks);
对于(int i=0;i
在JasperReport JRXML中,您可以像这样注册该定制器类:

    <barChart>
        <chart customizerClass="cz.trask.experiment.jr.CategoryAxisWithImagesCustomizer"
               isShowLegend="false">
            <reportElement x="0" y="0" width="550" height="348" uuid="a2cbb6b5-a76d-469b-8e8e-daa533260f20"/>
            <chartTitle/>
            <chartSubtitle/>
            <chartLegend/>
        </chart>
        ...
    </barChart>

...
我的例子的结果是:

好的,这看起来真的很好,非常感谢!我的最后一个任务是从customizer类中访问图像文件。我可以通过列键这样做,但是有没有一种方法可以通过getParameterValue()访问变量?我一直想这样做,但也许这会带来更多的麻烦。是的,你可以