用开源绘图控件ZedGraph画K线。主要是其中类JapaneseCandleStick的应用。
效果图如下:
核心代码:
private void CreateZedGraph(ZedGraphControl zgc)
{
zgc.Controls.Clear();
GraphPane myPane = new GraphPane(); // (GraphPane)pane[0];
zgc.GraphPane=myPane ;
// Set the title and axis labels
myPane.Title.Text = "K线图";
myPane.XAxis.Title.Text = "日期";
myPane.XAxis.Title.FontSpec.FontColor = Color.Black;
myPane.YAxis.Title.Text = "价格";
myPane.YAxis.Title.FontSpec.FontColor = Color.Black;
//Get Data
StockPointList spl = new StockPointList();
Random rand = new Random();
// First day is jan 1st
XDate xDate = new XDate(2006, 1, 1);
double open = 50.0;
for (int i = 0; i < 50; i++)
{
double x = xDate.XLDate;
double close = open + rand.NextDouble() * 10.0 - 5.0;
double hi = Math.Max(open, close) + rand.NextDouble() * 5.0;
double low = Math.Min(open, close) - rand.NextDouble() * 5.0;
StockPt pt = new StockPt(x, hi, low, open, close, 100000);
spl.Add(pt);
open = close;
// Advance one day
xDate.AddDays(1.0);
// but skip the weekends
if (XDate.XLDateToDayOfWeek(xDate.XLDate) == 6)
xDate.AddDays(2.0);
}
//添加栅格线
//myPane.XAxis.MajorGrid.IsVisible = true;
//myPane.YAxis.MajorGrid.IsVisible = true;
//myPane.XAxis.MajorGrid.Color = Color.LightGray;
//myPane.YAxis.MajorGrid.Color = Color.LightGray;
//myPane.YAxis.MajorGrid.DashOff = 0;
//myPane.XAxis.MajorGrid.DashOff = 0;
myPane.XAxis.Type = AxisType.Date;
myPane.XAxis.Scale.Format = "MM-dd";
myPane.XAxis.Scale.FontSpec.Angle = 45;//X轴文字方向,0-90度
//开始Y轴坐标设置
设置Y轴坐标的范围
//myPane.YAxis.Scale.Max = Math.Round(maxhi * 1.2, 2);//Math.Ceiling(maxhi);
//myPane.YAxis.Scale.Min = Math.Round(minlow * 0.8, 2);
//Y轴最大刻度,注意minStep只会显示刻度线不会显示刻度值
myPane.YAxis.Scale.MajorStep = 0.01;
myPane.XAxis.Scale.FontSpec.FontColor = Color.Black;
myPane.YAxis.Scale.FontSpec.FontColor = Color.Black;
myPane.XAxis.Type = AxisType.DateAsOrdinal;
//myPane.Legend.FontSpec.Size = 18f;
//myPane.Legend.Position = LegendPos.InsideTopRight;
//myPane.Legend.Location = new Location(0.5f, 0.6f, CoordType.PaneFraction,
// AlignH.Right, AlignV.Top);
JapaneseCandleStickItem myCurve = myPane.AddJapaneseCandleStick("", spl);
myCurve.Stick.IsAutoSize = true;
//myCurve.Stick.Color = Color.Blue;
myCurve.Stick.FallingFill = new Fill(Color.Green); //下跌填充色
myCurve.Stick.RisingFill = new Fill(Color.Red); //上涨填充色:条形块颜色
myCurve.Stick.FallingBorder = new Border(Color.Green, 1); //下跌边框和上下影线的颜色
myCurve.Stick.RisingBorder = new Border(Color.Red, 1); //上涨边框和上下影线的颜色
// pretty it up a little
//myPane.Chart.Fill = new Fill(Color.LightBlue, Color.LightGoldenrodYellow, 135.0f);
//myPane.Fill = new Fill(Color.Orange, Color.FromArgb(220, 220, 255), 45.0f);
Color c1 = ColorTranslator.FromHtml("#ffffff");
Color c2 = ColorTranslator.FromHtml("#ffd693");
myPane.Chart.Fill = new Fill(c1);//图形区域颜色
myPane.Fill = new Fill(c2);//整体颜色
myPane.AxisChange();
zgc.Refresh(); // 没有此句则不显示K线图像
}