0
votes

I am using Teechart for Xamarin forms. I want to be able to size the candles of my chart when the chart has been zoomed so the aren't big spaces between them. There was an issue with the .Zoomed event which wasn't triggering but the Steema team was really fast at fixing it after my report. Now there is another issue - when I use the code below, I am getting an exception - tChart1.Axes.Bottom.Maximum and tChart1.Axes.Bottom.Minimum aren't returning a proper value for the candles.

public class App : Application
{
    public double tChartMax;
    public double tChartMin;
    public Steema.TeeChart.Chart tChart1;
    public Steema.TeeChart.Styles.Candle tSeries;

    double widthRatio;
    int origCandleWidth;


    public App ()
    {
        var tChart1 = new Steema.TeeChart.Chart ();

        var tSeries = new Steema.TeeChart.Styles.Candle ();
        tSeries.FillSampleValues (30);
        tChart1.Series.Add (tSeries);
        tChart1.Aspect.View3D = false;

        ChartView chartView = new ChartView {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,

            WidthRequest = 400,
            HeightRequest = 500
        };

        tChart1.UndoneZoom += (object sender, EventArgs e) => {
            tSeries.Color = Color.Black;
            //double range = tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum;
        };

        tChart1.Zoomed += (object sender, EventArgs e) => {
            tSeries.Color = Color.Pink;
            tChartMax = tChart1.Axes.Bottom.Maximum;
            tChartMin = tChart1.Axes.Bottom.Minimum;
            double range = tChartMax - tChartMin;
        };


        tChart1.Zoomed += tChart1_Zoomed;
        tChart1.UndoneZoom += tChart1_UndoneZoom;
        tChart1.BeforeDrawSeries += tChart1_BeforeDrawSeries;

        origCandleWidth = tSeries.CandleWidth;
        widthRatio = (tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum) / origCandleWidth;


        chartView.Model = tChart1;

        MainPage = new ContentPage {
            Content = new StackLayout {
                Children = {
                    chartView,
                }
            },
        };
    }

    bool zoomed = false;
    void tChart1_UndoneZoom(object sender, EventArgs e)
    {
        zoomed = true;
    }

    void tChart1_Zoomed(object sender, EventArgs e)
    {
        zoomed = true;
    }

    void tChart1_BeforeDrawSeries(object sender, Graphics3D g)
    {
        if(zoomed)
        {
            double range = tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum;
            double tmpRatio = range / origCandleWidth;

            if (widthRatio > 0 && widthRatio != tmpRatio)
            {
                tSeries.CandleWidth = Utils.Round((widthRatio / tmpRatio) * origCandleWidth);
            }
            else
            {
                tSeries.CandleWidth = origCandleWidth;
            }
        }
        zoomed = false;
    }
}
1
Since series hasn't been painted at that stage, axes values may not have been initialized and therefore don't have valid values. Have you tried using that code on TChart.AfterDraw event?Narcís Calvet
The behavior is similar, but I am starting to think that even if I make this work, this is not the proper way to control the size of the candles. Does teechart provide any method in which I the candle width increases with the zoom level so that there aren't big empty gaps of space when you zoom in?Ilia Stoilov
The easiest option I can think of is setting tChart1.Touch.Style = TouchStyle.FullChart;. More info can be found at teechart.net/docs/teechart/net/libpcl/html/…Narcís Calvet

1 Answers

0
votes

The answer for this question is contained in the comment which Narcis Calvet made:

The easiest option I can think of is setting tChart1.Touch.Style = TouchStyle.FullChart;. More info can be found at http://www.teechart.net/docs/teechart/net/libpcl/html/SteemaTeeChartTouchStyle.htm