Skip to content

Commit

Permalink
fix #289
Browse files Browse the repository at this point in the history
  • Loading branch information
doutriaux1 committed Dec 18, 2017
1 parent 99491e4 commit f406203
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
20 changes: 20 additions & 0 deletions tests/test_vcs_drawLinesAndMarkersLegend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ def testDrawMarkerLegend(self):

fnm = "test_drawLinesAndMarkersLegend.png"
self.checkImage(fnm)
self.x.clear()
t = self.x.gettemplate("deftaylor")
t.legend.x1 = .5
t.legend.x2 = 1
t.legend.y1 = .2
t.legend.y2 = .65
ids = ["Sea Level Pressure (ERA-Interim)","SW Cloud Forcing (CERES-EBAF 4.0)","LW Cloud Forcing (CERES-EBAF 4.0)","Land Precipitation (GPCP 2.2)","Ocean Precipitation (GPCP 2.2)","2-m temperature","More"]
#ids = ["SLP(ERA-Interim)","A2","A3","B1","C1","C2","C3"]
id_sizes = [15., 15., 15., 15., 15., 15., 15.,]
id_colors = ["red","orange","green","cyan","blue","purple","black"]
symbols = ["diamond","square_fill","circle","triangle_right_fill","triangle_left_fill","triangle_up_fill","triangle_down_fill"]
colors = ["red","orange","green","cyan","blue","purple","black"]
sizes = [20., 20., 20., 20., 20., 20., 20.,]
n = 7
t.drawLinesAndMarkersLegend(self.x, linecolors=[[0,0,0,0]]*n, linetypes=['solid',]*n, linewidths=[0,]*n, markercolors=id_colors, markertypes=symbols, markersizes=id_sizes, strings=ids, scratched=None, stringscolors=id_colors, stacking='vertical', bg=False, render=True)

fnm = "test_drawLinesAndMarkersLegend_nolines.png"
self.checkImage(fnm)


15 changes: 12 additions & 3 deletions vcs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,10 @@ def drawLinesAndMarkersLegend(canvas, templateLegend,
dx = abs(templateLegend.x2 - templateLegend.x1)
dy = abs(templateLegend.y2 - templateLegend.y1)

nolines = True
for lwidth in linewidths:
nolines = nolines and lwidth == 0.
print("No lines", nolines)
# Loop until we can fit all elts into the box
while maxx * maxy < nlines:
maxwidth = 0
Expand All @@ -2661,11 +2665,15 @@ def drawLinesAndMarkersLegend(canvas, templateLegend,
ext = canvas.gettextextent(text)[0]
maxwidth = max(maxwidth, ext[1] - ext[0])
maxheight = max(maxheight, ext[3] - ext[2])
if len(strings[i]) > 4:
if nolines:
leg_lines = 0.
leg_spc = .015
elif len(strings[i]) > 4:
leg_lines = maxwidth / 3.
leg_spc = leg_lines / 3.
else:
leg_lines = maxwidth
leg_spc = leg_lines / 3.
leg_spc = leg_lines / 3.
maxwidth = maxwidth + leg_lines + leg_spc
maxx = int(dx / maxwidth)
maxy = int(dy / maxheight)
Expand All @@ -2684,6 +2692,7 @@ def drawLinesAndMarkersLegend(canvas, templateLegend,
else:
nV = min(maxy, len(strings)) # How many elts on horizontal direction
nH = numpy.ceil(nlines / float(nV)) # How many elts vertically
print("NV NH:",nV, nH)
spcX = (dx - maxwidth * nH) / (nH + 1)
spcY = (dy - maxheight * nV) / (nV + 1)
txs = []
Expand Down Expand Up @@ -2715,12 +2724,12 @@ def drawLinesAndMarkersLegend(canvas, templateLegend,
# so that we create less objet/renderers
ln = canvas.createline()
ln.color = [linecolors[i], ]
ln.type = linetypes[i]
if linewidths[i] > 0:
ln.width = linewidths[i]
ln.priority = templateLegend.priority
else:
ln.priority = 0
ln.type = linetypes[i]
# TODO check if previous marker was identical
# so that we create less objet/renderers
mrk = canvas.createmarker()
Expand Down

0 comments on commit f406203

Please sign in to comment.