Child: [r25] (diff)

Download this file

Graphs.java    110 lines (88 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-09-13T00:00:00Z
* LicenseName: NOASSERTION
* FileName: Graphs.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Outputs graphs as image files. </text>
*/
package utils;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;
public class Graphs {
public static File generate(File baseFolder, String titles[], int[] values) {
DefaultPieDataset dataset = new DefaultPieDataset();
// add our data values
int i = -1;
for(String title : titles){
i++;
dataset.setValue(title, values[i]);
}
final JFreeChart chart =
// ChartFactory.createPieChart("", dataset, true, true, false);
ChartFactory.createPieChart3D("", // chart title
dataset, // data
true, // include legend
true,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
//plot.setStartAngle(290);
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
// final PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
// plot.setLegendLabelGenerator(
// new StandardPieSectionLabelGenerator("{0} {2}"));
chart.setBorderVisible(false);
chart.getPlot().setOutlineVisible(false);
chart.getLegend().setFrame(BlockBorder.NONE);
//chart.getLegend().setVisible(false);
plot.setCircular(true);
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2}", NumberFormat.getNumberInstance(), NumberFormat
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}", NumberFormat.getNumberInstance(), NumberFormat
.getPercentInstance()));
plot.setNoDataMessage("No data found.");
plot.setSectionPaint(1, java.awt.Color.green);
plot.setSectionPaint(2, java.awt.Color.CYAN);
plot.setSectionOutlinesVisible(false);
final ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
final File file = new File(baseFolder, "chart.png");
try {
ChartUtilities.saveChartAsPNG(file, chart, 190, 160, info);
} catch (IOException ex) {
Logger.getLogger(Graphs.class.getName()).log(Level.SEVERE, null, ex);
}
//
//// final ChartPanel chartPanel = new ChartPanel(chart, true);
// final ChartPanel chartPanel = new ChartPanel(chart, true);
// chartPanel.setMinimumDrawWidth(0);
// chartPanel.setMaximumDrawWidth(Integer.MAX_VALUE);
// chartPanel.setMinimumDrawHeight(0);
// chartPanel.setMaximumDrawHeight(Integer.MAX_VALUE);
// JDialog dialog = new JDialog();
// dialog.add(chartPanel);
// dialog.setLayout(new GridLayout(1, 1));
// dialog.setSize(400, 200);
// dialog.setVisible(true);
return file;
}
}