示例#1
0
def category(data, name='', xlabel='', ylabel='', stacked=False, trid=False):
    """
  Creates a category bar chart.
    
  *data* is a ``dict`` whose keys are category names and whose values are 
  numerical (the height of the bar). 

  To plot multiple series *data* is specified as a ``dict`` whose keys are 
  series names and values are a ``dict`` of category names to numerical values.

  Setting *stacked* to ``True`` results in a stacked bar char. Setting *trid*
  to ``True`` results in a 3D bar chart.
  """
    dataset = DefaultCategoryDataset()
    for k, v in data.iteritems():
        if isinstance(v, dict):
            for k2, v2 in v.iteritems():
                dataset.addValue(v2, k2, k)
        else:
            dataset.addValue(v, "", k)

    if trid:
        if stacked:
            chart = ChartFactory.createStackedBarChart3D(
                name, xlabel, ylabel, dataset, PlotOrientation.VERTICAL, True,
                True, True)
        else:
            chart = ChartFactory.createBarChart3D(name, xlabel, ylabel,
                                                  dataset,
                                                  PlotOrientation.VERTICAL,
                                                  True, True, True)
    else:
        if stacked:
            chart = ChartFactory.createStackedBarChart(
                name, xlabel, ylabel, dataset, PlotOrientation.VERTICAL, True,
                True, True)
        else:
            chart = ChartFactory.createBarChart(name, xlabel, ylabel, dataset,
                                                PlotOrientation.VERTICAL, True,
                                                True, True)
    return Chart(chart)
	def generatePlotPanel(self, plotTitle, listData):
		"""
		1) Create a CategoryDataset
		2) Create a BarChart (or  Create a BarPlot and put it in a JFreeChart)
		3) Put the BarChart in a ChartPanel
		"""
		# Get a dictionary of value occurence in the list {value1:count, value2:count...}
		dataDico = Counter(listData)
		#print dataDico # value: counts OK
		 
		# Create a Pie dataset from the dicoData
		dataset = DefaultCategoryDataset() 
		for key, value in dataDico.items(): 
			#print key, value 
			dataset.setValue(value, key, "") 
		
		# Create an instance of JFreeChart 	
		chart = ChartFactory.createBarChart(plotTitle, "Categories", "Count", dataset) 
		# Alternative way
		#piePlot = PiePlot(pieDataset)
		#chart   = JFreeChart(plotTitle, piePlot)
		
		return ChartPanel(chart)