Label settings

Let's take a look at a traditional line chart where we can use labels. Here's a simple chart with some dummy datasets. Copy the code into your editor of choice and run the script. Your output should be what is shown in the next screenshot.

You can also set label settings using similar parameters used with our legend. Let's build a simple line chart that we can see our changes on. Add the following code to an editor of your choice and run the script. Be sure to save the output as lineparam.svg:

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

param_line_chart = pygal.Line()
param_line_chart.title = 'Parameter Line Chart'
param_line_chart.x_labels = map(str, ["Data Object 1", "Data Object 2", "Data Object 3", "Data Object 4", "Data Object 5", "Data Object 6"])
param_line_chart.add('Data-Set 1', [8, 16, 24, 32, 48, 56])
param_line_chart.add('Data-Set 2', [2, 4, 6, 8, 10, 12])
param_line_chart.add('Data-Set 3', [1, 3, 5, 7, 9, 12])

param_line_chart.render_to_file('lineparam.svg')

The following screenshot shows the results of our script:

Label settings

Line charts have some specific parameters, such as fill=true, as mentioned in Chapter 3, Getting Started with pygal. You can also resize the specified labels using the label_font_size parameter, as shown in the following code:

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

param_line_chart = pygal.Line(fill=True, label_font_size=20)
param_line_chart.title = 'Parameter Line Chart'
param_line_chart.x_labels = map(str, ["Data Object 1", "Data Object 2", "Data Object 3", "Data Object 4", "Data Object 5", "Data Object 6"])
param_line_chart.add('Data-Set 1', [8, 16, 24, 32, 48, 56])
param_line_chart.add('Data-Set 2', [2, 4, 6, 8, 10, 12])
param_line_chart.add('Data-Set 3', [1, 3, 5, 7, 9, 12])

param_line_chart.render_to_file('lineparam.svg')

The following screenshot shows the results of our script:

Label settings

We can rotate the labels using the x_label_rotation parameter, and remove the lines all together using the stroke=false parameter, as shown in the following code:

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

param_line_chart = pygal.Line(fill=False, stroke=False, label_font_size=20, x_label_rotation=50)
param_line_chart.title = 'Parameter Line Chart'
param_line_chart.x_labels = map(str, ["Data Object 1", "Data Object 2", "Data Object 3", "Data Object 4", "Data Object 5", "Data Object 6"])
param_line_chart.add('Data-Set 1', [8, 16, 24, 32, 48, 56])
param_line_chart.add('Data-Set 2', [2, 4, 6, 8, 10, 12])
param_line_chart.add('Data-Set 3', [1, 3, 5, 7, 9, 12])

param_line_chart.render_to_file('lineparam.svg')

The following screenshot shows the results of our script:

Label settings

Line charts have one specific parameter, interpolate='cubic', which allows the line to curve in the data. Here's an example:

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

param_line_chart = pygal.Line(interpolate='cubic', label_font_size=20, x_label_rotation=50)
param_line_chart.title = 'Parameter Line Chart'
param_line_chart.x_labels = map(str, ["Data Object 1", "Data Object 2", "Data Object 3", "Data Object 4", "Data Object 5", "Data Object 6"])
param_line_chart.add('Data-Set 1', [8, 16, 24, 32, 48, 56])
param_line_chart.add('Data-Set 2', [2, 4, 6, 8, 10, 12])
param_line_chart.add('Data-Set 3', [1, 3, 5, 7, 9, 12])

param_line_chart.render_to_file('lineparam.svg')

The following screenshot shows the results of our script:

Label settings
..................Content has been hidden....................

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