0.785 (45°) 或 1.57 (90°)。false(水平)。net.sf.jasperreports.chart.category.axis.tick.label.margin 設置較大值。<categoryAxisFormat>
<axisFormat tickLabelRotation="0.785"
tickLabelMask=" "
verticalTickLabels="false"
labelFontName="SansSerif" labelFontSize="8"/>
</categoryAxisFormat>
在 JRXML 中直接設置 tickLabelRotation 和字體縮小通常能解決 80% 情況。
public class BarChartCustomizer implements JRChartCustomizer {
@Override
public void customize(JFreeChart chart, JRChart jrChart) {
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
// 1. 標簽 45° 傾斜
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4)
);
// 或垂直:CategoryLabelPositions.UP_90
// 2. 字體縮小
domainAxis.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 8));
// 3. 關鍵:設置最大標簽行數(實現換行)
domainAxis.setMaximumCategoryLabelLines(3); // 最多 3 行
domainAxis.setCategoryLabelWidthRatio(0.5f); // 寬度比例
// 4. 避免重疊:標準間距
domainAxis.setLowerMargin(0.02);
domainAxis.setUpperMargin(0.02);
domainAxis.setCategoryMargin(0.1);
}
}
報表屬性 → **Customizer Class** 填寫全類名。
iReport 本身不支持滾動,**需在前端容器實現**:
<div style="width: 100%; overflow-x: auto; white-space: nowrap;">
<!-- 這裡嵌入導出的圖表圖片或 SVG -->
<img src="report.html" />
</div>
| 策略 | 說明 |
|---|---|
| 數據分組/分頁 | 每圖顯示 15-20 個類別,生成多張圖或分頁報表 |
| Top N + 其他 | 只顯示前 10 大類,其餘合併為「其他」 |
| 縮寫/編碼 | 標簽用簡稱,提示框顯示全名 |
| 改用橫向條形圖 | Y 軸放標簽,天然寬度充足,無需旋轉 |
overflow-x: auto 容器。