March 18, 2022

Unite and annotate: Pine Tables now support headers and tooltips

Headers and cell merging

It’s now possible to merge several cells in a table to create fancy headers and separators. A merged cell doesn’t have to be a header though: you can merge cells in any direction, as long as the resulting cell doesn’t affect any already merged cells, and doesn’t go outside of the table’s bounds.

To create a merged cell, use the new table.merge_cells() function. The rules are simple. You need to pass the coordinates of the first and last cell you want to merge. Everything between them will be merged into a single cell. This size of a merged cell is automatically determined by the dimensions of its neighboring rows and columns.

In the example script below, you can see three merged cells: the first one merges all cells in row zero and contains “SMA Table”, the others below it create the sub-headers.

//@version=5
indicator("MA Ratings", overlay = true)

TRANSP = 80
TABLE_WIDTH = 6
var CELL_TOOLTIP = "Cell color depends on the SMA direction: green for rising SMAs, red for falling ones"

var smaLengths = array.from(10, 20, 50, 100, 200, 500)

smaValues = array.new_float()
for i = 0 to array.size(smaLengths) - 1
    array.push(smaValues, ta.sma(close, array.get(smaLengths, i)))

var maTable = table.new(position.top_right, TABLE_WIDTH, 4, border_width = 2)

if barstate.isfirst
    headerColor = color.new(color.blue, TRANSP)
    table.cell(maTable, 0, 0, text = "SMA Table", bgcolor = headerColor)
    table.cell(maTable, 0, 1, text = "Short-term", bgcolor = headerColor)
    table.cell(maTable, 2, 1, text = "Long-term", bgcolor = headerColor)
    table.merge_cells(maTable, 0, 0, 5, 0)
    table.merge_cells(maTable, 0, 1, 1, 1)
    table.merge_cells(maTable, 2, 1, 5, 1)

if barstate.islast
    for i = 0 to TABLE_WIDTH - 1
        cellTitle = str.format("SMA {0}", array.get(smaLengths, i))
        cellValue = array.get(smaValues, i)
        cellColor = color.new(array.get(smaValues, i) >= array.get(smaValues[1], i) ? color.green : color.red, TRANSP)
        table.cell(maTable, i, 2, bgcolor = cellColor, text = cellTitle, text_color = color.gray, tooltip = CELL_TOOLTIP)
        table.cell(maTable, i, 3, bgcolor = cellColor, text = str.tostring(cellValue,  format.mintick), tooltip = CELL_TOOLTIP)

Tooltips

The second improvement to tables are tooltips. Tooltips are floating text labels that appear when you hover over a table’s cell. They are handy to provide information on a cell’s content without crowding the table.

To create a cell with a tooltip, just pass a string to the new tooltip argument of the table.cell() function. See tooltips in action in our example script, where we use tooltip = CELL_TOOLTIP in the table.cell() call of the last two lines. The tooltips will look like this:

To stay informed of new Pine features, keep an eye on our Pine User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box on Telegram, its Twitter account, and from the Pine Script public chat on TradingView.

We hope you find this highly-requested feature useful. Please keep sending us your feedback and suggestions for improvement. We build TradingView for you, and we’re always keen to hear from you.

Look first / Then leap

Launch Chart