2

i would like to personilize my label of data in my Pie Chart, beacause in my legend i see data=value, in label i see data=value. how i do to see only name data in legend and value in label? my code for paint a chart is this:

    public JfreeChart(String applicationTitle, String chartTitle)throws SQLException {
    super(applicationTitle);
    JFreeChart barChart = null;
    dataset=createDataset(conn,barChart);


    barChart = ChartFactory.createPieChart(chartTitle,dataset, true, true, false);
    ChartPanel chartPanel = new ChartPanel(barChart);
    PiePlot plot = new PiePlot(dataset);
    plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
    plot.setNoDataMessage("No data available");
    plot.setCircular(true);
    plot.setLabelGap(0.02);

    chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
    setContentPane(chartPanel);
    try {
        ChartUtilities.saveChartAsPNG(new File("directory"), barChart, 600,450);
        writeChartToPDF(barChart, 600, 450,"C:\\Users\\axs0552\\Desktop\\grafico.pdf");
    } catch (IOException ex) {
        System.out.println(ex.getLocalizedMessage());
    }
}

public static void writeChartToPDF(JFreeChart chart, int width, int height,
        String fileName) {
    PdfWriter writer = null;

    Document document = new Document();

    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(
                fileName));
        document.open();
        PdfContentByte contentByte = writer.getDirectContent();
        PdfTemplate template = contentByte.createTemplate(width, height);
        Graphics2D graphics2d = template.createGraphics(width, height,new DefaultFontMapper());
        Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,height);

        chart.draw(graphics2d, rectangle2d);

        graphics2d.dispose();
        contentByte.addTemplate(template, 0, 0);

    } catch (Exception e) {
        e.printStackTrace();
    }
    document.close();
}

and my pie chart is this:

pieCart

skatedan
  • 103
  • 1
  • 10

1 Answers1

1

Absent your implementation of createDataset(), I'm guessing that the problem comes from the key passed to the setValue() method of your DefaultPieDataset. Instead, use the MessageFormat symbol {1} to construct a custom StandardPieSectionLabelGenerator, as shown here.

PiePlot plot = (PiePlot) chart.getPlot();
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator("{0} = {1}");
plot.setLabelGenerator(gen);

image

Then you can elide the unwanted characters from your implementation of createDataset() to keep them out of the legend requested in your invocation of ChartFactory.createPieChart().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • onother thing, it is possible click a section and view another chart( LineChart for example) to view more details? – skatedan Apr 26 '16 at 14:38
  • 1
    Yes, add a `ChartMouseListener`, for [example](http://stackoverflow.com/q/1110913/230513). – trashgod Apr 26 '16 at 15:00
  • i don't understand how to use this method, i place in creathechart(), but how i can link a click of one section of pie chart to details in one line chart? – skatedan Apr 26 '16 at 15:13
  • I'd have the `ChartMouseListener` call `setChart()` on an adjacent chart panel; ping me if you post a question on this topic. – trashgod Apr 26 '16 at 15:43