Plotting - ge-semtk/semtk GitHub Wiki

SPARQLgraph includes capability to create plots from query results.

To add a plot

To add a plot to a nodegroup,

  • Load the nodegroup into SPARQLgraph and click "Run", which will show the results table.
  • In the Results pane, click the Plots button.
  • Click "Add" to display the "Add Plot" dialog. Select a name, graph type, and 2 table columns to plot.

  • Click "OK". The "Edit Plot" dialog will appear, showing the generated plot specification in a text editor. Using the text editor, you may customize the plot as specified in the Plotly Javascript library documentation. For example, you may modify the choices for x/y axes, specify a graph type, customize the legend, or specify colors.

  • Click "OK" to show the resulting plot.

  • Save the nodegroup to the store.

Specifying Data from the query

SemTK has a growing number of transformation functions that will take date from the query and inject it into the Plot.ly JSON.

Add a column

The function SEMTK_TABLE.col["?colName"] will be replaced by an array of values from the column name.

For example,

"data": [
        {
            "x": "SEMTK_TABLE.col[?timestamp]",
            "y": "SEMTK_TABLE.col[?Value]",
            "type": "scatter"
        }
]

will cause the values in columns ?timestamp and ?Value to be substituted into the JSON, resulting in something like this being sent to Plot.ly:

"data": [
        {
            "x":["2017-03-23T10:00:00","2017-03-23T10:01:00","2017-03-23T10:02:00"],
            "y":["0.24","0.01","0.15"],
            "type": "scatter"
        }
]

This will generate a plot such as the sample shown at the top of this page.

SEMTK_FOREACH: Splitting traces by values in a key column

The function SEMTK_TABLE.col["?colName"] can be combined with SEMTK_FOREACH json object to create separate traces for each unique value in a key column.

A SEMTK_FOREACH object can be used in this manner

  • applied to an object inside a JSON array
  • contains a "colName" field to specify the table column used to split
  • optionally contains a "fillWithValue" field which will receive the unique value of the target column

Example data and plot

This example shows how to tranform a table like this:

into a plot like this:

The Json

In this example, the column ?variableName has values of "temperature" or "status1", separate scatterplot traces for temperature Values and status1 Values:

    "data": [
        {
            "SEMTK_FOREACH": {
                "colName": "?variableName",
                "fillWithValue": "name"
            },
            "x": "SEMTK_TABLE.col[?timestamp]",
            "y": "SEMTK_TABLE.col[?Value]",
            "type": "scatter"
        }
    ],
}

Here, SemTK splits the results table into separate tables, each with a unique value of ?variableName, in this case "temperature" and "status1". It creates traces for each and inserts the value into the JSON field "name".

"data": [
        "data": [
        {
            "x":["2017-03-23T10:00:00","2017-03-23T10:01:00","2017-03-23T10:02:00",...],
            "y":["0.24","0.01","0.15",...],
            "name":"status1",
            "type":"scatter"
        },{
             "x":["2017-03-23T10:00:00","2017-03-23T10:01:00","2017-03-23T10:02:00",...],
             "y":["180.8","185.2","200.5",...],
             "name":"temperature",
             "type":"scatter"
        }
    ],
]

fillByValue: Configuring traces

SEMTK_FOREACH may be augmented with JSON snippets that can be assigned to trace depending on the value in the splitting column.

Add "fillByValue" JSON object where each key is a potential value and the contents should be added to the trace.

"SEMTK_FOREACH": {
 "colName": "variableName",
 "fillWithValue": "name",
 "fillByValue": {
    "temperature": {
       "mode": "markers",
       "marker": { "color": "rgb(219, 64, 82)", "size": 12 }
    },
    "status1": {
      "marker": { "color": "rgb(60, 60, 160)", "size": 6 },
      "type":"scatter"
    }
  }
}

This will add the given JSON to the "temperature" and "status1" traces, resulting in JSON like this being sent to the plotter:

"data":[
   {
      "name":"temperature",
      "mode":"markers",
      "marker":{"color":"rgb(219, 64, 82)","size":12},
      "x":["2017-03-23T10:00:00","2017-03-23T10:01:00","2017-03-23T10:02:00",...],
      "y":[180.8,185.2,200.5,...],"type":"scatter"
   },
   {
      "name":"status1",
      "marker":{ "color":"rgb(60, 60, 160)", "size":6  },
      "type":"scatter",
      "x":["2017-03-23T10:00:00","2017-03-23T10:01:00","2017-03-23T10:02:00",...],
      "y":[0.24,0.01,0.15,...]
   },
]

If the values in the splitting column are not known in advance, the keys in fillByValue may instead be SEMTK_TRACE_1, SEMTK_TRACE_2,... These are applied using these rules:

  • start with trace N = 1
  • if value in the splitting column value does match a key in fillByValue, search for SEMTK_TRACE_N
  • if found, apply SEMTK_TRACE_N and increment N

This JSON would have the same effect as the JSON above (except the SEMTK_TRACE_1 and SEMTK_TRACE_2 mapping to "temperature" or "status1" will depend on what order they occur in the results table):

"SEMTK_FOREACH": {
 "colName": "variableName",
 "fillWithValue": "name",
 "fillByValue": {
    "SEMTK_TRACE_1": {
       "mode": "markers",
       "marker": { "color": "rgb(219, 64, 82)", "size": 12 }
    },
    "SEMTK_TRACE_2": {
      "marker": { "color": "rgb(60, 60, 160)", "size": 6 },
      "type":"scatter"
    }
  }
}