def pieChart(fd, width, height, title, data, labels, bgColor=None, labelColor='#000000', colorScheme=DEFAULT_COLOR): dataSet = [(name, [[0, value]]) for name, value in izip(labels, data)] axisLabels = [dict(v=i, label=label) for i, label in enumerate(labels)] options = Option(title=title, titleFont='Times', titleFontSize=24, pieRadius=0.35, legend=Option(hide=True), colorScheme=colorScheme, background=Option(baseColor=bgColor), axis=Option(labelColor=labelColor, x=Option(ticks=axisLabels))) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) chart = PieChart(surface, options) chart.addDataset(dataSet) chart.render() surface.write_to_png(fd)
def testPie(): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 400, 400) chart = PieChart(surface) dataSet = ( ('myFirstDataset', [[0, 3]]), ('mySecondDataset', [[0, 1.4]]), ('myThirdDataset', [[0, 0.46]]), ('myFourthDataset', [[0, 0.3]]), ) chart.addDataset(dataSet) chart.render() surface.write_to_png("testpie.png")
def testPie(): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 400, 400) chart = PieChart(surface) dataSet = ( ('myFirstDataset', [[0, 3]]), ('mySecondDataset', [[0, 1.4]]), ('myThirdDataset', [[0, 0.46]]), ('myFourthDataset', [[0, 0.3]]), ) chart.addDataset(dataSet) chart.render() surface.write_to_png("testpie.png")
def pieChart(fd, width, height, title, data, labels, bgColor=None, labelColor='#000000', colorScheme=DEFAULT_COLOR): dataSet = [(name, [[0, value]]) for name, value in izip(labels, data)] axisLabels = [dict(v=i, label=label) for i, label in enumerate(labels)] options = Option( title=title, titleFont='Times', titleFontSize=24, pieRadius=0.35, legend=Option(hide=True), colorScheme=colorScheme, background=Option(baseColor=bgColor), axis=Option(labelColor=labelColor, x=Option(ticks=axisLabels))) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) chart = PieChart(surface, options) chart.addDataset(dataSet) chart.render() surface.write_to_png(fd)
def analyze_searches(request): import cairo from pycha.pie import PieChart results = {} sf = initialize_search_form(request) for field_name, field in sf.fields.items(): if isinstance(field, ClassChoiceField) or isinstance(field, CustomChoiceField): results[field] = {'data': {}, 'meta': {'total': 0}} srs = SearchRegistry.objects.filter(date__gte = date.today() - timedelta(days = 7)) num_queries = len(srs) folder = settings.MEDIA_ROOT + '/temp/' for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except: pass for sr in srs: key_vals = sr.query.split('&') for key_val in key_vals: vals = key_val.split('=') field_name = vals[0] field = sf.fields[field_name] if not (isinstance(field, ClassChoiceField) or isinstance(field, CustomChoiceField)): continue val = vals[1] str_val = field.get_object_name(val) if str_val not in results[field]['data']: results[field]['data'][str_val] = [0, 0] results[field]['data'][str_val][0] += 1 results[field]['meta']['total'] += 1 for field, field_dict in results.items(): try: results[field]['meta']['percentage'] = 100.0 * results[field]['meta']['total'] / num_queries except: results[field]['meta']['percentage'] = 0.0 sub_total = results[field]['meta']['total'] for field_option, sub_results in field_dict['data'].items(): sub_results[1] = 100.0 * sub_results[0] / sub_total surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 400) chart = PieChart(surface) dataSet = [(field_option, [[0, sub_results[1]]]) for field_option, sub_results in field_dict['data'].items()] if dataSet: chart.addDataset(dataSet) chart.render() filename = folder + str(field.name) + "-" + str(num_queries) + ".png" surface.write_to_png(filename) results[field]['meta']['is_available'] = True else: results[field]['meta']['is_available'] = False return append_manager_ptype_to_response(request, 'manager/analyze_searches.html', { 'form': sf, 'results': results, 'num_queries': num_queries, })
def analyze_searches(request): import cairo from pycha.pie import PieChart results = {} sf = initialize_search_form(request) for field_name, field in sf.fields.items(): if isinstance(field, ClassChoiceField) or isinstance( field, CustomChoiceField): results[field] = {'data': {}, 'meta': {'total': 0}} srs = SearchRegistry.objects.filter(date__gte=date.today() - timedelta(days=7)) num_queries = len(srs) folder = settings.MEDIA_ROOT + '/temp/' for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except: pass for sr in srs: key_vals = sr.query.split('&') for key_val in key_vals: vals = key_val.split('=') field_name = vals[0] field = sf.fields[field_name] if not (isinstance(field, ClassChoiceField) or isinstance(field, CustomChoiceField)): continue val = vals[1] str_val = field.get_object_name(val) if str_val not in results[field]['data']: results[field]['data'][str_val] = [0, 0] results[field]['data'][str_val][0] += 1 results[field]['meta']['total'] += 1 for field, field_dict in results.items(): try: results[field]['meta']['percentage'] = 100.0 * results[field][ 'meta']['total'] / num_queries except: results[field]['meta']['percentage'] = 0.0 sub_total = results[field]['meta']['total'] for field_option, sub_results in field_dict['data'].items(): sub_results[1] = 100.0 * sub_results[0] / sub_total surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 400) chart = PieChart(surface) dataSet = [(field_option, [[0, sub_results[1]]]) for field_option, sub_results in field_dict['data'].items()] if dataSet: chart.addDataset(dataSet) chart.render() filename = folder + str( field.name) + "-" + str(num_queries) + ".png" surface.write_to_png(filename) results[field]['meta']['is_available'] = True else: results[field]['meta']['is_available'] = False return append_manager_ptype_to_response(request, 'manager/analyze_searches.html', { 'form': sf, 'results': results, 'num_queries': num_queries, })