1376
1376
x0 += inner_step
1378
class StreamChart(VerticalBarPlot):
1384
background = "white light_gray",
1387
series_legend = None,
1391
series_colors = None):
1393
VerticalBarPlot.__init__(self, surface, data, width, height, background, border,
1394
False, grid, False, True, False,
1395
None, x_labels, None, x_bounds, y_bounds, series_colors)
1397
def calc_steps(self):
1398
other_dir = other_direction(self.main_dir)
1399
self.series_amplitude = self.bounds[self.main_dir][1] - self.bounds[self.main_dir][0]
1400
if self.series_amplitude:
1401
self.steps[self.main_dir] = float(self.plot_dimensions[self.main_dir])/self.series_amplitude
1403
self.steps[self.main_dir] = 0.00
1404
series_length = len(self.data)
1405
self.steps[other_dir] = float(self.plot_dimensions[other_dir])/series_length
1407
def render_legend(self):
1410
def ground(self, index):
1411
sum_values = sum(self.data[index])
1412
return -0.5*sum_values
1414
def calc_angles(self):
1415
middle = self.plot_top - self.plot_dimensions[VERT]/2.0
1416
self.angles = [tuple([0.0 for x in range(len(self.data)+1)])]
1417
for x_index in range(1, len(self.data)-1):
1419
x0 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ]
1420
x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ]
1421
y0 = middle - self.ground(x_index-1)*self.steps[VERT]
1422
y2 = middle - self.ground(x_index+1)*self.steps[VERT]
1423
t.append(math.atan(float(y0-y2)/(x0-x2)))
1424
for data_index in range(len(self.data[x_index])):
1425
x0 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ]
1426
x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ]
1427
y0 = middle - self.ground(x_index-1)*self.steps[VERT] - self.data[x_index-1][data_index]*self.steps[VERT]
1428
y2 = middle - self.ground(x_index+1)*self.steps[VERT] - self.data[x_index+1][data_index]*self.steps[VERT]
1430
for i in range(0,data_index):
1431
y0 -= self.data[x_index-1][i]*self.steps[VERT]
1432
y2 -= self.data[x_index+1][i]*self.steps[VERT]
1434
if data_index == len(self.data[0])-1 and False:
1435
self.context.set_source_rgba(0.0,0.0,0.0,0.3)
1436
self.context.move_to(x0,y0)
1437
self.context.line_to(x2,y2)
1438
self.context.stroke()
1439
self.context.arc(x0,y0,2,0,2*math.pi)
1441
t.append(math.atan(float(y0-y2)/(x0-x2)))
1442
self.angles.append(tuple(t))
1443
self.angles.append(tuple([0.0 for x in range(len(self.data)+1)]))
1445
def render_plot(self):
1447
middle = self.plot_top - self.plot_dimensions[VERT]/2.0
1448
p = 0.4*self.steps[HORZ]
1449
for data_index in range(len(self.data[0])-1,-1,-1):
1450
self.context.set_source_rgba(*self.series_colors[data_index])
1452
#draw the upper line
1453
for x_index in range(len(self.data)-1) :
1454
x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ]
1455
y1 = middle - self.ground(x_index)*self.steps[VERT] - self.data[x_index][data_index]*self.steps[VERT]
1456
x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ]
1457
y2 = middle - self.ground(x_index + 1)*self.steps[VERT] - self.data[x_index + 1][data_index]*self.steps[VERT]
1459
for i in range(0,data_index):
1460
y1 -= self.data[x_index][i]*self.steps[VERT]
1461
y2 -= self.data[x_index+1][i]*self.steps[VERT]
1464
self.context.move_to(x1,y1)
1466
ang1 = self.angles[x_index][data_index+1]
1467
ang2 = self.angles[x_index+1][data_index+1] + math.pi
1468
self.context.curve_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1),
1469
x2+p*math.cos(ang2),y2+p*math.sin(ang2),
1472
for x_index in range(len(self.data)-1,0,-1) :
1473
x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ]
1474
y1 = middle - self.ground(x_index)*self.steps[VERT]
1475
x2 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ]
1476
y2 = middle - self.ground(x_index - 1)*self.steps[VERT]
1478
for i in range(0,data_index):
1479
y1 -= self.data[x_index][i]*self.steps[VERT]
1480
y2 -= self.data[x_index-1][i]*self.steps[VERT]
1482
if x_index == len(self.data)-1:
1483
self.context.line_to(x1,y1+2)
1485
#revert angles by pi degrees to take the turn back
1486
ang1 = self.angles[x_index][data_index] + math.pi
1487
ang2 = self.angles[x_index-1][data_index]
1488
self.context.curve_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1),
1489
x2+p*math.cos(ang2),y2+p*math.sin(ang2),
1492
self.context.close_path()
1496
self.context.move_to(self.borders[HORZ] + 0.5*self.steps[HORZ], middle)
1497
for x_index in range(len(self.data)-1) :
1498
x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ]
1499
y1 = middle - self.ground(x_index)*self.steps[VERT] - self.data[x_index][data_index]*self.steps[VERT]
1500
x2 = self.borders[HORZ] + (0.5 + x_index + 1)*self.steps[HORZ]
1501
y2 = middle - self.ground(x_index + 1)*self.steps[VERT] - self.data[x_index + 1][data_index]*self.steps[VERT]
1503
for i in range(0,data_index):
1504
y1 -= self.data[x_index][i]*self.steps[VERT]
1505
y2 -= self.data[x_index+1][i]*self.steps[VERT]
1507
ang1 = self.angles[x_index][data_index+1]
1508
ang2 = self.angles[x_index+1][data_index+1] + math.pi
1509
self.context.set_source_rgba(1.0,0.0,0.0)
1510
self.context.arc(x1+p*math.cos(ang1),y1+p*math.sin(ang1),2,0,2*math.pi)
1512
self.context.set_source_rgba(0.0,0.0,0.0)
1513
self.context.arc(x2+p*math.cos(ang2),y2+p*math.sin(ang2),2,0,2*math.pi)
1515
'''self.context.set_source_rgba(0.0,0.0,0.0,0.3)
1516
self.context.arc(x2,y2,2,0,2*math.pi)
1517
self.context.fill()'''
1518
self.context.move_to(x1,y1)
1519
self.context.line_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1))
1520
self.context.stroke()
1521
self.context.move_to(x2,y2)
1522
self.context.line_to(x2+p*math.cos(ang2),y2+p*math.sin(ang2))
1523
self.context.stroke()
1525
for x_index in range(len(self.data)-1,0,-1) :
1526
x1 = self.borders[HORZ] + (0.5 + x_index)*self.steps[HORZ]
1527
y1 = middle - self.ground(x_index)*self.steps[VERT]
1528
x2 = self.borders[HORZ] + (0.5 + x_index - 1)*self.steps[HORZ]
1529
y2 = middle - self.ground(x_index - 1)*self.steps[VERT]
1531
for i in range(0,data_index):
1532
y1 -= self.data[x_index][i]*self.steps[VERT]
1533
y2 -= self.data[x_index-1][i]*self.steps[VERT]
1535
#revert angles by pi degrees to take the turn back
1536
ang1 = self.angles[x_index][data_index] + math.pi
1537
ang2 = self.angles[x_index-1][data_index]
1538
self.context.set_source_rgba(0.0,1.0,0.0)
1539
self.context.arc(x1+p*math.cos(ang1),y1+p*math.sin(ang1),2,0,2*math.pi)
1541
self.context.set_source_rgba(0.0,0.0,1.0)
1542
self.context.arc(x2+p*math.cos(ang2),y2+p*math.sin(ang2),2,0,2*math.pi)
1544
'''self.context.set_source_rgba(0.0,0.0,0.0,0.3)
1545
self.context.arc(x2,y2,2,0,2*math.pi)
1546
self.context.fill()'''
1547
self.context.move_to(x1,y1)
1548
self.context.line_to(x1+p*math.cos(ang1),y1+p*math.sin(ang1))
1549
self.context.stroke()
1550
self.context.move_to(x2,y2)
1551
self.context.line_to(x2+p*math.cos(ang2),y2+p*math.sin(ang2))
1552
self.context.stroke()
1555
#self.context.arc(self.dimensions[HORZ]/2, self.dimensions[VERT]/2,50,0,3*math.pi/2)
1556
#self.context.fill()
1378
1559
class PiePlot(Plot):
1379
1560
def __init__ (self,
1380
1561
surface = None,
2034
2218
CairoPlot.bar_plot ('bar2', data, 400, 300, border = 20, grid = True, rounded_corners = False)
2037
plot = HorizontalBarPlot(name, data, width, height, background, border, series_labels,
2221
plot = HorizontalBarPlot(name, data, width, height, background, border,
2038
2222
display_values, grid, rounded_corners, stack, three_dimension,
2039
x_labels, y_labels, x_bounds, y_bounds, colors)
2223
series_labels, x_labels, y_labels, x_bounds, y_bounds, colors)
2227
def stream_chart(name,
2231
background = "white light_gray",
2234
series_legend = None,
2240
#TODO: Fix docstring for horizontal_bar_plot
2241
plot = StreamChart(name, data, width, height, background, border,
2242
grid, series_legend, x_labels, x_bounds, y_bounds, colors)