Search code examples
pythonpygal

How do I override functions in this class?


I am using the pygal library to create charts and want to override a few of the functions, such as the _tooltip_data function, among others. For starters, I am trying to override the _title function (To create the text, I realize I can do: gauge.title = "Blah Title", but I want to override some other things in the _title function).

I'm pretty lost. To create a Gauge chart, I create a Gauge class, which inherets from MyGraph class, which inherets from pypal's Graph class. If I try to create a MyGauge chart, then I get an error:

AttributeError: 'module' object has no attribute 'MyGauge'

Here's my code, complete with what I'm sure are a ton of errors. Keep in mind that I'm still a newb (I probably didn't have to tell you that, you could probably guess that just by looking at my code).

import pygal, os 
from pygal.graph.graph import Graph
from pygal.graph.gauge import Gauge
from pygal.graph import CHARTS_NAMES

class MyGraph(Graph):

    def _title(self):
        """Make the title"""
        if self.title:
            for i, title_line in enumerate(self.title, 1):
                self.svg.node(
                    self.nodes['graph'], 'text', class_='title',
                    x=self.margin.left + self.view.width / 2,
                    y=i * (self.title_font_size + 10)
                ).text = "Hey, this is my Chart Title" # my addition

class MyGauge(MyGraph):
    pass


gauge_chart = pygal.MyGauge(human_readable=True,pretty_print=True) #@UndefinedVariable
gauge_chart.title = 'DeltaBlue V8 benchmark results'
gauge_chart.x_labels = ['Richards', 'DeltaBlue', 'Crypto', 'RayTrace', 'EarleyBoyer', 'RegExp', 'Splay', 'NavierStokes']
gauge_chart.range = [0, 10000]
gauge_chart.add('Chrome', 8212)
gauge_chart.add('Firefox', 8099)
gauge_chart.add('Opera', 2933)
gauge_chart.add('IE', 41)


png = 'gauge.png'
if os.path.isfile(png):
    os.remove(png)

gauge_chart.render_to_png(png)

I'm stuck. I even tried to create a MyPygal class, which inherets from pygal, so I could override the CHARTS_NAMES

class MyPygal(pygal):
    def __init__(self):
        CHARTS_NAMES = [
            'Line',
            'StackedLine',
            'XY',
            'Bar',
            'HorizontalBar',
            'StackedBar',
            'HorizontalStackedBar',
            'Pie',
            'Radar',
            'Funnel',
            'Pyramid',
            'VerticalPyramid',
            'Dot',
            'Gauge',
            'MyGauge' # my new chart type
        ]

Which I call with "MyPygal.MyGauge" but with that I get the error:

TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

Am I even on the right track?


Solution

  • If you create your own class in your own file, don't try to refer to it with pygal.MyGauge. Using the pygal. prefix is for referring to things inside the pygal library. But your MyGauge class isn't part of that library; you created it yourself. Just instantiate your class with MyGauge(...).

    Also, why did you create the MyGauge class at all? It doesn't do anything. Why not just instantiate MyGraph directly?