> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.ninjatrader.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.ninjatrader.com/_mcp/server.

# GetBarIdxByX()

## Definition

Returns the **ChartBars** index value at a specified x-coordinate relative to the ChartControl.

## Method Return Value

An **int** value representing the bar index.

## Syntax

`ChartBars.GetBarIdxByX(ChartControl chartControl, int x)`

## Method Parameters

| Parameter        | Description                                                         |
| ---------------- | ------------------------------------------------------------------- |
| **chartControl** | The **ChartControl** object used to determine the chart's time axis |
| **x**            | The x-coordinate used to find a bar index value                     |

## Examples

```csharp
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
   // get the users mouse down point and convert to device pixels for DPI accuracy
   int mousePoint = chartControl.MouseDownPoint.X.ConvertToHorizontalPixels(chartControl.PresentationSource);
   
   // convert mouse point to bar index
   int barIdx = ChartBars.GetBarIdxByX(chartControl, mousePoint);
   
   Print("User clicked on Bar #" + barIdx);
}
```