我正在用Steema TeeChart建立一个烛台图。我有一个数据类:
public class Candles
{
public DateTime date { get; set; }
public double open { get; set; }
public double high { get; set; }
public double low { get; set; }
public double close { get; set; }
public Candles (long date, double open, double high, double low, double close)
{
this.date = epoch2string(date/1000000);
this.open = open;
this.high = high;
this.low = low;
this.close = close;
}
public Candles(int date)
{
this.date = epoch2string(date);
}
private DateTime epoch2string(long epoch) {
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epoch);
}
}
我正在用循环添加数据:
foreach (var item in candleList) {
chartStyle.Add (item.open, item.high, item.low, item.close);
}
但是我应该如何添加我的类的"日期"值作为X轴上的标签?
为每个蜡烛添加日期作为DateTime值将自动执行此操作,例如:
Steema.TeeChart.Styles.Candle candle1 = new Steema.TeeChart.Styles.Candle(tChart1.Chart);
DateTime date = DateTime.Now;
Random tmp = new Random();
for (int i = 0; i < 10; i++)
{
double open = tmp.Next();
double high = tmp.Next();
double low = tmp.Next();
double close = tmp.Next();
candle1.Add(date.AddDays(i), open, high, low, close);
}
在你的例子中应该是:
foreach (var item in candleList) {
chartStyle.Add (item.date, item.open, item.high, item.low, item.close);
}
根据某些图表设置,这可能不是自动的,在这种情况下,您可以强制:
candle1.XValues.DateTime = true;
tChart1.Axes.Bottom.Labels.Style = AxisLabelStyle.Value;
tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MMM";
基于我的示例代码。
如果你想添加日期作为文本标签并删除周末间隙,你可以使用RemoveGaps属性,或者在TeeChart for . net的功能演示中显示的All FeaturesWelcome !Chart stylesFinancialCandle (OHLC)Axis labels no Weekends示例。我在这里发布了一个基于上面提到的演示的例子