Embed in HTML
Save the code below as basic_line.tql and we will show you how to embed the result of this TQL into web page.
FAKE( linspace(0, 360, 100))
MAPVALUE(2, sin((value(0)/180)*PI))
CHART(
theme("white"),
chartOption({
"xAxis": { "type": "category", "data": column(0) },
"yAxis": {},
"series": [ { "type": "line", "data": column(1) } ]
})
)IFRAME
| |
JSON Response
Call .tql script file with a custom HTTP header X-Tql-Output: json
Since v8.0.42
(which replaces the deprecated header X-Chart-Output
Since v8.0.14
)
to produce the result in JSON instead of full HTML document,
so that caller can embed the chart into any place of the HTML DOM.
The X-Tql-Output: json header is actually equivalent to the CHART() SINK with chartJson(true) option like CHART( chartJson(true), chartOption({...})).
The example of chartJson(true) can be found in the section “As Reading API”.
When the reponse of /db/tql is JSON, it contains required addresses of the result javascript.
{
"chartID": "NDg4ODQ4MzMxMjgyMDYzMzY",
"jsAssets": ["/web/echarts/echarts.min.js"],
"jsCodeAssets": ["/web/api/tql-assets/NDg4ODQ4MzMxMjgyMDYzMzY.js"],
"style": {
"width": "600px",
"height": "600px"
},
"theme": "white"
}chartIDrandom generated chartID of echarts, a client can set a specific ID withchartID()option.jsAssetsserver returns the addresses of echarts resources. The array may contains the main echarts (echarts.min.js) and extra plugins javascript files.jsCodeAssetsmachbase-neo generates the javascript to properly render the echarts with the result data.
The HTML document below is an exmaple to utilize the JSON response above to render echarts.
| |
- Line 3, Pre-load apache echarts library which is included in
jsAssetsfields in above response example. - Line 14, The HTTP header
X-Tql-Output: json. so that machbase-neo TQL engine generates a JSON containing meta information of chart instead of full HTML document. Becuase when a client requests a*.tqlfile withGETmethod, machbase-neo generates HTML document for the chart by default. - Line 23, Load js files into the HTML DOM tree that are generated and replied in
jsCodeAssets.
Dynamic TQL
The api /db/tql can receive POSTed TQL script and produces the result in javascript.
Caller side javascrpt can load the result javascript dynamically as the example below.
In this example, the chartID() (line 20) is provided and the document has a <div> with the same id.
| |
Loading Sequence Problem
In the examples above, if we tried to load the both of jsAssets and jsCodeAssets dynamically, like below code for example.
| |
There must be some loading sequence issue, because the chart library (apache echarts) in obj.jsAssets might not be completely loaded
before obj.jsCodeAssets are loaded.
To avoid the problem of loading sequence, it can be fixed like below code.
Add load event listener to enable callback for load-completion.
| |
When the last jsAssets loaded, start to load jsCodeAssets.
| |