jfreechart - 将总标签添加到堆积条形图

时间:2012-07-24 08:24:00

标签: java jfreechart

是否有可能在标签中显示stcaked bar总量 我正在添加一个示例,其中为每个系列添加标签

enter image description here

stackedxybarrenderer.setSeriesItemLabelGenerator(index, new MyItemLabelGenerator());

没有做到这一点。它在系列中绘制标签(在小方块内)。 有什么建议?

我正在使用此(来自omnline示例)

private JFreeChart createChart(TableXYDataset tablexydataset) {
    DateAxis dateaxis = new DateAxis("Date");
    dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    NumberAxis numberaxis = new NumberAxis("Y");
    StackedXYBarRenderer stackedxybarrenderer = new StackedXYBarRenderer(0.10000000000000001D);

    stackedxybarrenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
    stackedxybarrenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
    stackedxybarrenderer.setBaseItemLabelsVisible(true);
    stackedxybarrenderer.setDrawBarOutline(false);
    XYPlot xyplot = new XYPlot(tablexydataset, dateaxis, numberaxis, stackedxybarrenderer);
    JFreeChart jfreechart = new JFreeChart("Stacked XY Bar Chart demo 2", xyplot);
    return jfreechart;
}

 private TableXYDataset createDataset() {
    TimeTableXYDataset timetablexydataset = new TimeTableXYDataset();
    Day day = new Day(1, 3, 2005);
    Day day1 = new Day(2, 3, 2005);
    Day day2 = new Day(3, 3, 2005);
    Day day3 = new Day(4, 3, 2005);
    Day day4 = new Day(5, 3, 2005);
    timetablexydataset.add(day, 1.0D, "Series 1");
    timetablexydataset.add(day1, 1.7D, "Series 1");
    timetablexydataset.add(day2, 2.2999999999999998D, "Series 1");
    timetablexydataset.add(day3, 3.7000000000000002D, "Series 1");
    timetablexydataset.add(day4, 2.6000000000000001D, "Series 1");
    timetablexydataset.add(day, 3.2000000000000002D, "Series 2");
    timetablexydataset.add(day1, 1.1000000000000001D, "Series 2");
    timetablexydataset.add(day2, 1.3999999999999999D, "Series 2");
    timetablexydataset.add(day3, 2.8999999999999999D, "Series 2");
    timetablexydataset.add(day4, 0.59999999999999998D, "Series 2");
    return timetablexydataset;
}

4 个答案:

答案 0 :(得分:1)

也许试试这个:

 renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
 renderer.setBaseItemLabelsVisible(true);

答案 1 :(得分:1)

请尝试: -

StackedBarRenderer barrenderer = (StackedBarRenderer)categoryplot.getRenderer();
barrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
barrenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));

对于Category Axis,您可以通过以下方式实现相同目标: -

CategoryAxis axis = new CategoryAxis();
axis.setLabel("Date");
axis.setLabelFont(new Font("Courier", Font.BOLD, 16));
axis.setTickLabelFont(new Font("Courier", Font.BOLD, 11));
categoryplot.setDomainAxis(axis);

答案 2 :(得分:0)

也许试试这个;)...我知道它有点衬衫,但我不认为jfreechart中有一个自动的整体机制。

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeTableXYDataset;
import org.jfree.data.xy.TableXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;


public class GroupLabels extends ApplicationFrame {
    public GroupLabels(final String title) {
        super(title);

        final JFreeChart chart = createChart(createDataset());
        final ChartPanel panel = new ChartPanel(chart);
        setContentPane(panel);
    }

    public static class MyXYItemLabelGenerator implements XYItemLabelGenerator {
        Map<Number,List<Double>> totals = new HashMap<Number,List<Double>>();
        NumberFormat df = DecimalFormat.getNumberInstance();
        protected void init(XYDataset dataset) {
            if (!totals.isEmpty()) return;

            int seriesCnt = dataset.getSeriesCount();
            for (int series=0; series<seriesCnt; series++) {
                for (int item=0; item<dataset.getItemCount(series); item++) {
                    Number key = dataset.getX(series, item);
                    List<Double> vals = totals.get(key);
                    if (!totals.containsKey(key)) {
                        vals = new ArrayList<Double>(seriesCnt);
                        for (int i=0;i<seriesCnt;i++) vals.add(0d);
                        totals.put(key, vals);
                    }
                    double val = dataset.getYValue(series, item);
                    for (int i=series-1; i>=0; i--) {
                        val += Double.isNaN(vals.get(i)) ? 0d : vals.get(i);
                    }
                    vals.set(series, val);
                }
            }
        }

