You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JFreeChart的扇区标签中嵌入图标以传递额外信息?

在JFreeChart扇区标签中嵌入图标的实现方法

要实现类似ChartDirector的扇区标签嵌入图标效果,核心是自定义JFreeChart的饼图标签生成器,通过AttributedString将图标与文本组合。具体步骤如下:

1. 自定义带图标的标签生成器

实现PieSectionLabelGenerator接口,重写支持富文本的generateAttributedSectionLabel方法,将图标作为图形属性插入到标签文本中:

import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.data.general.PieDataset;
import java.text.AttributedString;
import java.text.AttributedCharacterIterator;
import java.awt.image.BufferedImage;
import java.awt.*;
import javax.imageio.ImageIO;
import java.io.File;

public class IconPieLabelGenerator implements PieSectionLabelGenerator {

    private final PieSectionLabelGenerator baseGenerator;
    private final BufferedImage[] icons;

    public IconPieLabelGenerator(PieSectionLabelGenerator baseGenerator, BufferedImage[] icons) {
        this.baseGenerator = baseGenerator;
        this.icons = icons;
    }

    @Override
    public String generateSectionLabel(PieDataset dataset, Comparable key) {
        return baseGenerator.generateSectionLabel(dataset, key);
    }

    @Override
    public AttributedString generateAttributedSectionLabel(PieDataset dataset, Comparable key) {
        // 基于默认生成器获取基础文本
        AttributedString attrStr = baseGenerator.generateAttributedSectionLabel(dataset, key);
        if (attrStr == null) {
            String label = generateSectionLabel(dataset, key);
            attrStr = new AttributedString(label);
        }

        // 根据扇区索引匹配对应图标
        int index = dataset.getIndex(key);
        if (index >= 0 && index < icons.length) {
            BufferedImage icon = icons[index];
            // 创建图标属性,顶部对齐文本
            GraphicAttribute iconAttr = new ImageGraphicAttribute(icon, GraphicAttribute.TOP_ALIGNMENT);
            
            // 在文本前添加空格并替换为图标,避免覆盖原文本
            String originalText = attrStr.getIterator().toString();
            attrStr = new AttributedString(" " + originalText);
            attrStr.addAttribute(TextAttribute.CHAR_REPLACEMENT, iconAttr, 0, 1);
        }
        return attrStr;
    }
}

2. 集成到JFreeChart饼图中

加载图标资源,创建饼图并设置自定义标签生成器,同时调整标签排版参数:

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.File;

public class IconPieChartDemo {
    public static void main(String[] args) throws Exception {
        // 构建饼图数据集
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("类别A", 35);
        dataset.setValue("类别B", 45);
        dataset.setValue("类别C", 20);

        // 加载图标(建议提前缩放到与文本行高匹配的尺寸)
        BufferedImage iconA = ImageIO.read(new File("icon_a.png"));
        BufferedImage iconB = ImageIO.read(new File("icon_b.png"));
        BufferedImage iconC = ImageIO.read(new File("icon_c.png"));
        BufferedImage[] icons = {iconA, iconB, iconC};

        // 创建基础标签生成器(自定义显示格式)
        StandardPieSectionLabelGenerator baseGenerator = 
            new StandardPieSectionLabelGenerator("{0}: {1} ({2})");
        // 包装为带图标的生成器
        IconPieLabelGenerator iconGenerator = new IconPieLabelGenerator(baseGenerator, icons);

        // 创建饼图
        JFreeChart chart = ChartFactory.createPieChart(
            "带图标标签的饼图",
            dataset,
            true,  // 显示图例
            true,  // 显示工具提示
            false  // 不生成URL
        );

        PiePlot plot = (PiePlot) chart.getPlot();
        // 设置自定义标签生成器
        plot.setLabelGenerator(iconGenerator);
        // 调整标签间距与字体,优化排版
        plot.setLabelGap(0.02);
        plot.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));

        // 导出或显示图表
        // ChartUtilities.saveChartAsPNG(new File("icon_pie.png"), chart, 600, 400);
    }
}

注意事项

  • 图标尺寸需与文本行高匹配,过大的图标会导致标签排版混乱,可通过Image.getScaledInstance()提前缩放。
  • 若需将图标放在文本右侧,可修改文本拼接逻辑,将图标插入到文本末尾。
  • 图标资源可从类路径加载(使用getClass().getResource("/icons/icon_a.png")),避免文件路径问题。

内容的提问来源于stack exchange,提问作者Bishop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 08:42:38