Sunday, 24 January 2016

qgis - Charts and Graphs that only display features in current extent


Using QGIS, QGIS Server, and Lizmap3/3Liz I am trying to make a pie chart that compares attributes but only of the features in the current extent. So when you pan around the webmap the graph changes based on the features in view. I know it can be done in the composer with attribute tables but how can it be accomplished using Lizmap and Dataviz from within the project?


The diagrams feature only works on single features. What I am trying to do is create a chart from the attributes of the features in view with a pie graph nested in a docker to show me, for an example, within the current view how many house holds are there and of those house holds what percentage makes more/less than $xx.xx. As you pan around your statistics will change based on area and how many homes with data are in the extent; and of course if you zoomed all the way out you would gets stats on everything in the view.




Answer



How to display diagrams for all selected features


Generate diagrams for aggregated attributes of all selected features with an expression using the is_selected() and aggregate functions. For example, this expression calculates the total "population" for selected features: sum( "population", '',is_selected()).


Combine attributes as desired into one diagram. Other Aggregate functions include:



  • count and count_distinct

  • iqr (inter quartile range), q1 (calculated first quartile) and q3 (calculated third quartile)

  • majority (most commonly occurring value) and minority

  • mean and median

  • maximum and minimum


  • stdev (standard deviation)

  • sum


To display only one diagram, add a virtual field with this expression, where "id" is a field where each feature has a unique value: "id" = minimum( "id" , '', is_selected()). Use the new field to control "Show diagram" in Layer Properties > Diagram tab > Rendering > Visibility.




How to adapt the method described above to display diagrams for visible features


To change this method to work for features in the map canvas, create a custom python function that refers features within the current map canvas. Substitute your custom function for is_selected() in the expressions above.


It might be possible to use the canvasx() and canvasy() functions in the expression builder instead of creating a custom function, but these functions seem to be broken at the moment.


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...