Page 1 of 1

Labels on CandleBar series issue on zoom

Posted: Thu May 24, 2012 3:02 pm
by 10046738
I have an issue where the bottom axis label is not being displayed correctly when chart is zoomed and candle style is CandleBar. I am trying to only display month-year on the bottom axis. The GetAxisLabel method works correctly when chart is unzoomed, but when zoomed GetAxisLabel is not called for every e.ValueIndex. If I change the candle style to Line, then the GetAxisLabel method is called for each e.ValueIndex.

How do I get the GetAxisLabel method to be called for each e.ValueIndex when the candle style is CandleBar?

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Steema.TeeChart.TChart chart = new Steema.TeeChart.TChart();

        public Form1()
        {
            InitializeComponent();

            chart.Parent = this;
            chart.Dock = DockStyle.Fill;
            chart.GetAxisLabel += new Steema.TeeChart.GetAxisLabelEventHandler(chart_GetAxisLabel);
            chart.Panel.MarginBottom = 10;
            chart.Legend.Visible = false;
            chart.Aspect.View3D = false;


            Panel panel = new Panel();
            panel.Height = 50;
            panel.Parent = this;
            panel.Dock = DockStyle.Top;
            Button btnChange = new Button();
            btnChange.Click += new EventHandler(btnChange_Click);
            btnChange.Parent = panel;
            btnChange.Top = 10;
            btnChange.Left = 10;
            btnChange.Text = "Change Type";

            FillChart(chart);

        }

        void chart_GetAxisLabel(object sender, Steema.TeeChart.GetAxisLabelEventArgs e)
        {
            if (e.ValueIndex != -1)
            {
                DateTime lDate = DateTime.FromOADate(e.Series[e.ValueIndex].X);
                if (e.ValueIndex == 0)
                    e.LabelText = "";
                else
                {
                    DateTime lPrevDate = DateTime.FromOADate(e.Series[e.ValueIndex].X - 1);
                    if (lPrevDate.Month != lDate.Month)
                        e.LabelText = lDate.Month.ToString() + "-" + lDate.Year.ToString();
                    else
                        e.LabelText = "";

                }
            }
        }

        void btnChange_Click(object sender, EventArgs e)
        {
            Steema.TeeChart.Styles.Candle series = (Steema.TeeChart.Styles.Candle)chart.Series[0];
            if (series.Style == Steema.TeeChart.Styles.CandleStyles.CandleBar)
                series.Style = Steema.TeeChart.Styles.CandleStyles.Line;
            else
                series.Style = Steema.TeeChart.Styles.CandleStyles.CandleBar;

        }


        void FillChart(Steema.TeeChart.TChart chart)
        {
            chart.Series.Clear();
            Steema.TeeChart.Styles.Candle lCandle = new Steema.TeeChart.Styles.Candle(chart.Chart);
            lCandle.Style = Steema.TeeChart.Styles.CandleStyles.CandleBar;
            lCandle.UpCloseColor = Color.Black;
            lCandle.DownCloseColor = Color.Black;
            lCandle.FillSampleValues(10000);
            lCandle.XValues.DateTime = false;
            lCandle.Visible = true;
            lCandle.Title = "OHLC";

            for (int i = 0; i < lCandle.Count; i++)
            {
                lCandle[i].Label = i.ToString();
            }

            chart.Refresh();
            chart.Axes.Bottom.Labels.Style = Steema.TeeChart.AxisLabelStyle.Text;
            chart.Axes.Bottom.Labels.Separation = 0;
            chart.Axes.Bottom.Labels.Angle = 90;
        }


    }
}

Re: Labels on CandleBar series issue on zoom

Posted: Fri May 25, 2012 10:16 am
by 10050769
Hello Voodoo,

GetAxisLabel isn't the best way to achieve as your code works correctly with DateTime values and CandleSerie. I have modified your code to work without GetAxisLabel Event and works fine for me:

