2012-02-27 40 views

回答

1
from javax.swing import JButton, JFrame, JPanel, JLabel, JMenuBar 
from java.awt import GridBagLayout,GridBagConstraints 
from java.awt import BorderLayout as BorderLayout 
from javax.swing import WindowConstants 
from ptolemy import * 
from ptolemy.plot import Plot as Plot 
from RainfallAnalysis import RainfallAnalysis 
from jarray import array; 

class Histogram(Plot): 
    dataset = 0; 
    theJFrame = JFrame(); 
    def __init__(self): 
     self.theJFrame.setSize(400, 350);     #outer box 
     self.setSize(350, 300);      #graph window 
     self.setButtons(True);      #buttons to print, edit, etc. 
     self.setMarksStyle("none");     #do not show marks at points 
    ## 
    # Draw a histogram. 
    # It is assumed that all bins are of equal size. 
    # @param name The name to give this histogram in the key 
    # @param xMin minimum of x-range covered by histogram 
    # @param xMax maximum of x-range covered by histogram 
    # @param y array of bin heights; length of array is used to give number of  points 

    def drawHistogram(self,name, xMin, xMax,y): 
     binWidth = (xMax - xMin)/y.__len__(); 
     self.setBars(binWidth,0.0); 
     self.setConnected(False);    # do not join bars with a line 
     first = True; 
     self.setYLabel("Rain Measurement"); 
     self.setXLabel("days"); 
     for i in range(y.__len__()):    #loop to add bars to plot 
      x = i 
      self.addPoint(self.dataset, x, y[i], not first); 
      first = False; 
     self.addLegend(self.dataset, name); 
     self.dataset = self.dataset+1; 

    def showIt(self): 
     gridbag = GridBagLayout(); 
     c = GridBagConstraints(); 
     self.theJFrame.getContentPane().setLayout(gridbag); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.gridwidth = 1; 
     gridbag.setConstraints(self, c); 
     self.theJFrame.getContentPane().add(self); 
     self.theJFrame.setVisible(True); 
     self.theJFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 

if __name__ == '__main__': 
    h = Histogram(); 
    rf = RainfallAnalysis(); 
    min = rf.getMin(); 
    max = rf.getMax(); 
    data = rf.getData(); 
    h.drawHistogram("rainfall",min,max,data); 
    h.showIt(); 

和数据仅仅是双数的数组。