2013-07-06 11 views
9

W jaki sposób ten kompilowany przykład fragmentu kodu minimalnego, który wykorzystuje JFreeChart jako interfejs API do rysowania, jest dostosowywany w celu pokazania wartości zarówno wartości absoulte, jak i procentów? Nie mogłem wyodrębnić tych informacji z żadnego fragmentu kodu w Internecie ani z samej instrukcji JFreechart. Fragment kodu tworzy wykres kołowy pokazujący tylko procenty. Wartości bezwzględne w moim przypadku również mają znaczenie, więc muszę wyświetlać je pod wartościami procentowymi.jfreechart dostosuj piechart, aby pokazać wartości bezwzględne i wartości procentowe

Oto kod: (Uwaga brakuje importu)

public class MyMinimalPieChartExample { 
    public static void main(String[] args) { 
    DefaultPieDataset dataset = new DefaultPieDataset(); 
    dataset.setValue("some data 1",99); 
    dataset.setValue("some data 2", 77); 

    //third adaption 
    JFreeChart someChart = ChartFactory.createPieChart(
      "some chart header", dataset, 
      true, true, false); 
    PiePlot illegalLegalRestPiePlot4 = (PiePlot) someChart.getPlot(); 
    illegalLegalRestPiePlot4.setSectionPaint("some data 1", new Color(0, 255, 0)); 
    illegalLegalRestPiePlot4.setSectionPaint("some data 2", 
      new Color(255, 0, 0)); 
    PiePlot plot4 = (PiePlot) someChart.getPlot(); 
    plot4.setExplodePercent("some data 1", 0.4); 
    plot4.setSimpleLabels(true); 

    PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(
      "{0} = {2}", new DecimalFormat("0"), new DecimalFormat("0.00%")); 
    plot4.setLabelGenerator(generator); 

    try { 
     ChartUtilities.saveChartAsJPEG(new File("C:/myMinimalPieChartExample.jpeg"), 
       someChart, 1200, 1000); 
    } catch (Exception e) { 
     System.err.println("couldn't write chart"); 
    } 
    } 
} 

Odpowiedz

13

Użyj MessageFormat symbol {1} dla absolutnej wartości przekroju.

Aby uzyskać szczegółowe informacje, patrz StandardPieSectionLabelGenerator.

pie plot

public class MyMinimalPieChartExample { 

    private static final String KEY1 = "Datum 1"; 
    public static final String KEY2 = "Datum 2"; 

    public static void main(String[] args) { 
     DefaultPieDataset dataset = new DefaultPieDataset(); 
     dataset.setValue(KEY1, 99); 
     dataset.setValue(KEY2, 77); 

     JFreeChart someChart = ChartFactory.createPieChart(
      "Header", dataset, true, true, false); 
     PiePlot plot = (PiePlot) someChart.getPlot(); 
     plot.setSectionPaint(KEY1, Color.green); 
     plot.setSectionPaint(KEY2, Color.red); 
     plot.setExplodePercent(KEY1, 0.10); 
     plot.setSimpleLabels(true); 

     PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
      "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%")); 
     plot.setLabelGenerator(gen); 

     JFrame f = new JFrame("Test"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new ChartPanel(someChart) { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(400, 300); 
      } 
     }); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 

    } 
} 
+0

Zobacz także ten pokrewnych [odpowiedź] (http://stackoverflow.com/a/3551238/230513). – trashgod

Powiązane problemy