Help:Displaying numerical distributions

Semantic extension(s): Semantic Result Formats
Further extension(s):  -/-
Keyword(s):  -/-


It can be quite helpful to use inline queries to calculate the distribution, or popularity, of the values of a single semantic property. For instance, if you have a wiki about locations around the world, with each location listing (semantically) the country that it is contained in, it might be interesting to see a table showing a listing of countries and the number of locations that each one has on the wiki. If you use the Semantic Result Formats extension, you could even show that information in a pie chart or bar graph form.

For charts[edit]

If you want to show such a distribution in a bar or pie chart, there's a very easy way to do this: use the "distribution=yes" parameter. Here's an example, for a pie chart showing the distribution of countries for the cities whose information is contained on this wiki. The query is:

{{#ask:[[Category:City]][[Located in::+]]
|?Located in
NoteNote: Make sure that the setting $smwgQMaxInlineLimit (default: 500) exceeds the number of results, which is here the number of cities with some value for Property:Locatin in. Otherwise, your chart will not be accurate.

For tables and other display formats[edit]

If you want to instead display this numerical information in a table or another, non-graphical display format, it's still possible to do, but it requires more work; you'll need to take an intermediate step. Each "parent" page must include a 'count' query, getting the number of its "children", and then contain a semantic property linking to that number. So, for our example, you would create a new property, perhaps called "Contains number of locations", of type Number. If the semantic property between a location and its country were called "Has country", you would then add to each country page the following call:

[[Contains number of locations::{{#ask:[[Has country::{{PAGENAME}}]]|format=count}}| ]]

If country pages are defined using templates, then you only need to add this call once, to the template.

Then, you could create a table showing this information with the following call:

{{#ask:[[Contains number of locations::+]]|sort=Contains number of locations|

You could change the value of 'format' to produce different types of displays.

One big weakness of this approach is that the numbers will not automatically change in the display as the actual values change; that is because the semantic values in each of the "sub-pages" (in the example, pages for countries) do not automatically refresh themselves: instead, this refreshing needs to happen manually. If all the sub-pages are defined by a single template, an easy way to do this is to make a small change to the template's page and re-save it; this will cause the semantic data of all the pages that include that template to eventually be refreshed. Another approach is to refresh all of the wiki's semantic data at regular intervals.

See here for one demonstration of a numerical distribution, on the site Discourse DB.