Bonjour,
Je tente péniblement à produire un graphique via Reportlab, un LinePlot() et je bloque sur l'ajout des noms des axes. J'ai réussi une petite fonction qui me produit un graphique plus ou moins maitrisé grâce à la documentation, et à trouver un exemple dans une class. Je me trouve bien embêté car mon niveau ne me permet pas de travailler cette class.
Et ne comprenant pas comment fonctionne cette ligne je n'arrive pas à l'adapter à la fonction.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
self._add(self,Label(),name='XLabel',validate=None,desc="The label on the horizontal axis")
Je pense aussi que ReportLab graphique en version à la main est pas des plus simple, voici ma base de test.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin
from reportlab.lib.validators import Auto
from reportlab.graphics.widgets.markers import makeMarker
from reportlab.pdfbase.pdfmetrics import stringWidth, EmbeddedType1Face, registerTypeFace, Font, registerFont
from reportlab.graphics.charts.axes import XValueAxis, YValueAxis, AdjYValueAxis, NormalDateXValueAxis
from reportlab.platypus import Paragraph, Spacer, SimpleDocTemplate, Table
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.graphics.shapes import Drawing
 
contenue    = []
 
def graph1():
    "Sample drawing, xvalue/yvalue axes, y connected at left of x."
    drawing = Drawing(400, 200)
    chart = LinePlot()
    chart2 = LinePlot()
    # line styles
    chart.lines[0].strokeColor = colors.green
    chart.lines[0].symbol = makeMarker(None)
    chart.lines[0].symbol.size        = 2
    chart.lines[0].symbol.angle       = 15
    chart.data = [[(1, 0), (2, 5.29), (3,5.2),(4,2),(5,4.6),(8,3)]]
    #chart.xValueAxis.setPosition(50, 50, 300)
    chart.xValueAxis.visibleGrid           = 1
 
    chart.yValueAxis = YValueAxis()
    chart.yValueAxis.setPosition(50, 50, 125)
    chart.yValueAxis.joinAxisMode = 'left'
    #chart.yValueAxis.configure(chart.data)
    drawing.add(chart)
 
    chart2.data = [[(1, 0.391), (2, 1.29), (3, 3.298),(4,2.335),(5,0),(8,0)]]
    #chart2.data = [[(0,0)]]
    #chart2.yValueAxis.setPosition(50, 50, 125)
    chart2.xValueAxis.visible           = 0
    chart2.yValueAxis.joinAxisMode = 'right'
    # y2 axis
    chart2.yValueAxis.visibleGrid           = 1
    chart2.yValueAxis.gridStrokeWidth       = 0.15
    chart2.yValueAxis.gridStrokeColor       = colors.darkgrey
    chart2.yValueAxis.visibleAxis           = 1
#    chart.yValueAxis.labels.fontSize       = fontSize -1
    chart2.yValueAxis.labels.textAnchor = 'start'
    chart2.yValueAxis.labels.boxAnchor = 'w'
    chart2.yValueAxis.labels.angle = 0
    chart2.yValueAxis.labels.dx = 5
    chart2.yValueAxis.labels.dy = 0
 
    chart2.yValueAxis.strokeWidth           = 0.45
    chart2.yValueAxis.visible               = 1
    chart2.yValueAxis.labels.rightPadding   = 2
    chart2.yValueAxis.rangeRound            ='both'
    chart2.yValueAxis.tickLeft             = -2.5
    chart2.yValueAxis.minimumTickSpacing    = 8
    chart2.yValueAxis.maximumTicks          = 10
    chart2.yValueAxis.visibleSubTicks       = 1
    chart2.yValueAxis.subTickHi             = 0
    chart2.yValueAxis.subTickLo             = 1
    chart2.yValueAxis.subTickNum            = 1
    chart2.yValueAxis.forceZero             = 0
    chart2.yValueAxis.avoidBoundFrac        = 0.1
    drawing.add(chart2)
    return drawing 
 
