2011-11-10 4 views
3

Reportlab을 사용하여 LinePlot 차트를 생성하고 있습니다. X 축에 대해 숫자가 아닌 라벨을 가져올 수 없습니다. enter image description here보고서 레이블에 날짜 레이블 추가 LinePlot 차트

누구에게 아이디어가 있습니까?

이 내 Lineplot 차트 클래스 (주이다. 메신저이 클래스 밖에서 계산 및 설정을 donig,하지만 난 당신이 테스트 할 수있는 간단한 예제를 생산

import reportlab 
from advisor.charting.Font import Font 

from reportlab.lib.colors import Color, HexColor 
from reportlab.lib.pagesizes import cm, inch 
from reportlab.graphics.charts.legends import Legend 
from reportlab.graphics.charts.textlabels import Label 
from reportlab.graphics.charts.linecharts import HorizontalLineChart 
from reportlab.graphics.charts.lineplots import LinePlot 
from reportlab.graphics.shapes import Drawing, String, _DrawingEditorMixin 
from reportlab.graphics.widgets.markers import makeMarker 


class TacticalAugLineGraph(_DrawingEditorMixin, Drawing):  
    def __init__(self, width=100, height=110, legend=False, *args, **kw): 
     apply(Drawing.__init__, (self, width, height) + args, kw) 

     chartFont = Font('Gotham-Bold') 

     self._add(self, LinePlot(), name='chart', validate=None, desc=None) 
     self.chart._inFill = 1 
     self.chart.x    = 20 
     self.chart.y    = 15 
     self.chart.width  = 85 
     self.chart.height = 95 

     #self.chart.lineLabelFormat = '%d%%' 
     self.chart.yValueAxis.valueMin = 0 
     self.chart.yValueAxis.valueMax = 100 
     self.chart.yValueAxis.valueStep = 10 

    def apply_colors(self, colors): 

     self.chart.lines[0].strokeColor = colors[0] 
     self.chart.lines[1].strokeColor = colors[1] 
     self.chart.lines[2].strokeColor = colors[2] 
     self.chart.lines[3].strokeColor = colors[3] 

답변

5

요점을 얻을 결과는 같을 수 이 : 포맷터에서 enter image description here

#!/usr/bin/python 
from reportlab.graphics.charts.lineplots import LinePlot 
from reportlab.graphics.shapes import Drawing 
from reportlab.lib import colors 
from random import randint 
from datetime import date, timedelta 

# Generate some testdata 
data = [ 
    [(x,randint(90,100)) for x in range(0,2001,100)], 
    [(x,randint(30,80)) for x in range(0,2001,100)], 
    [(x,randint(5,20)) for x in range(0,2001,100)], 
    ] 

# Create the drawing and the lineplot 
drawing = Drawing(400, 200) 
lp = LinePlot() 
lp.x = 50 
lp.y = 50 
lp.height = 125 
lp.width = 300 
lp._inFill = 1 
lp.data = data 
for i in range(len(data)): 
    lp.lines[i].strokeColor = colors.toColor('hsl(%s,80%%,40%%)'%(i*60)) 

# Specify where the labels should be 
lp.xValueAxis.valueSteps = [5, 500, 1402, 1988] 
# Create a formatter that takes the value and format it however you like. 
def formatter(val): 
    #return val 
    #return 'x=%s'%val 
    return (date(2010,1,1) + timedelta(val)).strftime('%Y-%m-%d') 

# Use the formatter 
lp.xValueAxis.labelTextFormat = formatter 
drawing.add(lp) 

from reportlab.graphics import renderPDF 
renderPDF.drawToFile(drawing, 'example.pdf', 'lineplot with dates') 

당신이 그것에 더 나은 그립을 얻기 위해 놀 수있는이 대안 리턴 문이

.

물론 x 축의 데이터가 시작일 날짜 인 경우 포맷터가 필요하지 않습니다.이 경우 기본 위치 지정에 만족하지 않으면 레이블의 위치를 ​​지정할 수 있습니다.

위 예제는 reportlab documentation의 105 페이지에서 아이디어를 차용합니다.

행운을 빌어 요.