Skip to main content

Creates svg based charts in python

Project description

A Python package for creating and rendering SVG charts, including line charts, axes, legends, and text labels. This package supports both simple and complex chart structures and is highly customisable for various types of visualisations.

Why did I make this project

This project is designed for charts that are easily embedded into python web applications (or other web applications) with minimum fuss.

Many charting libraries for the web rely on JavaScript-driven client-side rendering, often requiring an intermediate canvas before producing a polished visual. On the other hand, popular python based charting libraries focus on image-based rendering. Such images are rigid and intractable once embedded into web applications and detailed customisation is impossible. If such libraries do generate resolution independent output it is very difficult to customise.

This package takes a different approach: it generates clean, standalone SVG charts entirely within Python that can be immediately embedded into a web application. By leveraging SVG’s inherent scalability and styling flexibility, it eliminates the need for JavaScript dependencies, client-side rendering, or post-processing steps. The result is a lightweight, backend-friendly solution for producing high-quality, resolution-independent charts without sacrificing control or maintainability.

Every chart element is designed to be easily modified, giving developers precise control over appearance and structure. As such, all of the lower level elements are accessible via properties of the charts.

Installation

pip install pysvgchart

Alternatively, you can clone this repository and install it locally:

git clone https://github.com/arowley-ai/py-svg-chart.git
cd py-svg-chart
pip install .

Usage

Usage depends on which chart you had in mind but each one follows similar principles.

Simple donut chart

Create a simple donut chart:

import pysvgchart as psc

values = [10, 20, 30, 40]
donut_chart = psc.DonutChart(values)
svg_string = donut_chart.render()

The following is the output of this code

Simple donut chart example

Simple line chart

Create a simple line chart:

import pysvgchart as psc

 x_values = list(range(100))
 y_values = [4000]
 for i in range(99):
     y_values.append(y_values[-1] + 100 * random.randint(0, 1))

 line_chart = psc.SimpleLineChart(
     x_values=x_values,
     y_values=[y_values, [1000 + y for y in y_values]],
     y_names=['predicted', 'actual'],
     x_max_ticks=20,
     y_zero=True,
 )
 line_chart.add_grids(minor_y_ticks=4, minor_x_ticks=4)
 line_chart.add_legend()

 svg_string = line_chart.render()

The following is the output of this code

Simple line chart example

More stylised example

The following code demonstrates a heavily customised line chart example

import pysvgchart as psc

def y_labels(num):
    num = float('{:.3g}'.format(num))
    magnitude = 0
    while abs(num) >= 1000:
        magnitude += 1
        num /= 1000.0
    rtn = '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', 'K', 'M', 'B', 'T'][magnitude])
    return rtn.replace('.00', '').replace('.0', '')

def x_labels(date):
    return date.strftime('%b')

dates = [dt.date.today() - dt.timedelta(days=i) for i in range(500) if (dt.date.today() + dt.timedelta(days=i)).weekday() == 0]
actual = [(1 + math.sin(d.timetuple().tm_yday / 183 * math.pi)) * 50000 + 1000 * i + random.randint(-10000, 10000) for i, d in enumerate(dates)]
expected = [a + random.randint(-10000, 10000) for a in actual]
line_chart = psc.SimpleLineChart(x_values=dates, y_values=[actual, expected], y_names=['Actual sales', 'Predicted sales'], x_max_ticks=30, x_label_format=x_labels, y_label_format=y_labels, width=1200)
line_chart.series['Actual sales'].styles = {'stroke': "#DB7D33", 'stroke-width': '3'}
line_chart.series['Predicted sales'].styles = {'stroke': '#2D2D2D', 'stroke-width': '3', 'stroke-dasharray': '4,4'}
line_chart.add_legend(x_position=700, element_x=200, line_length=35, line_text_gap=20)
line_chart.add_y_grid(minor_ticks=0, major_grid_style={'stroke': '#E9E9DE'})
line_chart.x_axis.tick_lines, line_chart.y_axis.tick_lines = [], []
line_chart.x_axis.axis_line = None
line_chart.y_axis.axis_line.styles['stroke'] = '#E9E9DE'
line_end = line_chart.legend.lines[0].end
styles = {'fill': '#FFFFFF', 'stroke': '#DB7D33', 'stroke-width': '3'}
line_chart.add_custom_element(psc.Circle(x_position=line_end.x, y_position=line_end.y, radius=4, styles=styles))
line_end = line_chart.legend.lines[1].end
styles = {'fill': '#2D2D2D', 'stroke': '#2D2D2D', 'stroke-width': '3'}
line_chart.add_custom_element(psc.Circle(x_position=line_end.x, y_position=line_end.y, radius=4, styles=styles))
for limit, tick in zip(line_chart.x_axis.limits, line_chart.x_axis.tick_texts):
    if tick.content == 'Jan':
        line_chart.add_custom_element(psc.Text(x_position=tick.position.x, y_position=tick.position.y + 15, content=str(limit.year), styles=tick.styles))

The following is the output of this code

Complex line chart example

Contributing

We welcome contributions! If you’d like to contribute to the project, please follow these steps:

  • Fork this repository.

  • Optionally, create a new branch (eg. git checkout -b feature-branch).

  • Commit your changes (git commit -am ‘Add feature’).

  • Push to the branch (eg. git push origin feature-branch).

  • Open a pull request.

Created a neat chart?

All of the charts in the showcase folder are generated by pytest. If you create something neat that you’d like to share then see if it can be added to the test suite and it will be generated alongside other showcase examples.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysvgchart-0.0.11.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysvgchart-0.0.11-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pysvgchart-0.0.11.tar.gz.

File metadata

  • Download URL: pysvgchart-0.0.11.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for pysvgchart-0.0.11.tar.gz
Algorithm Hash digest
SHA256 3b4563982c3cbf53983939615a5e473b58ce0a98a7f567b953ac2e0eb0d3b306
MD5 3c9b900cbe1c86d0cc9bb6ba5fb74fa8
BLAKE2b-256 ea342c297143908f8b9f2e5a3234557a049d0c19c258c40aa819fb5869a884c9

See more details on using hashes here.

File details

Details for the file pysvgchart-0.0.11-py2.py3-none-any.whl.

File metadata

  • Download URL: pysvgchart-0.0.11-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for pysvgchart-0.0.11-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0460db0200a3ec58df6b13a3cc1333f8440bbc431d727499902af89bd3eeb28e
MD5 1fc572487d4c82ee1e1c5ec644ee2605
BLAKE2b-256 c1ebf67b5346d62d70a4084f87af732b08d945ec088c4640db12f8bf32680724

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page