Hi Christopher,
Have a Look on following code:
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;
using Steema.TeeChart;
using Steema.TeeChart.Styles;
namespace TeeChartExample
{
public partial class Form1 : Form
{
Candle series = new Candle();
int totalSampleValues = 1000;
int displayValues = 100;
int pageNumber;
int scrollValue;
int previousMaxMargin = 0, previousMinMargin = 0;
int currentMaxMargin = 0, currentMinMargin = 0;
int previousMinimum = 0, previousMaximum = 0;
double previousLeftMax=0, previousLeftMin = 0;
private void InitializeComponent()
{
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.tChart1 = new Steema.TeeChart.TChart();
this.SuspendLayout();
// hScrollBar1
this.hScrollBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.hScrollBar1.Location = new System.Drawing.Point(0, 424);
this.hScrollBar1.Maximum = 1000;
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(722, 16);
this.hScrollBar1.TabIndex = 0;
// tChart1
this.tChart1.Aspect.View3D = false;
this.tChart1.Axes.Bottom.Grid.Visible = false;
this.tChart1.Axes.Left.Visible = false;
this.tChart1.Axes.Right.Grid.Visible = false;
this.tChart1.Axes.Top.Grid.Visible = false;
this.tChart1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tChart1.Header.Lines = new string[] { "" };
this.tChart1.Legend.Visible = false;
this.tChart1.Location = new System.Drawing.Point(0, 0);
this.tChart1.Name = "tChart1";
this.tChart1.Panel.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
this.tChart1.Panel.Brush.Gradient.Visible = false;
this.tChart1.Panel.MarginBottom = 1;
this.tChart1.Panel.MarginLeft = 1;
this.tChart1.Panel.MarginRight = 1;
this.tChart1.Panel.MarginTop = 1;
this.tChart1.Size = new System.Drawing.Size(722, 424);
this.tChart1.TabIndex = 1;
this.tChart1.Walls.Back.Pen.Visible = false;
this.tChart1.Walls.Back.Visible = false;
this.tChart1.Walls.Bottom.Pen.Visible = false;
this.tChart1.Walls.Bottom.Visible = false;
this.tChart1.Walls.Left.Pen.Visible = false;
this.tChart1.Walls.Left.Visible = false;
this.tChart1.Walls.Visible = false;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(722, 440);
this.Controls.Add(this.tChart1);
this.Controls.Add(this.hScrollBar1);
this.Name = "Scrolling and Paging Example";
this.Text = "Form1";
this.ResumeLayout(false);
}
public Form1()
{
InitializeComponent();
InitializeChart();
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
hScrollBar1.Value = hScrollBar1.Maximum-1;
hScrollBar1_Scroll(sender, new ScrollEventArgs(ScrollEventType.Last, hScrollBar1.Maximum));
tChart1.Draw();
}
private void InitializeChart()
{
//Event
tChart1.AfterDraw += new PaintChartEventHandler(tChart1_AfterDraw);
tChart1.DoubleClick += new EventHandler(tChart1_DoubleClick);
//Initialize Series
series.DownCloseColor = Color.Black;
series.VertAxis = VerticalAxis.Both;
tChart1.Aspect.View3D = false;
tChart1.Series.Add(series);
series.FillSampleValues(totalSampleValues);
//Set Margin
tChart1.Axes.Left.MaximumOffset = 30;
tChart1.Axes.Left.MinimumOffset = 30;
//Enable Paging
pageNumber = Convert.ToInt32(totalSampleValues / displayValues);
tChart1.Page.MaxPointsPerPage = this.displayValues;
tChart1.Page.Current = this.pageNumber;
tChart1.Page.AutoScale = true;
tChart1.Draw();
//Enable Scroliing
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = totalSampleValues - displayValues - 1;
previousMinMargin = totalSampleValues - displayValues - 1;
previousMaxMargin = totalSampleValues - 1;
hScrollBar1.Scroll += new ScrollEventHandler(hScrollBar1_Scroll);
//Custom Bottom Axis
tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd";
tChart1.Axes.Bottom.Increment = Utils.GetDateTimeStep(DateTimeSteps.OneWeek);
tChart1.Panel.MarginBottom = 10;
}
public void SetMinMax(int minimum, int maximum)
{
try
{
this.scrollValue = hScrollBar1.Value;
this.tChart1.Axes.Bottom.SetMinMax(this.tChart1[0].XValues[minimum], this.tChart1[0].XValues[maximum]);
this.tChart1.Axes.Bottom.AdjustMaxMin();
currentMinMargin = minimum;
currentMaxMargin = maximum;
if (currentMinMargin < previousMinMargin - displayValues)
{
this.tChart1.Page.Previous();
pageNumber = pageNumber - 1;
this.tChart1.Page.Current = pageNumber;
previousMinMargin = currentMinMargin;
previousMaxMargin = currentMaxMargin;
}
else if (currentMaxMargin > previousMaxMargin + displayValues)
{
this.tChart1.Page.Next();
pageNumber = pageNumber + 1;
this.tChart1.Page.Current = pageNumber;
previousMinMargin = currentMinMargin;
previousMaxMargin = currentMaxMargin;
}
this.tChart1.Invalidate();
}
catch (Exception ex)
{
}
}
void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
SetMinMax(e.NewValue-displayValues, e.NewValue);
}
void tChart1_DoubleClick(object sender, EventArgs e)
{
this.tChart1.ShowEditor();
}
void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
{
Axis bottom = tChart1.Axes.Bottom;
int fontHeight = 10;
Rectangle rect = Utils.FromLTRB(bottom.IStartPos, bottom.Position, bottom.IEndPos, bottom.Position + fontHeight * 2);
g.Pen.Color = Color.Red;
g.Brush.Visible = false;
g.Rectangle(rect);
DateTime minDate = DateTime.FromOADate(bottom.Minimum);
DateTime maxDate = DateTime.FromOADate(bottom.Maximum);
for (DateTime date = minDate; date.Date <= maxDate; date = date.AddDays(1))
{
if (date.Day == 1)
{
DateTime tmpDate = new DateTime(date.Year, date.Month, date.Day);
int tmpX = bottom.CalcPosValue(tmpDate.ToOADate());
int tmpY = rect.Bottom + rect.Height;
g.VerticalLine(tmpX, rect.Top, tmpY);
g.Font.Color = Color.Red;
g.TextOut(tmpX, tmpY - fontHeight, tmpDate.ToString("MMMM"));
}
}
//For Scrolling and AutoScale
previousLeftMin = this.tChart1[0].YValues[currentMinMargin];
previousLeftMax = previousLeftMin;
for (int count = currentMinMargin; count < currentMaxMargin; count++)
{
if (this.tChart1[0].YValues[count] < previousLeftMin)
{
previousLeftMin = this.tChart1[0].YValues[count];
}
else if (this.tChart1[0].YValues[count] > previousLeftMax)
{
previousLeftMax = this.tChart1[0].YValues[count];
}
}
this.tChart1.Axes.Left.SetMinMax(previousLeftMin, previousLeftMax);
this.tChart1.Axes.Right.SetMinMax(previousLeftMin, previousLeftMax);
}
}
}
We achieved following in above code:
1] Scrolling Using External Control (like HScrollBar)
2] Paging with Autoscale=True
What we needed now:
1] Zooming Candle Width according to TotalSampleValues, DisplayValues and Chart Size (i.e. Width & Height)
Note: TotalSampleValues=Total No. of Sample Values Plotted on the Chart
DisplayValues= Max Points Per Page
2] Zooming with Paging
For example after zooming, it should automatically calculate new DisplayValues and new No. of Pages, and work accordingly.
3] One of the major issue we are having right now is Candle Chart showing blank spaces for saturday and Sunday (or Trading Holidays). Instead of skipping this holidays it shows us blank spaces, which causes TeeChart .Net to show wrong chart. If we used your given workaround of assigning labels, it may have problem while setting Bottom Axis Min Max.