Stacked line charts

Stacked line charts work in a manner similar to traditional line charts, but they stack multiple sets of data over each other to show the specific values for a group. Copy the following code into your project's main Python file and run the file. Also, take note of the multiple add() functions on our chart. Since the chart has multiple datasets in one chart, we need to create a dataset for each:

# -*- coding: utf-8 -*-
import pygal

#create a new stacked line chart.
line = pygal.StackedLine(fill=True)
line.title = 'Web hits in the past 2 years' #set chart title
line.x_labels = map(str, range(2012, 2014)) #set the x-axis labels.
line.add('Site A', [None, 0, 12, 32, 72, 148]) #set values.
line.add('Site B', [2, 16, 12, 87, 91, 342]) #set values.
line.add('Site C', [42, 55, 84, 88, 90, 171]) #set values.
line.render_to_file('linechart.svg') #set filename.

The following screenshot shows the results of our script:

Stacked line charts

Once rendered, your stacked chart will look like what's shown in the preceding screenshot. Open the directory your project is in to find the linechart.svg file. Note how pygal overrides your original SVG file by default; keep this in mind when working with this library. Also, you will notice that we added a fill=True parameter to our StackedLine function when we declared our chart; this is a chart parameter. More on this later, but here we can see that filled colors are added below the chart's line.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.116.137