This is my code :
Code: Select all
public class MetricCharts extends ApplicationFrame {
public MetricCharts(final String title) throws Exception {
super(title);
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Returns a sample dataset.
*
* @return The dataset.
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static CategoryDataset createDataset() throws Exception {
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File("C:/Users/Napitupulu/AppData/Local/Temp/pti_php_depend/summary.xml"));
NodeList nodeList = document.getElementsByTagName("metrics");
for(int x=nodeList.getLength()-1;x>=0;x--){
String m = nodeList.item(x).getAttributes().getNamedItem("ahh").getNodeName();
//System.out.println(m);
}
for(int x=nodeList.getLength()-1;x>=0;x--){
Double a = Double.parseDouble(nodeList.item(x).getAttributes().getNamedItem("ahh").getNodeValue());
Double b = Double.parseDouble(nodeList.item(x).getAttributes().getNamedItem("cloc").getNodeValue());
double c = b/a;
dataset.addValue(a,nodeList.item(x).getAttributes().getNamedItem("ahh").getNodeName(),nodeList.item(x).getAttributes().getNamedItem("ahh").getNodeName());
dataset.addValue(c,nodeList.item(x).getAttributes().getNamedItem("cloc").getNodeName(),nodeList.item(x).getAttributes().getNamedItem("cloc").getNodeName());
}
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
public static JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Bar Chart Demo", // chart title
"Metrics", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
);
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
// disable bar outlines...
final BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(true);
// set up gradient paints for series...
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.lightGray
);
//final GradientPaint gp1 = new GradientPaint(
// 0.0f, 0.0f, Color.green,
//0.0f, 0.0f, Color.lightGray
//);
//final GradientPaint gp2 = new GradientPaint(
// 0.0f, 0.0f, Color.red,
//0.0f, 0.0f, Color.lightGray
//);
renderer.setSeriesPaint(0, gp0);
//renderer.setSeriesPaint(1, gp1);
//renderer.setSeriesPaint(2, gp2);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.STANDARD
);
return chart;
}
public static void main(final String[] args) throws Exception {
final MetricCharts demo = new MetricCharts("Barchart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
MetricChartsPDF charts;
charts = new MetricChartsPDF();
charts.saveChartToPDF(createChart(dataset), 500, 400, "C://barchart.pdf");
}
}