2014-09-03 78 views
1

我需要根據XML文件中「狀態」字段的值生成條形圖(如下所示)。 Y軸上的條形是XML文件中的「鍵」字段。 (計劃A,計劃B等)。使用xml文件的簡單jfreechart條形圖

這些條的顏色取決於相應標籤中的「狀態」字段。 (成功 - 綠色,失敗 - 紅色,InProgress - 藍色)

我試着用Google搜索如何通過讀取XML文件來生成條形圖。但我無法弄清楚。

請幫我解決從xml文件中解析「狀態」字段&這個用例的正確jfreechart。

XML文件內容:

<results> 
<result id="1" number="10" lifeCycleState="Finished" state="Failed" key="PLAN-A"> 
</result> 

<result id="2" number="20" lifeCycleState="Finished" state="Success" key="PLAN-B"> 
</result> 

<result id="3" number="30" lifeCycleState="Finished" state="Success" key="PLAN-C"> 
</result> 

<result id="4" number="40" lifeCycleState="InProgress" state="InProgress" key="PLAN-D"> 
</result> 
</results> 

圖是:

enter image description here

回答

2

這裏是你在找什麼一個例子:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GradientPaint; 
import java.io.File; 
import java.io.IOException; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.plot.CategoryPlot; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.renderer.category.BarRenderer; 
import org.jfree.data.category.CategoryDataset; 
import org.jfree.data.category.DefaultCategoryDataset; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

public class BarChartDemo extends ApplicationFrame { 


    public BarChartDemo(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 
    */ 
    private CategoryDataset createDataset() throws Exception { 
     final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document document = db.parse(new File("src/input.xml")); 
     NodeList nodeList = document.getElementsByTagName("result"); 


     for(int x=nodeList.getLength()-1;x>=0;x--){ 
      dataset.addValue(10.0, nodeList.item(x).getAttributes().getNamedItem("state").getNodeValue(),nodeList.item(x).getAttributes().getNamedItem("key").getNodeValue()); 
     } 

     return dataset; 

    } 

    /** 
    * Creates a sample chart. 
    * 
    * @param dataset the dataset. 
    * 
    * @return The chart. 
    */ 
    private JFreeChart createChart(final CategoryDataset dataset) { 

     // create the chart... 
     final JFreeChart chart = ChartFactory.createBarChart(
      "Bar Chart Demo",   // chart title 
      "key",    // domain axis label 
      "number",     // range axis label 
      dataset,     // data 
      PlotOrientation.HORIZONTAL, // orientation 
      true,      // include legend 
      true,      // tooltips? 
      false      // URLs? 
     ); 

     // 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.createIntegerTickUnits()); 

     // disable bar outlines... 
     final BarRenderer renderer = (BarRenderer) plot.getRenderer(); 
     renderer.setDrawBarOutline(false); 

     // 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.createUpRotationLabelPositions(Math.PI/6.0) 
    ); 

     return chart; 

    } 


    public static void main(final String[] args) throws Exception { 

     final BarChartDemo demo = new BarChartDemo("Barchart"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 

    } 

} 

編輯:修復基於狀態的顏色值,執行以下操作:

/* renderer.setSeriesPaint(0, gp0); 
    renderer.setSeriesPaint(1, gp1); 
    renderer.setSeriesPaint(2, gp2);*/ 

評論上面這段代碼在你的程序,並添加下面的代碼來代替:

int pos=0; 
    List<Comparable> statusList = dataset.getRowKeys(); 
    for(Comparable status : statusList){ 

     if(status.equals("InProgress")){ 
      renderer.setSeriesPaint(pos,gp0); 
     } 
     else if (status.equals("Success")){ 
      renderer.setSeriesPaint(pos,gp1); 
     } 
     else if (status.equals("Failed")){ 
      renderer.setSeriesPaint(pos,gp2); 
     } 
     pos++; 
    } 

EDIT2: 保存爲圖片,在createChart方法,只是當你運行它,它

 int width=640; 
     int height=480; 
     File BarChart=new File("src/chart.png"); 
     ChartUtilities.saveChartAsPNG(BarChart, chart, width, height); 
     return chart; 

:做return chart之前,請將您的代碼會給你以下幾點: enter image description here

+0

這是一個很好的幫助..非常感謝.. – 2014-09-04 07:07:13

+0

但是,如果xml文件順序發生變化,這些顏色表示會在圖形中發生變化假設如果PLAN-A的xml文件中的「狀態」是「Success」,那麼在(失敗 - 紅色,成功 - 綠色,進行中 - 藍色) – 2014-09-04 08:23:48

+1

請參閱我的編輯根據狀態具有顏色,但仍未進行編輯,如果您將PLAN-A的狀態更改爲「成功」,圖表將僅顯示「綠色」欄。 – user3487063 2014-09-04 13:45:22

1

一小點,圖形顯示10.0爲所有值。 更改: dataset.addValue(10.0,... : dataset.addValue(Double.valueOf(nodeList.item(x).getAttributes()。getNamedItem(「number」)。getNodeValue()),..