【问题标题】:jFreeChart custom domain axis labelsjFreeChart 自定义域轴标签
【发布时间】:2017-04-26 15:53:25
【问题描述】:

我希望有人可以帮助我为 Jasper Reports 创建的 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 指的是“实际”与“估计”的值,它们是两个系列。对于“1:N:A”示例,我想做的只是将可见的刻度标签更改为“Nov”。自定义标签生成器之类的东西会更改图表其他部分的标签,而不是刻度标签。我可以成功设置刻度标签字体,但似乎无法让标签本身改变。

编辑:关于这种情况的另一个棘手的部分是要求显示 13 个月,包括前 11 个月、当前和即将到来的。即将到来的月份总是一个估计值,因此是“A”和“E”系列)。这让人很痛苦,因为这意味着总是有一个重复的月份,因此需要合并的列。

任何帮助将不胜感激。如果需要更多信息,请告诉我。

转帖到http://www.jfree.org/forum/viewtopic.php?f=3&t=117811

【问题讨论】:

    标签: java jasper-reports jfreechart


    【解决方案1】:

    自定义CategoryItemLabelGenerator,通常用于标记条形,可能不是正确的选择。如here 所示,CategoryAxis 通过绘图的getCategoriesForAxis() 方法从CategoryDataset 的列键中获取类别标签的文本。您可以在创建数据集时指定所需的键。

    【讨论】:

    • 根据您的建议,我从现有的数据集键创建了一个 SymbolAxis,它可以工作,但这样做的问题是似乎没有办法将新的 SymbolAxis 设置为域轴,因为绘图的 setDomainAxis() 仅采用 CategoryAxis 而绘图的 setRangeAxis() 确实采用 SymbolAxis 作为输入。我错过了什么吗?
    • 糟糕,我的错误; SymbolAxis 不适合您的类别图的域轴
    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多