~kklimonda/ubuntu/lucid/hamster-applet/lucid-proposed

« back to all changes in this revision

Viewing changes to hamster/charting.py

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2008-09-08 17:37:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080908173705-wvbg8ciiprf2lyoc
Tags: 2.23.92-0ubuntu1
* New upstream release (LP: #262744)
  - fixed code so that it works also with Python 2.4
  - Fixed bug with tasks falling into unsorted category (bug #548914)
  - Fixed error when switching tasks with doubleclick
  - Bump up pygtk and libgtk to 2.12, pyobject to 2.14
    intltool to 0.37.1
  - Downgrade XS-python and python b-d and depends to 2.4
* debian/control:
  - Change Homepage field to GNOME url
 

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
    r,g,b = color[0] / 255.0, color[1] / 255.0, color[2] / 255.0
77
77
    context.set_source_rgb(r, g, b)
78
78
    
 
79
def set_color_gdk(context, color):
 
80
    r,g,b = color.red / 65536.0, color.green / 65536.0, color.blue / 65536.0
 
81
    context.set_source_rgb(r, g, b)
79
82
    
80
83
class Chart(gtk.DrawingArea):
81
84
    """Chart constructor. Optional arguments:
300
303
        
301
304
        res = []
302
305
        for i in range(len(data)):
303
 
            factor = data[i][1] / float(max_value) if max_value > 0 else 0
304
 
            
 
306
            factor = 0
 
307
            if max_value > 0:
 
308
                factor = data[i][1] / float(max_value)
 
309
            
 
310
            color = None
 
311
            if len(data[i]) > 2:
 
312
                color = data[i][2]
 
313
            
 
314
            background = None
 
315
            if len(data[i]) > 3:
 
316
                background = data[i][3]
 
317
                
305
318
            res.append({"label": data[i][0],
306
319
                        "value": data[i][1],
307
 
                        "color": data[i][2] if len(data[i]) > 2 else None,
308
 
                        "background": data[i][3] if len(data[i]) > 3 else None,
 
320
                        "color":  color,
 
321
                        "background":  background,
309
322
                        "factor": factor
310
323
                        })
311
324
        
353
366
                graph_width = step * records #no need to have that white stuff
354
367
 
355
368
        graph_y = rect.y
356
 
        graph_height = graph_y - rect.x + rect.height - 15
 
369
        graph_height = rect.height - 15
357
370
        
358
371
        max_size = graph_height - 15
359
372
 
381
394
        set_color(context, dark[8])
382
395
        
383
396
        # scale lines
384
 
        stride = self.default_grid_stride if self.stretch_grid == False else int(graph_height / 4)
 
397
        stride = int(graph_height / 4)
 
398
        if self.stretch_grid == False:
 
399
            stride = self.default_grid_stride
385
400
            
386
401
        for y in range(graph_y, graph_y + graph_height, stride):
387
402
            context.move_to(graph_x - 10, y)
400
415
        context.set_dash ([]);
401
416
 
402
417
 
403
 
        # labels
404
 
        set_color(context, dark[8]);
 
418
        # scale labels
 
419
        set_color_gdk(context, self.style.fg[gtk.STATE_NORMAL]);
405
420
        for i in range(records):
406
421
            extent = context.text_extents(data[i]["label"]) #x, y, width, height
407
422
            context.move_to(graph_x + (step * i) + (step - extent[2]) / 2.0,
409
424
            context.show_text(data[i]["label"])
410
425
 
411
426
        # values for max min and average
412
 
        max_label = "%.1f" % self.max if self.there_are_floats else "%d" % self.max
 
427
        max_label = "%d" % self.max
 
428
        if self.there_are_floats:
 
429
            max_label = "%.1f" % self.max
 
430
 
413
431
        extent = context.text_extents(max_label) #x, y, width, height
414
432
 
415
433
        context.move_to(graph_x - extent[2] - 16, rect.y + 10)
425
443
 
426
444
        # bars themselves
427
445
        for i in range(records):
428
 
            color = data[i]["color"] if  data[i]["color"] != None else 3
 
446
            color = data[i]["color"] or 3
 
447
 
429
448
            bar_size = graph_height * data[i]["factor"]
430
449
            #on animations we keep labels on top, so we need some extra space there
431
 
            bar_size = bar_size * 0.8 if self.values_on_bars and self.animate else bar_size * 0.9
 
450
            if self.values_on_bars and self.animate:
 
451
                bar_size = bar_size * 0.8
 
452
            else:
 
453
                bar_size = bar_size * 0.9
 
454
                
432
455
            bar_size = max(bar_size, 1)
433
456
            
434
457
            gap = step * 0.05
447
470
 
448
471
        if self.values_on_bars:
449
472
            for i in range(records):
450
 
                label = "%.1f" % data[i]["value"] if self.there_are_floats else "%d" % data[i]["value"]
 
473
                if self.there_are_floats:
 
474
                    label = "%.1f" % data[i]["value"]
 
475
                else:
 
476
                    label = "%d" % data[i]["value"]
451
477
                extent = context.text_extents(label) #x, y, width, height
452
478
                
453
479
                bar_size = graph_height * data[i]["factor"]
454
480
                
455
 
                bar_size = bar_size * 0.8 if self.animate else bar_size * 0.9
 
481
                if self.animate:
 
482
                    bar_size = bar_size * 0.8
 
483
                else:
 
484
                    bar_size = bar_size * 0.9
456
485
                    
457
486
                vertical_offset = (step - extent[2]) / 2.0
458
487
                
503
532
        graph_width = rect.width + rect.x - graph_x
504
533
 
505
534
        graph_y = rect.y
506
 
        graph_height = graph_y - rect.x + rect.height
507
 
        
508
 
        
509
 
        step = int(graph_height / float(records)) if records > 0 else 30
 
535
        graph_height = rect.height
 
536
        
 
537
        
 
538
        if records > 0:
 
539
            step = int(graph_height / float(records))
 
540
        else:
 
541
            step = 30
 
542
            
510
543
        if self.max_bar_width:
511
544
            step = min(step, self.max_bar_width)
512
545
            if self.collapse_whitespace:
517
550
 
518
551
        ellipsize_label = lambda(text): 3
519
552
 
520
 
        #now let's put the labels and align them right
521
 
        set_color(context, dark[8]);
 
553
        # now let's put scale labels and align them right
 
554
        set_color_gdk(context, self.style.fg[gtk.STATE_NORMAL]);
522
555
        for i in range(records):
523
556
            label = data[i]["label"]
524
557
            if self.legend_width:
544
577
        set_color(context, dark[8])
545
578
 
546
579
        # scale lines        
547
 
        grid_stride = self.default_grid_stride if self.stretch_grid == False else (graph_width) / 3.0
 
580
        if self.stretch_grid == False:
 
581
            grid_stride = self.default_grid_stride
 
582
        else:
 
583
            grid_stride = int(graph_width / 3.0)
 
584
            
548
585
        for x in range(graph_x + grid_stride, graph_x + graph_width - grid_stride, grid_stride):
549
586
            context.move_to(x, graph_y)
550
587
            context.line_to(x, graph_y + graph_height)
569
606
 
570
607
        # bars themselves
571
608
        for i in range(records):
572
 
            color = data[i]["color"] if  data[i]["color"] != None else 3
 
609
            color = data[i]["color"] or 3
573
610
            bar_y = graph_y + (step * i) + gap
574
611
            bar_size = max_size * data[i]["factor"]
575
612
            bar_size = max(bar_size, 1)
583
620
        set_color(context, dark[8])        
584
621
        if self.values_on_bars:
585
622
            for i in range(records):
586
 
                label = "%.1f" % data[i]["value"] if self.there_are_floats else "%d" % data[i]["value"]
 
623
                if self.there_are_floats:
 
624
                    label = "%.1f" % data[i]["value"]
 
625
                else:
 
626
                    label = "%d" % data[i]["value"]
587
627
                extent = context.text_extents(label) #x, y, width, height
588
628
                
589
629
                bar_size = max_size * data[i]["factor"]
600
640
        else:
601
641
            # values for max min and average
602
642
            context.move_to(graph_x + graph_width + 10, graph_y + 10)
603
 
            max_label = "%.1f" % self.max if self.there_are_floats else "%d" % self.max
 
643
            if self.there_are_floats:
 
644
                max_label = "%.1f" % self.max
 
645
            else:
 
646
                max_label = "%d" % self.max
 
647
                
604
648
            context.show_text(max_label)
605
649
        
606
650
        
617
661
        
618
662
        step = graph_width / float(records)
619
663
        graph_y = rect.y
620
 
        graph_height = graph_y - rect.x + rect.height - 15
 
664
        graph_height = rect.height - 15
621
665
        
622
666
        max_size = graph_height - 15
623
667
 
648
692
        set_color(context, dark[8])
649
693
        
650
694
        # scale lines
651
 
        stride = self.default_grid_stride if self.stretch_grid == False else int(graph_height / 4)
 
695
        if self.stretch_grid == False:
 
696
            stride = self.default_grid_stride
 
697
        else:
 
698
            stride = int(graph_height / 4)
652
699
            
653
700
        for y in range(graph_y, graph_y + graph_height, stride):
654
701
            context.move_to(graph_x - 10, y)
666
713
        
667
714
        context.set_dash ([]);
668
715
 
669
 
        # labels
670
 
        set_color(context, dark[8]);
 
716
        # scale labels
 
717
        set_color_gdk(context, self.style.fg[gtk.STATE_NORMAL]);
671
718
        for i in range(records):
672
719
            if i % 5 == 0:
673
720
                context.move_to(graph_x + 5 + (step * i), graph_y + graph_height + 13)
674
721
                context.show_text(data[i]["label"])
675
722
 
676
723
        # values for max min and average
677
 
        max_label = "%.1f" % self.max if self.there_are_floats else "%d" % self.max
 
724
        if self.there_are_floats:
 
725
            max_label = "%.1f" % self.max
 
726
        else:
 
727
            max_label = "%d" % self.max
 
728
            
678
729
        extent = context.text_extents(max_label) #x, y, width, height
679
730
 
680
731
        context.move_to(graph_x - extent[2] - 16, rect.y + 10)