        public String generateLabel(XYDataset dataset, int series, int item) {
            init(dataset);
            Number key = dataset.getX(series, item);
            return df.format(totals.get(key).get(series));
        }
    }

    private JFreeChart createChart(TableXYDataset tablexydataset) {
        DateAxis dateaxis = new DateAxis("Date");
        dateaxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
        NumberAxis numberaxis = new NumberAxis("Y");
        StackedXYBarRenderer stackedxybarrenderer = new StackedXYBarRenderer(0.10000000000000001D);


        stackedxybarrenderer.setBaseItemLabelGenerator(new MyXYItemLabelGenerator());
        stackedxybarrenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
        stackedxybarrenderer.setBaseItemLabelsVisible(true);
        stackedxybarrenderer.setDrawBarOutline(false);
        XYPlot xyplot = new XYPlot(tablexydataset, dateaxis, numberaxis, stackedxybarrenderer);
        JFreeChart jfreechart = new JFreeChart("Stacked XY Bar Chart demo 2", xyplot);
        return jfreechart;
    }

    private TableXYDataset createDataset() {
        TimeTableXYDataset timetablexydataset = new TimeTableXYDataset();
        Day day = new Day(1, 3, 2005);
        Day day1 = new Day(2, 3, 2005);
        Day day2 = new Day(3, 3, 2005);
        Day day3 = new Day(4, 3, 2005);
        Day day4 = new Day(5, 3, 2005);
        timetablexydataset.add(day, 1.0D, "Series 1");
        timetablexydataset.add(day1, 1.7D, "Series 1");
//        timetablexydataset.add(day2, 2.2999999999999998D, "Series 1");
        timetablexydataset.add(day3, 3.7000000000000002D, "Series 1");
        timetablexydataset.add(day4, 2.6000000000000001D, "Series 1");
        timetablexydataset.add(day, 3.2000000000000002D, "Series 2");
        timetablexydataset.add(day1, 1.1000000000000001D, "Series 2");
        timetablexydataset.add(day2, 1.3999999999999999D, "Series 2");
        timetablexydataset.add(day3, 2.8999999999999999D, "Series 2");
        timetablexydataset.add(day4, 0.59999999999999998D, "Series 2");
        return timetablexydataset;
    }

    public static void main(String[] args) {
        GroupLabels demo = new GroupLabels("11626934 example");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}

答案 3 :(得分:0)

为什么不完全使用注释:

    // First calculate the max y vale (used for positioning the label)
    double maxy = 0;
    for (int col=0; col<dataset.getColumnCount(); col++) {
        double colTotal = 0;
        for (int row=0; row<dataset.getRowCount(); row++) {
            if (dataset.getValue(row, col) != null) {
                colTotal += dataset.getValue(row, col).doubleValue();
            }
        }
        maxy = Math.max(maxy,  colTotal);
    }
    // Calculate the offset - 2% of the max y value worked for me
    double offset = maxy *0.02;

    // calculate the column total for each column
    for (int col=0; col<dataset.getColumnCount(); col++) {
        double colTotal = 0;
        for (int row=0; row<dataset.getRowCount(); row++) {
            if (dataset.getValue(row, col) != null) {
                colTotal += dataset.getValue(row, col).doubleValue();
            }
        }
        //  display as decimal integer
        NumberFormat nf = DecimalFormat.getIntegerInstance();
        // Create the annotation
        CategoryTextAnnotation cta = new CategoryTextAnnotation(nf.format(Math.round(colTotal)),dataset.getColumnKey(col), colTotal+offset);
        cta.setFont(fLabel);
        // Add to the plot
        plot.addAnnotation(cta);
    }