Search code examples
xamarin.formsteechart

Teechart Axes.Bottom.Minimum and Maximum not working for candles?


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;
    }
}

Solution

  • 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