4 # introduce a new symbol style where size and color are given by some data
5 class changesymbol(graph
.style
.symbol
):
7 def __init__(self
, sizecolumnname
="size", colorcolumnname
="color",
8 gradient
=color
.gradient
.Rainbow
,
9 symbol
=graph
.style
.symbol
.circle
,
10 symbolattrs
=[deco
.filled
, deco
.stroked([color
.gray
.black
])],
12 # add some configuration parameters and modify some other
13 self
.sizecolumnname
= sizecolumnname
14 self
.colorcolumnname
= colorcolumnname
15 self
.gradient
= gradient
16 graph
.style
.symbol
.__init
__(self
, symbol
=symbol
, symbolattrs
=symbolattrs
, **kwargs
)
18 def columnnames(self
, privatedata
, sharedata
, agraph
, columnnames
, dataaxisnames
):
19 # register the new column names
20 if self
.sizecolumnname
not in columnnames
:
21 raise ValueError("column '%s' missing" % self
.sizecolumnname
)
22 if self
.colorcolumnname
not in columnnames
:
23 raise ValueError("column '%s' missing" % self
.colorcolumnname
)
24 return ([self
.sizecolumnname
, self
.colorcolumnname
] +
25 graph
.style
.symbol
.columnnames(self
, privatedata
, sharedata
, agraph
,
26 columnnames
, dataaxisnames
))
28 def drawpoint(self
, privatedata
, sharedata
, graph
, point
):
29 # replace the original drawpoint method by a slightly revised one
30 if sharedata
.vposvalid
and privatedata
.symbolattrs
is not None:
31 x_pt
, y_pt
= graph
.vpos_pt(*sharedata
.vpos
)
32 color
= self
.gradient
.getcolor(point
[self
.colorcolumnname
])
33 privatedata
.symbol(privatedata
.symbolcanvas
, x_pt
, y_pt
,
34 privatedata
.size_pt
*point
[self
.sizecolumnname
],
35 privatedata
.symbolattrs
+ [color
])
37 g
= graph
.graphxy(width
=10)
38 g
.plot(graph
.data
.points([[random
.random() for i
in range(4)]
39 for i
in range(1000)],
40 x
=1, y
=2, size
=3, color
=4),
42 g
.writeEPSfile("changesymbol")
43 g
.writePDFfile("changesymbol")
44 g
.writeSVGfile("changesymbol")