Solid Gauge Chart in Pygal

Solid Gauge Chart in Pygal

Pygal, a Python library for creating SVG (Scalable Vector Graphics) charts, supports various types of charts including solid gauge charts. A solid gauge chart is useful for displaying progress or completion metrics.

Here's a step-by-step guide on how to create a solid gauge chart using Pygal:

Step 1: Install Pygal

If you haven't installed Pygal, you can do so using pip:

pip install pygal 

Step 2: Import Pygal

In your Python script, import Pygal:

import pygal 

Step 3: Create a Solid Gauge Chart

Instantiate a SolidGauge chart object and add your data. In a solid gauge chart, data is typically added as a percentage.

gauge_chart = pygal.SolidGauge( half_pie=True, inner_radius=0.70, style=pygal.style.styles['default'](value_font_size=10) ) percent_formatter = lambda x: '{:.10g}%'.format(x) dollar_formatter = lambda x: '${:.10g}'.format(x) gauge_chart.value_formatter = percent_formatter # Add data to the chart gauge_chart.add('Completion', [{'value': 75, 'max_value': 100}]) 

In this example:

  • half_pie=True makes the gauge a half circle.
  • inner_radius=0.70 adjusts the radius of the gauge.
  • value_formatter is used to format the display of the values. Here, it's set to display percentages.

Step 4: Render the Chart to a File

Render the chart to an SVG file:

gauge_chart.render_to_file('solid_gauge_chart.svg') 

This command will create an SVG file named 'solid_gauge_chart.svg' in your current directory.

Complete Example

Here's the complete code:

import pygal # Create a solid gauge chart gauge_chart = pygal.SolidGauge( half_pie=True, inner_radius=0.70, style=pygal.style.styles['default'](value_font_size=10) ) percent_formatter = lambda x: '{:.10g}%'.format(x) gauge_chart.value_formatter = percent_formatter # Add data gauge_chart.add('Completion', [{'value': 75, 'max_value': 100}]) # Save the chart to a file gauge_chart.render_to_file('solid_gauge_chart.svg') 

Note

  • Solid gauge charts in Pygal are simple and effective for displaying single-value metrics like completion percentages.
  • Pygal charts are SVGs, which are scalable and suitable for web applications. If you need a different format (like PNG), you'll have to convert the SVG or use a tool/library that can render the chart in the desired format.
  • Pygal offers customization options for styling your charts, so you can adjust the appearance to match your needs or preferences.

More Tags

date-difference goland clob data-visualization webclient razor-components alter-table airflow-scheduler traversal file-storage

More Programming Guides

Other Guides

More Programming Examples