Can't aligned text on Chart

Created at 07 Jan 2020, 12:52
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MY

mylowsmoke

Joined 22.10.2019

Can't aligned text on Chart
07 Jan 2020, 12:52


Hello, I have for some time trying to align text in column on the Chart but to no avail. Anyone has a solution to this problem? Please help.

The example code as follows:

string text = "\t" + "Opening Price: " + "\t" + MarketSeries.Open.Last(0) + "\t" + MarketSeries.Open.Last(1) + "\t" + MarketSeries.Open.Last(2) + "\n" + "\t" + "Target met?:" + "\t" + "No" + "\t" + "No" + "\t" + "Yes";

 

var description = Chart.DrawStaticText("line1", text, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Blue);


cTrader Automate
@mylowsmoke
Replies

redtick123
07 Jan 2020, 17:01 ( Updated at: 21 Dec 2023, 09:21 )

It's hard to do it with static text. If you want to control alignment it's better to use custom UI controls. You can use Grid to make a table. But it is a bit complicated, for each cell you need to create a TextBlock.

I'm working on DataGrid control, that makes it easier.

1. Get class from here and paste it to your cBot or indicator:
https://raw.githubusercontent.com/redtick123/cTraderRedUI/master/DataGrid.cs

2.  cBot with your example will look like this:

private DataGrid DataGrid;

protected override void OnStart()
{
    DataGrid = new DataGrid 
    {
        VerticalAlignment = VerticalAlignment.Top,
        HorizontalAlignment = HorizontalAlignment.Center,
        Margin = new Thickness(0, 40, 0, 0),
        CellsPadding = "12 6",
        CellsMargin = 2,
        CellsForegroundColor = "#ffffff",
        FontSize = 12
    };

    DataGrid.SetText("Opening Price", 0, 0);
    DataGrid.SetText(Bid.ToString(), 0, 1);
    DataGrid.SetText(Bid.ToString(), 0, 2);
    DataGrid.SetText(Bid.ToString(), 0, 3);

    DataGrid.SetText("Target met", 1, 0);

    DataGrid.SetText("No", 1, 1);
    DataGrid.SetCellBackgroundColor(1, 1, Color.Green);

    DataGrid.SetText("No", 1, 2);
    DataGrid.SetCellBackgroundColor(1, 2, Color.Green);

    DataGrid.SetText("Yes", 1, 3);
    DataGrid.SetCellBackgroundColor(1, 3, Color.Red);

    Chart.AddControl(DataGrid);
}

You can change text for cells as well foreground and background colors:


 


@redtick123