我希望有人可以帮助我设置自定义标签的域轴刻度标签由JASPER报告创建jFreeChart内。我试过了我在网上找到的所有东西,仍然没有骰子。这里是我的代码:jFreeChart自定义域轴标签
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.text.TextBlock;
import org.jfree.text.TextUtilities;
import org.jfree.ui.RectangleEdge;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;
public class ChartCustomizer implements JRChartCustomizer{
public class CustomColorRenderer extends BarRenderer {
private static final long serialVersionUID = -9045170581109026224L;
@Override
public Paint getItemPaint(int row, int col) {
CategoryDataset currentDataset = getPlot().getDataset();
String columnKey = (String) currentDataset.getColumnKey(col);
String[] columnKeyValues = columnKey.split(":");
if(columnKeyValues.length < 2) return getSeriesPaint(row);
String columnActualEstimated = columnKeyValues[2];
if(columnActualEstimated.equals("A")) {
return Color.RED;
} else if(columnActualEstimated.equals("E")) {
return Color.BLUE;
}
return getSeriesPaint(row);
}
}
public void customize(JFreeChart chart, JRChart jasperChart)
{
if(jasperChart.getChartType() == JRChart.CHART_TYPE_BAR) {
CategoryPlot plot = chart.getCategoryPlot();
CategoryDataset currentDataset = plot.getDataset();
double maxValue = Double.MIN_VALUE;
// Scan to get total max value for the chart in order to set chart height appropriately
for(int i = 0; i < currentDataset.getRowCount(); i++) {
//System.out.println(i);
for(int j = 0; j < currentDataset.getColumnCount(); j++) {
Number numberValue = currentDataset.getValue(i, j);
//System.out.println("Column " + j + " key: " + currentDataset.getColumnKey(j));
double value = numberValue == null ? Double.NaN : numberValue.doubleValue();
if(value > maxValue) {
maxValue = value;
}
}
}
// Add 10% to top margin
double tenPercent = maxValue * 0.1;
maxValue = (Math.round((maxValue * 1.1)/tenPercent) * tenPercent) + tenPercent;
// Set max bar height to max value
ValueAxis yAxis = plot.getRangeAxis();
yAxis.setAutoRange(false);
yAxis.setRange(0, maxValue);
CategoryAxis xAxis = plot.getDomainAxis();
// Set label font size
xAxis.setTickLabelFont(new Font("Arial", Font.PLAIN, 4));
// Will set single bar colors by value with a custom renderer
CustomColorRenderer customRenderer = new CustomColorRenderer();
// Set the chart to apply the custom renderer
plot.setRenderer(customRenderer);
}
}
}
这里是我的图表看起来像目前:
注意域轴显示键,例如 “1:N:A”。在这种情况下,1表示订单,N表示11月份,A表示这两个系列的值为“实际”与“预计”。我想要做的就是将可见刻度标签更改为“Nov”,即“1:N:A”示例。像自定义标签生成器这样的东西会更改图表其他部分的标签,而不是标签。我可以成功设置刻度标签字体,但似乎无法让标签本身发生变化。
编辑:关于这种情况的另一个棘手的部分是,要求显示包括前11个,当前和即将到来的13个月。即将到来的月份总是一个估计值,因此是“A”和“E”系列)。这使得它很痛苦,因为这意味着总是有重复的月份,因此需要合并的列。
任何帮助,将不胜感激。让我知道是否需要更多信息。
Crossposted到http://www.jfree.org/forum/viewtopic.php?f=3&t=117811
根据您的建议,我从现有的数据集键中创建了一个SymbolAxis,但这样做的问题是似乎没有办法将新的SymbolAxis设置为域轴,因为图的'setDomainAxis()'只需要一个'CategoryAxis',而图的'setRangeAxis()'确实需要'SymbolAxis'作为输入。我错过了什么吗? – Kettch19
糟糕,我的错误; “SymbolAxis”不适合您的类别图的域轴 – trashgod