Wednesday, June 22, 2016

Spider Chart maker using Python and Pygame

16th May 2017 - Updated to make the output an A4 landscape page.
Also, provided Inkscape created SVG file so you can prepare you can add your own School or Club name.  Make sure the final PNG resolution for __spider.png is 3508 x 2481 as the dot locations are hand coded for the spider to be in the location it is on this size of image.

Example of improved chart.



A few days ago a call went on out on Twitter for how to make lots of Spider Charts, also known as Radar charts in a programmatic way.  I decided to take up the challenge.


The code and associated files are available to download from GitHub

Below is a sample completed chart

To do this I started with a blank version of the chart and added the person name top left and the red dots and lines for the individual scores.

The program is written using Python and Pygame and was developed on the Raspberry Pi.
Since it doesn't use any other external libraries I'd expect it to work on other systems with Pygame installed.

To achieve this 2 external CSV files and a large (3508 x 2481) blank chart image are used.

The image file is: __spider.png

spider_data_points.csv is a comma separated variable list of the co-ordinates on the 3508 x 2481 image for the individual points.  By using a separate lookup if this was to be adapted to a different chart then you can just use a new master image and a different set of co-ordinates.

Student_Scores.csv is the file with the individual student (in this case) names and scores for the different skills.  There is no limit to the number of students that can have charts created as the code just goes through the csv line by line and creates a chart for each one.

Rather than creating the csv files manually it is so much easier to use a spreadsheet for data entry and then [Save As] a .csv
To facilitate this I've include .xlsx versions of the files.
These can be open and saved on the Raspberry Pi using OpenOffice.

Meaning if you needed to make 100 of these charts it would be a simple matter of:
Place all the code in a single directory
Enter the data in the spreadsheet.
Save as a .csv
execute spiderchart.py
Watch as all the charts are created automatically with the student name as the filename

Don't worry if the image is mainly off screen it is still being done correctly.

In the code there is a 1 second sleep after each chart is created. If you are doing lots then it might be worth commenting out the sleep.  It's useful for seeing what's going on.

As a complete side note: I was one of the backers of the Indiegogo pi-topCEED an All-in-One form factor for a Raspberry Pi desktop and it was delivered and set up on Monday.  It came with a Raspberry Pi 3.  This was the set up I used to develop and run the code.  I was very impressed by the quality of the 14" screen on the CEED.  This is now set up as the permanent home computer that my kids can use for playing Minecraft and exploring the CEEDuniverse, a game that teaches you about computers, coding and electronics.