Code: Select all

 Steema.TeeChart.TChart chart = new Steema.TeeChart.TChart();
        public Form1()
        {
            InitializeComponent();

            chart.Parent = this;
            chart.Dock = DockStyle.Fill;
          
            chart.Panel.MarginBottom = 10;
            chart.Legend.Visible = false;
            chart.Aspect.View3D = false;
            System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
            panel.Height = 50;
            panel.Parent = this;
            panel.Dock = DockStyle.Top;
            Button btnChange = new Button();
            btnChange.Click += new EventHandler(btnChange_Click);
            btnChange.Parent = panel;
            btnChange.Top = 10;
            btnChange.Left = 10;
            btnChange.Text = "Change Type";
            tChart1.Visible = false;
            FillChart(chart);
            tChart1.Draw();

        }
 
        void btnChange_Click(object sender, EventArgs e)
        {
            Steema.TeeChart.Styles.Candle series = (Steema.TeeChart.Styles.Candle)chart.Series[0];
            if (series.Style == Steema.TeeChart.Styles.CandleStyles.CandleBar)
                series.Style = Steema.TeeChart.Styles.CandleStyles.Line;
            else
                series.Style = Steema.TeeChart.Styles.CandleStyles.CandleBar;

        }

        void FillChart(Steema.TeeChart.TChart chart)
        {
            chart.Series.Clear();
            Steema.TeeChart.Styles.Candle lCandle = new Steema.TeeChart.Styles.Candle(chart.Chart);
            lCandle.Style = Steema.TeeChart.Styles.CandleStyles.CandleBar;
            lCandle.UpCloseColor = Color.Black;
            lCandle.DownCloseColor = Color.Black;
            lCandle.FillSampleValues(1000);
            lCandle.XValues.DateTime = true;
            lCandle.Visible = true;
            lCandle.Title = "OHLC";
            chart.Axes.Bottom.Labels.Angle = 90;
            chart.Axes.Bottom.Labels.DateTimeFormat = "MM - yyyy";
            chart.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(DateTimeSteps.OneMonth); 
        }
Can you tell us if previous code works as you expect?

I hope will helps.

Thanks,

Re: Labels on CandleBar series issue on zoom

Posted: Fri May 25, 2012 2:49 pm
by 10046738
Sandra,
Thank you for your response. Your example worked for the data I provided.

In the actual program my data does not have Saturday/Sunday data, so I use an index as my X value. Also, I have a radio group box that lets user chose labels by Month, Week, Quarter, Year, so I need something more dynamic.

I did get the GetAixsLabel method to work as expected once I figured out how to use the X value to map to a DateTime and then format that DateTime to the appropriate string. See the code attached. (This code will not work with the above code. I am including it for reference.) I'm still not sure why the GetAxisLabel method was not being called for each e.ValueIndex, but that issue has gone away.

Code: Select all

private void chrtMain_GetAxisLabel(object sender, Steema.TeeChart.GetAxisLabelEventArgs e)
        {
            List<Int32> lQuarters = new List<int> { 1, 4, 7, 10 };

            if (e.Series == chrtMain.Series[0] && sender == chrtMain.Series[0].GetHorizAxis && e.ValueIndex != -1)
            {
                DateTime lDate =MyUtils.XToDate[(int)e.Series[e.ValueIndex].X];
                if (e.ValueIndex == 0)
                    e.LabelText = "";
                else
                {
                    DateTime lPrevDate = MyUtils.XToDate[(int)e.Series[e.ValueIndex - 1].X];

                    if (FLabelType == ChartLabels.Types.Year)
                    {

                        if (lPrevDate.Year != lDate.Year)
                            e.LabelText = lDate.Year.ToString();
                        else
                            e.LabelText = "";
                    }
                    else if (FLabelType == ChartLabels.Types.Quarter)
                    {
                        if (lPrevDate.Month != lDate.Month && lQuarters.Contains(lDate.Month))
                            e.LabelText = lDate.ToShortMonthName() + "-" + lDate.Year.ToString();
                        else
                            e.LabelText = "";
                    }
                    else if (FLabelType == ChartLabels.Types.Month)
                    {
                        if (lPrevDate.Month != lDate.Month)
                            e.LabelText = lDate.ToShortMonthName() + "-" + lDate.Year.ToString();
                        else
                            e.LabelText = "";
                    }
                    else if (FLabelType == ChartLabels.Types.Week)
                    {
                        if (lPrevDate.DayOfWeek > lDate.DayOfWeek)
                        {
                            while (lDate.DayOfWeek != DayOfWeek.Monday)
                                lDate = lDate.AddDays(-1);
                            e.LabelText = lDate.ToString("MM/dd/yyyy");
                        }
                        else
                            e.LabelText = "";
                    }
                }
            }
        }

