Home » Table Functions » Understanding table.cell_set_text_valign() Function in Pine Script

Understanding table.cell_set_text_valign() Function in Pine Script

Photo of author
Published on

This tutorial will guide you through the syntax, usage, and practical applications of table.cell_set_text_valign() function, ensuring your tables are not only informative but also visually appealing.

Syntax Overview

The function is defined as:

table.cell_set_text_valign(table_id, column, row, text_valign) → void

Let’s break down each argument for clarity:

  • table_id (series table): This is the identifier of the table object where the cell is located. You must create a table object beforehand and reference it here.
  • column (series int): Specifies the column index of the cell, starting from 0. This means the first column in the table is referenced with a 0.
  • row (series int): Specifies the row index of the cell, also starting from 0. This is consistent with the column indexing, where the first row is 0.
  • text_valign (series string): Determines the vertical alignment of the cell’s text. It accepts three values: text.align_top, text.align_center, and text.align_bottom, allowing you to align the text at the top, center, or bottom of the cell respectively.

Example Usage

To illustrate the use of table.cell_set_text_valign(), let’s create a simple table and align texts in different cells:

//@version=5
indicator("My Table Example", overlay=true)

// Creating a table
var myTable = table.new(position.top_right, columns=2, rows=3)

// Adding some text to the table cells
table.cell(myTable, 0, 0, "Top Align")
table.cell(myTable, 1, 0, "Center Align")
table.cell(myTable, 0, 1, "Bottom Align")

// Setting vertical alignment
table.cell_set_text_valign(myTable, 0, 0, text.align_top)
table.cell_set_text_valign(myTable, 1, 0, text.align_center)
table.cell_set_text_valign(myTable, 0, 1, text.align_bottom)
Example

This code snippet creates a table with 2 columns and 3 rows, then assigns different vertical alignments to texts in various cells. Note how the table_id, column, and row parameters target the specific cell for alignment adjustment.

Deep Dive into Each Line of Code

  1. //@version=5: This specifies the version of Pine Script being used. In this case, it’s version 5.
  2. indicator("My Table Example", overlay=true): This line defines the indicator’s title and specifies that it will be overlaid on the price chart.
  3. var myTable = table.new(position.top_right, columns=2, rows=3): This line creates a new table with 2 columns and 3 rows, positioned at the top-right corner of the chart. The var keyword declares a variable named myTable to hold a reference to the created table.
  4. table.cell(myTable, 0, 0, "Top Align"), table.cell(myTable, 1, 0, "Center Align"), table.cell(myTable, 0, 1, "Bottom Align"): These lines populate the table cells with text. The table.cell() function takes the table reference (myTable), row index, column index, and the text to be displayed in the specified cell.
  5. table.cell_set_text_valign(myTable, 0, 0, text.align_top), table.cell_set_text_valign(myTable, 1, 0, text.align_center), table.cell_set_text_valign(myTable, 0, 1, text.align_bottom): These lines set the vertical alignment of the text within specific cells. The table.cell_set_text_valign() function takes the table reference (myTable), row index, column index, and the alignment type (e.g., text.align_top, text.align_center, text.align_bottom) to align the text within the specified cell vertically.

Key Features and Takeaways

  • Functionality: Allows precise control over the appearance of text within table cells, enhancing the readability and aesthetic of data presentation.
  • Syntax Flexibility: Accepts dynamic references for both row and column, enabling programmatic adjustments to table layouts.
  • Application: Ideal for financial dashboards, trading indicators, or any application requiring data presentation in tables on trading charts.

By mastering the table.cell_set_text_valign() function, you enhance your ability to create informative and visually appealing tables in Pine Script, thereby elevating the user experience of your trading indicators or strategies.

Leave a Comment