jfreechart -将total的标签添加到堆叠栏。

时间:2022-09-15 08:56:53

Is there a possibility to show the stcaked bar total amount in a label .
I am adding an example in which it adds a label to each serie

是否有可能在标签中显示stcaked的总金额。我添加了一个例子,在其中它为每个意甲添加了一个标签。

jfreechart -将total的标签添加到堆叠栏。

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

doesnt quite do it . it paints the label in the serie(inside the small squares). any suggestions?

不完全是这样。它在小方格内画出了标签。有什么建议吗?

I am using this (from omnline example )

我正在使用这个(来自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;
}

and

 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;
}

3 个解决方案

#1


1  

Please try with this:-

请试试这个:-

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

For Category Axis you can achieve the same by:-

对于类别轴,您可以实现相同的:-。

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


1  

Maybe try this:

也许试试这个:

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

#3


0  

Maybe try this ;) ... I know it's a bit shirt-sleeved, but I don't think there's an automatic total mechanism in jfreechart.

也许试试这个;我知道这有点不公平,但我不认为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);
    }
}

#1


1  

Please try with this:-

请试试这个:-

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

For Category Axis you can achieve the same by:-

对于类别轴,您可以实现相同的:-。

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


1  

Maybe try this:

也许试试这个:

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

#3


0  

Maybe try this ;) ... I know it's a bit shirt-sleeved, but I don't think there's an automatic total mechanism in jfreechart.

也许试试这个;我知道这有点不公平,但我不认为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);
    }
}