1

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

在此处输入图像描述

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

4 回答 4

1

请试试这个: -

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

对于类别轴,您可以通过以下方式实现相同的目标:-

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);
于 2012-07-24T08:59:56.353 回答
1

也许试试这个:

 renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
 renderer.setBaseItemLabelsVisible(true);
于 2012-07-24T08:40:00.393 回答
1

为什么不简单地使用注释:

    // 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);
    }
于 2019-11-14T10:36:09.457 回答
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);
    }
}
于 2013-04-26T17:42:10.430 回答