2012-07-24 93 views
0

是否有可能在標籤中顯示酒吧總額。
我加入其中,它增加了一個標籤,每個系列jfreechart - 將標籤總數添加到堆疊酒吧

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

回答

1

也許試試這個:

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

沒有。沒有做一件事。謝謝。 – Jeb 2012-07-24 08:51:33

+0

你可能會顯示代碼並解釋什麼是不正確的? – Dirk 2012-07-24 08:54:33

+0

是的。剛剛添加。 – Jeb 2012-07-24 08:57:48

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); 
+1

我使用StackedXYBarRenederer TableXYDataset - 我做它來操縱範圍通過設置上部loewer範圍很長。因此我沒有一個CategoryPlot。如上所述,我使用xyPlot。所有這一切都說stackxybarrenderer.setBaseItemLabelGenerator(新的StandardCategoryItemLabelGenerator());不工作。謝謝。 – Jeb 2012-07-24 09:02:22

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