1

stcaked バーの合計金額をラベルに表示する可能性はありますか。
各シリーズにラベルを追加する例を追加しています

ここに画像の説明を入力

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

なかなかできません。シリーズのラベルをペイントします(小さな四角の内側)。助言がありますか?

私はこれを使用しています(オムラインの例から)

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

多分これを試してください:

 renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
            ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER));
 renderer.setBaseItemLabelsVisible(true);
于 2012-07-24T08:40:00.393 に答える
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

単純に注釈を使用しない理由:

    // 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 に答える