This article delves into the syntax, usage, and application of the table.set_border_width()
function, providing clear examples and explanations to enhance your Pine Script programming skills.
Syntax
The syntax for the table.set_border_width()
function is straightforward:
table.set_border_width(table_id, border_width) → void
Arguments
- table_id (series table): This argument specifies the table object whose cell borders you intend to modify. It is essential to have created a table beforehand using the
table.new()
function. - border_width (series int): This argument defines the width of the borders inside the table, excluding the outer frame. It is an optional parameter, with a default value of 0, meaning no borders will be visible unless specified otherwise.
Example
To illustrate how the table.set_border_width()
function works, let’s create a simple example. We’ll first generate a table and then set the border width of its cells.
//@version=5 indicator("Color Adjusted Table Example", overlay=true) // Create a table with red background for headers and blue for data myColorAdjustedTable = table.new(position = position.top_right, columns = 3, rows = 2, bgcolor = color.new(color.red, 40)) // Set the border width of the table's cells with blue borders table.set_border_width(myColorAdjustedTable, 2) // Populate header cells with data, using red background table.cell(myColorAdjustedTable, 0, 0, "Asset", bgcolor=color.new(color.red, 10)) table.cell(myColorAdjustedTable, 1, 0, "Price", bgcolor=color.new(color.red, 10)) table.cell(myColorAdjustedTable, 2, 0, "Change", bgcolor=color.new(color.red, 10)) // Adding a row of data with blue background table.cell(myColorAdjustedTable, 0, 1, "BTC", bgcolor=color.new(color.blue, 10)) table.cell(myColorAdjustedTable, 1, 1, "50000", bgcolor=color.new(color.blue, 10)) table.cell(myColorAdjustedTable, 2, 1, "+5%", bgcolor=color.new(color.blue, 10))
Walkthrough of the Code
- Indicator Declaration:
indicator("Color Adjusted Table Example", overlay=true)
: This line declares a new indicator named “Color Adjusted Table Example” that will be overlaid on the main chart. Theoverlay=true
parameter specifies that the indicator should be drawn directly on the chart.
- Table Creation:
myColorAdjustedTable = table.new(position = position.top_right, columns = 3, rows = 2, bgcolor = color.new(color.red, 40))
: Creates a new table variablemyColorAdjustedTable
with the table displayed at the top right of the chart. It specifies that the table should have 3 columns and 2 rows. The background color of the table is set to a semi-transparent red usingcolor.new(color.red, 40)
, where40
indicates the transparency level.
- Set Table Border Width:
table.set_border_width(myColorAdjustedTable, 2)
: Sets the border width of the cells inmyColorAdjustedTable
to2
. This affects how thick the border lines around each cell will appear.
- Populate Header Cells:
table.cell(myColorAdjustedTable, 0, 0, "Asset", bgcolor=color.new(color.red, 10))
: Adds the header “Asset” to the first cell in the first row with a less transparent red background.table.cell(myColorAdjustedTable, 1, 0, "Price", bgcolor=color.new(color.red, 10))
: Adds the header “Price” to the second cell in the first row.table.cell(myColorAdjustedTable, 2, 0, "Change", bgcolor=color.new(color.red, 10))
: Adds the header “Change” to the third cell in the first row. Thebgcolor
for these headers is a less transparent red compared to the table’s background, making the headers slightly stand out.
- Adding Data Row:
- The next three lines of code add a row of data beneath the headers.
table.cell(myColorAdjustedTable, 0, 1, "BTC", bgcolor=color.new(color.blue, 10))
: This adds “BTC” to the first cell in the second row with a semi-transparent blue background.table.cell(myColorAdjustedTable, 1, 1, "50000", bgcolor=color.new(color.blue, 10))
: Adds “50000” to the second cell in the second row as the price.table.cell(myColorAdjustedTable, 2, 1, "+5%", bgcolor=color.new(color.blue, 10))
: Adds “+5%” to the third cell in the second row as the change percentage.
Key Features and Takeaways
- Customization: The
table.set_border_width()
function allows for detailed customization of tables in Pine Script, enabling clearer data presentation. - Syntax and Application: Understanding the function’s syntax is crucial for effectively applying it to your scripts. Remember, the
table_id
must reference an existing table, and theborder_width
determines the internal cell borders’ thickness. - Visual Enhancement: Adjusting the border width can significantly enhance the visual appeal and readability of the table, making it easier to distinguish between different data points or categories.