fix histogram to stroke lines to the baseline for steps=0 when two subsequent values...
[PyX/mjg.git] / examples / graphstyles / cal.py
blobefb77714c92befcafe9426dedb7d88aff99be59c
1 # In this example we create a calendar graph style. The style uses
2 # bar axes for both graph coordinates and it is a simple and minimal
3 # example in the sense that it does not make use of other styles
4 # for positioning.
6 import calendar
7 from pyx import *
9 class daystyle(graph.style._style):
11 def columnnames(self, privatedata, sharedata, graph, columnnames):
12 # register the new column names
13 usecolumnnames = ["day", "month", "weekday", "note"]
14 for columnname in usecolumnnames:
15 if columnname not in columnnames:
16 raise ValueError("column '%s' missing" % columnname)
17 return usecolumnnames
19 def adjustaxis(self, privatedata, sharedata, graph, columnname, data):
20 # adjust axes ranges
21 if columnname == "month":
22 graph.axes["x"].adjustaxis([(x, 0) for x in data])
23 graph.axes["x"].adjustaxis([(x, 1) for x in data])
24 if columnname == "day":
25 graph.axes["y"].adjustaxis([(x, 0) for x in data])
26 graph.axes["y"].adjustaxis([(x, 1) for x in data])
28 def drawpoint(self, privatedata, sharedata, graph, point):
29 # draw a single day
30 x1_pt, y1_pt = graph.pos_pt((point["month"], 0), (point["day"], 0))
31 x2_pt, y2_pt = graph.pos_pt((point["month"], 1), (point["day"], 1))
32 p = path.rect_pt(x1_pt, y1_pt, x2_pt - x1_pt, y2_pt - y1_pt)
33 if point["weekday"] == calendar.day_abbr[-1]:
34 graph.stroke(p, [deco.filled([color.gray(0.8)])])
35 else:
36 graph.stroke(p)
37 graph.text_pt(x1_pt+3, y2_pt-3,
38 "%i %s" % (point["day"], point["weekday"]),
39 [text.valign.top])
40 if point["note"]:
41 graph.text_pt(x1_pt+3, y1_pt+3, point["note"], [text.size.tiny])
43 # create calendar data
44 year = 2005
45 notes = {1: {17: r"\PyX{} 0.2 (2003)", 20: r"\PyX{} 0.5 (2004)", 22: r"\PyX{} 0.5.1 (2004)"},
46 3: {30: r"\PyX{} 0.6 (2004)", 31: r"\PyX{} 0.3 ('03), \PyX{} 0.6.1 ('04)"},
47 4: {4: r"\PyX{} 0.3.1 (2003)", 7: r"\PyX{} 0.6.2 (2004)", 27: r"\PyX{} 0.6.3 (2004)"},
48 7: {13: r"\PyX{} 0.8 (2005)"},
49 8: {13: r"\PyX{} 0.8.1 (2005)", 22: r"\PyX{} 0.4 (2003)"},
50 9: {17: r"\PyX{} 0.4.1 (2003)"},
51 10: {7: r"\PyX{} 0.1 (2002)", 21: r"\PyX{} 0.7 (2004)"},
52 12: {15: r"\PyX{} 0.7.1 (2004)"}}
53 d = graph.data.list([(day,
54 calendar.month_name[month],
55 calendar.day_abbr[calendar.weekday(year, month, day)],
56 notes.get(month, {}).get(day))
57 for month in range(1, 13)
58 for day in range(1, calendar.monthrange(year, month)[1]+1)],
59 day=1, month=2, weekday=3, note=4)
61 # create the calendar
62 g = graph.graphxy(width=40, x2=graph.axis.bar(dist=0, linkpainter=None),
63 y=graph.axis.bar(dist=0, reverse=1, painter=None))
64 g.plot(d, [daystyle()])
66 # we could write the full calendar by
67 # g.writeEPSfile("cal", paperformat=document.paperformat.A3, rotated=1)
69 # instead we clip the result to show only a small part
70 clip = canvas.clip(g.bbox().enlarged(0.1, bottom=-17, right=-25).path())
71 gc = canvas.canvas([clip])
72 gc.insert(g)
73 # and add some dots at the clipped parts
74 gcbb = gc.bbox()
75 c = canvas.canvas()
76 c.insert(gc)
77 c.text(gcbb.right()+0.5, gcbb.center()[1], r"\dots")
78 c.text(gcbb.center()[0], gcbb.bottom()-0.5, r"\dots")
79 c.writeEPSfile("cal")
80 c.writePDFfile("cal")