Thursday, 16 August 2018

How to create a histogram legend in QGIS print composer?


I've found this blog post while looking for a way to dynamically build a graph (histogram) in print composer.


The feature I want to replicate from the blog is drawing static rectangles in the qgis print composer and then size them based on a field value in this case as bars on a histogram.


Thus creating a legend/ histogram


I can follow this tutorial but I was just curious if a) there was an easier way outside of building the graph i need in matplotlib and importing an image or b) this functionality was going to be built in to qgis in future versions.


enter image description here



Answer




I've seen that blog post too, and wondered the same thing!


You could use HTML Frames and the D3 library, if you don't mind doing a bit of JavaScript.


Here's an example which I hard-coded. You can insert QGIS Expressions into your page source (from QGIS 2.14 or later)


example of d3 html frame in qgis print composer


In my case, I've saved the html file locally and hacked it until I got it to display, using browser's developer tools.


Once you have it working, you should be able to paste the page source into the 'source' text box directly, and drop QGIS expressions in (such as field values, variables etc.) using the Insert an expression button.


I cobbled together that example from alignedleft.com. This tutorial is useful as it shows how to build bar charts from simple lists; most examples I've seen use separate TSV files.


Bl.ocks.org has a very large set of examples.


You can get code for the AlignedLeft tutorials here on GitHub


This might make a good plugin... I see there are a couple of d3 plugins already, although I've not had a chance to try them - worth a look maybe?



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...