class Graph():
 
    def __init__(self,width=200,height=150,*args,**kw):
        Drawing.__init__(self,width,height,*args,**kw)
        self._add(self,LinePlot(),name='chart',validate=None,desc="The main chart")
        self.chart.width      = 115
        self.chart.height     = 80
        self.chart.x          = 30
        self.chart.y          = 40
        self.chart.lines[0].strokeColor = color01
        self.chart.lines[1].strokeColor = color02
        self.chart.lines[2].strokeColor = color03
        self.chart.lines[3].strokeColor = color04
        self.chart.lines[4].strokeColor = color05
        self.chart.lines[5].strokeColor = color06 
        self.chart.lines[6].strokeColor = color07
        self.chart.lines[7].strokeColor = color08 
        self.chart.lines[8].strokeColor = color09
        self.chart.lines[9].strokeColor = color10
        self.chart.lines[0].symbol = makeMarker('FilledSquare')
        self.chart.lines[1].symbol = makeMarker('FilledDiamond') 
        self.chart.lines[2].symbol = makeMarker('FilledStarFive')
        self.chart.lines[3].symbol = makeMarker('FilledTriangle')
        self.chart.lines[4].symbol = makeMarker('FilledCircle') 
        self.chart.lines[5].symbol = makeMarker('FilledPentagon')
        self.chart.lines[6].symbol = makeMarker('FilledStarSix') 
        self.chart.lines[7].symbol = makeMarker('FilledHeptagon')
        self.chart.lines[8].symbol = makeMarker('FilledOctagon')
        self.chart.lines[9].symbol = makeMarker('FilledCross')
        self.chart.fillColor       = backgroundGrey 
        self.chart.lineLabels.fontName  = 'Helvetica'
        self.chart.xValueAxis.labels.fontName       = 'Helvetica' 
        self.chart.xValueAxis.labels.fontSize       = 7
        self.chart.xValueAxis.forceZero             = 0
        self.chart.data             = [((0, 50), (100,100), (200,200), (250,210), (300,300), (400,500)), ((0, 150), (100,200), (200,300), (250,200), (300,400), (400, 600))]
        self.chart.xValueAxis.avoidBoundFrac           = 1
        self.chart.xValueAxis.gridEnd                  = 115
        self.chart.xValueAxis.tickDown                 = 3
        self.chart.xValueAxis.visibleGrid              = 1 
        self.chart.yValueAxis.tickLeft              = 3 
        self.chart.yValueAxis.labels.fontName       = 'Helvetica'
        self.chart.yValueAxis.labels.fontSize       = 7
        self._add(self,Label(),name='Title',validate=None,desc="The title at the top of the chart")
        self.Title.fontName   = 'Helvetica-Bold'
        self.Title.fontSize   = 7
        self.Title.x          = 100
        self.Title.y          = 135
        self.Title._text      = 'Chart Title'
        self.Title.maxWidth   = 180
        self.Title.height     = 20
        self.Title.textAnchor ='middle'
        self._add(self,Legend(),name='Legend',validate=None,desc="The legend or key for the chart")
        self.Legend.colorNamePairs = [(color01, 'Widgets'), (color02, 'Sprockets')]
        self.Legend.fontName       = 'Helvetica'
        self.Legend.fontSize       = 7
        self.Legend.x              = 153 
        self.Legend.y              = 85 
        self.Legend.dxTextSpace    = 5
        self.Legend.dy             = 5
        self.Legend.dx             = 5
        self.Legend.deltay         = 5 
        self.Legend.alignment      ='right'
        self._add(self,Label(),name='XLabel',validate=None,desc="The label on the horizontal axis")
        self.XLabel.fontName       = 'Helvetica'
        self.XLabel.fontSize       = 7
        self.XLabel.x              = 85
        self.XLabel.y              = 10
        self.XLabel.textAnchor     ='middle'
        self.XLabel.maxWidth       = 100 
        self.XLabel.height         = 20
        self.XLabel._text          = "X Axis"
        self._add(self,Label(),name='YLabel',validate=None,desc="The label on the vertical axis")
        self.YLabel.fontName       = 'Helvetica'
        self.YLabel.fontSize       = 7
        self.YLabel.x              = 12
        self.YLabel.y              = 80
        self.YLabel.angle          = 90
        self.YLabel.textAnchor     ='middle' 
        self.YLabel.maxWidth       = 100 
        self.YLabel.height         = 20
        self.YLabel._text          = "Y Axis" 
        self.chart.yValueAxis.forceZero           = 1
        self.chart.xValueAxis.forceZero           = 1
        self._add(self,0,name='preview',validate=None,desc=None)
 
 
contenue.append(Graph())
contenue.append(graph1())
doc = SimpleDocTemplate('test1.pdf',
                        pagesize = A4,
                        title = 'Premier test',
                        author = 'Moi',
                        topMargin=4,
                        showBoundary = 0)
doc.build(contenue)
Merci