1

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.

2
  • I don't know about Lizmap and Dataviz, but in QGIS you can create pie charts on the map canvas using the diagrams feature.
    – csk
    Commented Nov 9, 2018 at 19:16
  • Yes; it 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.
    – MrKingsley
    Commented Nov 13, 2018 at 12:11

1 Answer 1

1

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.

4
  • It seems that canvasx/canvasy are only available in QGIS3, I should have indicated that I am using the LTR. Is there any function, or set of functions, that can get the current extent without a custom python function? I really wouldn't know where to start on building one.
    – MrKingsley
    Commented Nov 15, 2018 at 13:13
  • I am going to accept this as the answer, it is generally what I was thinking I would need to do I just didn't think I would need to make a custom python control. I started a new question to get assistance with the control at gis.stackexchange.com/questions/302790/…. Thank you for the help.
    – MrKingsley
    Commented Nov 15, 2018 at 15:31
  • I believe that working with selected features instead of those within the map canvas is the closest you can get in 2.18 without using python. If anyone else knows of a way, hopefully they will post a separate answer.
    – csk
    Commented Nov 15, 2018 at 16:57
  • Joseph helped me with the python function, I had the right idea but was calling the wrong class for the extent. That conversation can be found here... gis.stackexchange.com/questions/302790/… if anyone else is interested.
    – MrKingsley
    Commented Nov 15, 2018 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.