示例#1
0
def graph(*data,**kwargs):
    ''' Creates a simple time series graph. Takes the output from getData (a list of Facts). 
        Alternately, you can pass the same set of parameters that are passed to getData().
        Takes an optional elements argument which expresses which elements to show. Also
        takes an optional showMissing argument which determines whether missing values (-9999.0)
        should be displayed. Also takes an optional lineWidth argument which lets you set lines
        heavier or lighter. Example: graph(data,elements=("temp","precip"),showMissing=False). 
        Returns the plot object in case you want to customize the graph in some way.
    '''
    from org.jfree.chart import ChartFactory,ChartFrame
    from org.jfree.data.time import TimeSeries,TimeSeriesCollection, Minute
    from java.awt import BasicStroke

    # Were we passed a dataset or parameters for obtaining a dataset?
    if len(data) == 3:
        station,date,element = data
        graph(getData(station,date,element))
        return
    else:
        data = data[0] # unwrap from tuple
    
    showMissing = kwargs.get('showMissing',True)
    lineWidth = kwargs.get('lineWidth',2.0)
    title = kwargs.get('title',"Time series graph")
    elementsToShow = [e.name for e in findElements(kwargs.get('elements',""))]
    datasets = {}
    for fact in sorted(data):
        
        if fact.value in missingValues and not showMissing: continue  
        station = str(fact.station.name)
        element = fact.element.name
        if elementsToShow and element not in elementsToShow: continue
        series = station+":"+element
        date = fact.datetime
        hour = date.getHour()
        if fact.subhourlyTime is not None:
            subhourlyTime = int(fact.subhourlyTime)
        else:
            subhourlyTime = 0
        if subhourlyTime == 60: # JFreeChart can't handle the 1-60 representation of minute in CRN
            subhourlyTime = 0
        minute = Minute(subhourlyTime,hour,date.getDay(),date.getMonth()+1,date.getYear()) # JFreeChart's representation of time
        if series not in datasets:
            datasets[series] = TimeSeries(series)
        datasets[series].add(minute,float(fact.value))
    
    timeSeriesCollection = TimeSeriesCollection()
    for dataset in datasets.values():
        timeSeriesCollection.addSeries(dataset)

    chart = ChartFactory.createTimeSeriesChart("", "UTC Date", "Value", timeSeriesCollection, True, True, False)
    plot = chart.getPlot()

    r = plot.getRenderer()
    r.setStroke(BasicStroke(lineWidth))
    
    frame = ChartFrame(title, chart)
    frame.pack()
    frame.setVisible(True)
    return plot
示例#2
0
 def __init__(self):
     """ generated source for method __init__ """
     super(ConfigurableDetailPanel, self).__init__(GridBagLayout())
     model = DefaultTableModel()
     model.addColumn("Step")
     model.addColumn("My Move")
     model.addColumn("Time spent")
     model.addColumn("Out of time?")
     self.moveTable = JZebraTable(model)
     self.moveTable.setShowHorizontalLines(True)
     self.moveTable.setShowVerticalLines(True)
     sidePanel = JPanel()
     self.memUsage = TimeSeries("Used Memory")
     self.memTotal = TimeSeries("Total Memory")
     self.memUsage.setMaximumItemCount(36000)
     self.memTotal.setMaximumItemCount(36000)
     memory = TimeSeriesCollection()
     memory.addSeries(self.memUsage)
     memory.addSeries(self.memTotal)
     memChart = ChartFactory.createTimeSeriesChart(None, None, "Megabytes", memory, True, True, False)
     memChart.setBackgroundPaint(getBackground())
     memChartPanel = ChartPanel(memChart)
     memChartPanel.setPreferredSize(Dimension(500, 175))
     sidePanel.add(memChartPanel)
     self.counters = HashSet()
     self.countersCollection = TimeSeriesCollection()
     counterChart = ChartFactory.createTimeSeriesChart(None, None, None, self.countersCollection, True, True, False)
     counterChart.getXYPlot().setRangeAxis(LogarithmicAxis("Count per 100ms"))
     counterChart.getXYPlot().getRangeAxis().setAutoRangeMinimumSize(1.0)
     counterChart.setBackgroundPaint(getBackground())
     counterChartPanel = ChartPanel(counterChart)
     counterChartPanel.setPreferredSize(Dimension(500, 175))
     sidePanel.add(counterChartPanel)
     self.scoreCountersCollection = TimeSeriesCollection()
     scoreCounterChart = ChartFactory.createTimeSeriesChart(None, None, "Score", self.scoreCountersCollection, True, True, False)
     scoreCounterChart.getXYPlot().getRangeAxis().setRange(0, 100)
     scoreCounterChart.setBackgroundPaint(getBackground())
     scoreCounterChartPanel = ChartPanel(scoreCounterChart)
     scoreCounterChartPanel.setPreferredSize(Dimension(500, 175))
     sidePanel.add(scoreCounterChartPanel)
     self.add(JScrollPane(self.moveTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), GridBagConstraints(0, 0, 1, 2, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, Insets(0, 0, 0, 0), 0, 0))
     self.add(sidePanel, GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, Insets(0, 0, 0, 0), 0, 0))
     self.add(JButton(resetButtonMethod()), GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, Insets(0, 0, 0, 0), 0, 0))