def get_pie_image(width, height, x, y, datas, lables, _colors): """ ��ɱ�״ͼ @param width: ͼƬ��� @param height: ͼƬ�Ŀ�� @param x: ͼƬ��x��� @param y: ͼƬ��y��� @param datas: ���ͼƬ����� @param lables: ��״ͼ��������� """ from reportlab.graphics.charts.piecharts import Pie drawing = Drawing(width, height) pc = Pie() pc.width = 80 pc.height = 80 pc.x = x pc.y = y pc.data = datas pc.labels = lables pc.slices.strokeWidth = 0.5 pc.startAngle = 90 pc.checkLabelOverlap = True pc.sideLabels = True pc.sideLabelsOffset = 0.1 pc.direction = 'clockwise' for i in range(len(lables)): pc.slices[i].fontName = "msyh" pc.slices[i].fontSize = 3 pc.slices[i].labelRadius = 3 pc.slices[i].popout = 5 pc.slices[i].fillColor = _colors[i] drawing.add(pc) return drawing
def pie_chart(data_for_pie): d = Drawing(400, 400) pc = Pie() pc.x = 100 pc.y = 210 pc.width = 170 pc.height = 170 pc.sideLabels = True pc.sideLabelsOffset = 0.05 car_name_lst = [] car_sales_lst = [] car_price_lst = [] data_for_pie = data_for_pie[1:] data_for_pie = sorted(data_for_pie, key=itemgetter(3), reverse=True) # so i can show 10 most popular cars for i in data_for_pie: car_name = i[1] car_name_lst.append(car_name) car_sales = i[3] car_sales_lst.append(car_sales) car_price = float(i[2].strip( "$")) #by default all the prices are in '', for example '18731.76' car_price_lst.append(car_price) pc.data = car_sales_lst[:10] pc.labels = list(set( car_name_lst[:10])) # by using set i wont have similar items in list d.add(pc, '') """have to done this because in task i have to calculate revenue on Xaxis and car name on Yaxis""" revenue_calculation_1 = car_sales_lst[:10] revenue_calculation_2 = car_price_lst[:10] car_revenue_list = [] for i in range(len(car_price_lst[:10])): reven = revenue_calculation_1[i] * revenue_calculation_2[i] car_revenue_list.append( int(reven)) #using int because its many digits after "," """bar chart """ data = [tuple(car_revenue_list) ] #for some reason bar chart accepts only [[]] or [()] bc = VerticalBarChart() bc.x = 50 bc.y = 0 bc.height = 125 bc.width = 300 bc.data = data bc.strokeColor = colors.black bc.valueAxis.valueMin = 7000000 bc.valueAxis.valueMax = 23000000 bc.valueAxis.valueStep = 1000000 bc.categoryAxis.labels.boxAnchor = 'ne' bc.categoryAxis.labels.dx = 8 bc.categoryAxis.labels.dy = -2 bc.categoryAxis.labels.angle = 30 bc.categoryAxis.categoryNames = list(set(car_name_lst[:10])) d.add(bc) return d
def torta(datos): tamanio = datos["tamanio"] data = datos["data"] labels = datos["labels"] colores = datos["colores"] titulo = datos["titulo"] grafico = Pie() grafico.x = 10 grafico.y = 10 grafico.startAngle = 45 grafico.width = tamanio grafico.height = tamanio grafico.data = data grafico.labels = labels grafico.slices.fontName = "Helvetica" grafico.slices.fontSize = FONTSIZE grafico.simpleLabels = False grafico.sideLabels = 1 grafico.sideLabelsOffset = 0.075 grafico.slices.label_simple_pointer = False grafico.slices.label_pointer_elbowLength = 0.5 grafico.slices.label_pointer_piePad = 3 grafico.slices.label_pointer_edgePad = 3 grafico.slices.label_pointer_strokeColor = colors.black grafico.slices.label_pointer_strokeWidth = 0.75 grafico.slices.strokeWidth=1.5 grafico.slices.strokeColor=colors.white for i in range(len(colores)): grafico.slices[i].fillColor = colors.HexColor(colores[i]) retorno = crearDrawing(grafico) if titulo: etiqueta = Label() etiqueta.fontSize = FONTSIZE etiqueta.fontName = "Helvetica-Bold" etiqueta.setText(titulo) etiquetar(retorno, etiqueta, tamanio/2.0, 0.0) return retorno
car_name = i[1] car_name_lst.append(car_name) car_sales = i[3] car_sales_lst.append(car_sales) car_price = float(i[2].strip( "$")) #by default all the prices are in '', for example '18731.76 car_price_lst.append(car_price) d = Drawing(400, 400) pc = Pie() pc.x = 100 pc.y = 240 pc.width = 130 pc.height = 130 pc.sideLabels = True pc.sideLabelsOffset = 0.05 pc.data = car_sales_lst[:10] pc.labels = list(set(car_name_lst[:10])) d.add(pc, '') revenue_calculation_1 = car_sales_lst[:10] revenue_calculation_2 = car_price_lst[:10] car_revenue_list = [] for i in range(len(car_price_lst[:10])): reven = revenue_calculation_1[i] * revenue_calculation_2[i] car_revenue_list.append( int(reven)) #using int because its many digits after "," #print(car_revenue_list) bc = VerticalBarChart()