3

I'm making a line chart in Pygal. I want to center the title of the chart, but I don't know how to do that.

I tried looking through the Pygal documentation, but I couldn't find anything relating to the alignment of a title. Here's what I have:

enter image description here

custom_style = Style(
    background = 'transparent',
    font_family='Avenir',
    colors = ['#14A1FF', '#14FF47'],
    opacity = .5)

chart = pygal.Line(
    style=custom_style, 
    print_values=True, 
    interpolate='hermite', 
    fill=True, dots_size=4, 
    show_y_guides=False,
    legend_at_bottom=True,
    legend_at_bottom_columns=2)

chart.title = "Rubik's Cube Solve Times Recorded Over Five Days"
chart.x_labels = ["1", "2", "3", "4", "5"]
chart.x_title = "Day"
chart.y_title = "Seconds"
chart.add("Average of 100", ao100)
chart.add("Average of 25", ao25)
chart.render_to_file('times.svg')
pcu
  • 1,204
  • 11
  • 27
neil_h_sw
  • 75
  • 4
  • Certainly, as you say there is nothing obvious in the documentation. It looks to me like you can only get the default location which is the centre. It appears that the X, and Y titles are centred relative to the axes, but the figure title is centred across the whole figure (so the axes + the y-axis title and tick-labels). This makes it appear to be off centre. This is more obvious on the pygal main page in their example plots (http://pygal.org/en/stable/). – Gavin Mar 24 '19 at 03:21

2 Answers2

1

By default you title has has property text-anchor:middle:

text-anchor attribute is used to align (start-, middle- or end-alignment) a string of text relative to a given point.

You can manually change this value, .i.e., to end in finale svg file (open file in text editor and find .title ).

pcu
  • 1,204
  • 11
  • 27
1

As mentioned in the comments the figure title is centred relative to the figure, rather than axes. This behaviour is hard-coded in the rendering functions, there are no configuration options that will change it.

One workaround is to create your own class that inherits from pygal.Line and over-rides the function that renders the title (which isn't very large):

class MyLineChart(pygal.Line):

    def __init__(self, *args, **kwargs):
        super(MyLineChart, self).__init__(*args, **kwargs)

    def _make_title(self):
        """Make the title"""
        if self._title:
            for i, title_line in enumerate(self._title, 1):
                self.svg.node(
                    self.nodes['title'],
                    'text',
                    class_='title plot_title',
                    x=self.margin_box.left + self.view.width / 2, # Modified
                    y=i * (self.style.title_font_size + self.spacing)
                ).text = title_line

The _make_title function above was copied straight from the source code for the Graph class (the class that Line itself inherits from). The only change is in the line indicated with the comment 'Modified', this was taken from the function that renders the x axis label (because that is centred on the axes).

With this you can replace chart = pygal.Line with chart = MyLineChart, but leave the rest of the code as it is. You might also want to change the name of the class to something more meaningful.

mostlyoxygen
  • 981
  • 5
  • 14
  • Sorry I never replied to this! I thought I did, but totally forgot to. Thanks so much for you help. This solution is really simple and it makes perfect sense. – neil_h_sw Jul 29 '19 at 21:30