Insight

Turn SPARQL queries into charts and views

With the graph populated, analysis is a query away — and in LinkedDataHub, queries, charts and views are resources like everything else. A chart is three resources chained together: a sp:Select query holding the SPARQL text, a ldh:ResultSetChart describing how to plot its results, and a ldh:Object block placing it on a document.

A revenue chart

On the Categories container, revenue per category — the query joins order items to products to categories across their documents:

<#category-revenue-query> a sp:Select ;
    dct:title "Category revenue" ;
    sp:text '''PREFIX schema: <https://schema.org/>

SELECT ?categoryName (SUM(?sale) AS ?revenue)
WHERE {
    GRAPH ?orderGraph {
        ?order schema:orderedItem ?orderItem .
        ?orderItem schema:orderedItem ?product ;
                   schema:orderQuantity ?quantity ;
                   schema:price ?price .
        BIND(?quantity * ?price AS ?sale)
    }
    GRAPH ?productGraph {
        ?product schema:category ?category .
    }
    GRAPH ?categoryGraph {
        ?category schema:name ?categoryName .
    }
}
GROUP BY ?category ?categoryName
ORDER BY DESC(?revenue)''' .

<#category-revenue> a ldh:ResultSetChart ;
    dct:title "Revenue by category" ;
    spin:query <#category-revenue-query> ;
    ldh:chartType <https://w3id.org/atomgraph/client#BarChart> ;
    ldh:categoryVarName "categoryName" ;
    ldh:seriesVarName "revenue" .

<#category-revenue-block> a ldh:Object ;
    rdf:value <#category-revenue> .

Add <#category-revenue-block> to the container's block sequence (rdf:_2 after the intro block) and PUT the document as before.

A view

Views render query results as document listings instead of plots — the container's own children listing is one. A grid of all categories:

<#select-categories-view> a ldh:View ;
    spin:query <#select-categories-query> ;
    ac:mode <https://w3id.org/atomgraph/client#GridMode> .

In the browser

The same resources can be created interactively: save a query from the query editor, then add a chart block that references it.

What you now see

The Categories container is a small dashboard: a revenue bar chart, a products-per-category chart, and a grid of the categories themselves — all live views over the graph, recomputed on each request.

Next: Composition