Re: Labels on CandleBar series issue on zoom

Posted: Mon May 28, 2012 12:09 pm
by 10050769
Hello Voodoo,

Ok, I have made a simple example where I am using a four RadioButtons and change the DateTimeFormat in these, but I don't use GetAxisLabels to achieve a good result:

Code: Select all

      Steema.TeeChart.TChart chart = new Steema.TeeChart.TChart();
        public Form1()
        {
            InitializeComponent();

            chart.Parent = this;
            chart.Dock = DockStyle.Fill;

            chart.Panel.MarginBottom = 10;
            chart.Legend.Visible = false;
            chart.Aspect.View3D = false;
            System.Windows.Forms.Panel panel = new System.Windows.Forms.Panel();
            panel.Height = 50;
            panel.Parent = this;
            panel.Dock = DockStyle.Top;
            Button btnChange = new Button();
            btnChange.Parent = panel;
            btnChange.Top = 10;
            btnChange.Left = 10;
            btnChange.Text = "Change Type";
            tChart1.Visible = false;
            FillChart(chart);
            chart.Draw();
         }

        void FillChart(Steema.TeeChart.TChart chart)
        {
            chart.Series.Clear();
            Steema.TeeChart.Styles.Candle lCandle = new Steema.TeeChart.Styles.Candle(chart.Chart);
            lCandle.Style = Steema.TeeChart.Styles.CandleStyles.CandleBar;
            lCandle.UpCloseColor = Color.Black;
            lCandle.DownCloseColor = Color.Black;
     
			double tmpOpen;
			double tmpClose;
			int count = 0;
			DateTime dt = DateTime.Now;
            Random r = new Random();
			for (int t=0;t<1000;t++) 
			{
				tmpOpen = r.Next(100);
				tmpClose = tmpOpen - r.Next(100);
				if(dt.DayOfWeek != DayOfWeek.Saturday & dt.DayOfWeek != DayOfWeek.Sunday) 
				{
					++count;
					lCandle.Add(dt,tmpOpen,tmpOpen + r.Next(50),tmpClose -r.Next(50),tmpClose);
				}
				dt = dt.AddDays(1);
			}

            lCandle.XValues.DateTime = true;
            lCandle.Visible = true;
            lCandle.Title = "OHLC";
            chart.Axes.Bottom.Labels.Angle = 90;
            chart.Axes.Bottom.Labels.DateTimeFormat = "MM - yyyy";
           
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                chart.Axes.Bottom.Labels.DateTimeFormat = "yyyy";
                chart.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(DateTimeSteps.OneYear);
             }
         }

     

        private void radioButton2_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
              chart.Axes.Bottom.Labels.DateTimeFormat = "MMMM";
              chart.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(DateTimeSteps.FourMonths);
             
            }
        }

        private void radioButton3_Click(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                chart.Axes.Bottom.Labels.DateTimeFormat = "MMMM";
                chart.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(DateTimeSteps.OneMonth);
            }
        }

        private void radioButton4_Click(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                chart.Axes.Bottom.Labels.DateTimeFormat = "dddd";
                chart.Axes.Bottom.Increment = Steema.TeeChart.Utils.GetDateTimeStep(DateTimeSteps.OneWeek);
                chart.Axes.Bottom.Labels.Style = AxisLabelStyle.PointValue;
            }
            
        }
Moreover, if you want other way to achieve that weekends don't appear in the Axis, I suggest you take a look in Demo of TeeChart .Net, concretely example All Features\Welcome !\Chart styles\Financial\Candle (OHLC)\Axis Labels no Weekends or if you prefer you can use custom labels as explain in this link. If my suggestion still doesn't help you, please send me your project so we can try to find a good solution for you.

Thank,