TeeChart的X轴为时间,多个Y轴的显示

TeeChart的X轴为时间,多个Y轴的显示TeeChart的X轴为时间,多个Y轴的显示

最后上代码

public partial class Test : Form
{
private TChart tChart = new TChart();
private int space = ; public Test()
{
InitializeComponent();
Init();
} /// <summary>
/// 添加若干个自定义坐标轴
/// </summary>
/// <param name="count"></param>
private void AddCustomAxis(int count)
{
List<BaseLine> listBaseLine = new List<BaseLine>();
for (int i = ; i < tChart.Series.Count; i++)
{
listBaseLine.Add((BaseLine)tChart.Series[i]);
} double single = ( - space * (count + )) / (count + );//单个坐标轴的百分比
tChart.Axes.Left.StartPosition = space;
tChart.Axes.Left.EndPosition = tChart.Axes.Left.EndPosition = tChart.Axes.Left.StartPosition + single;
tChart.Axes.Left.StartEndPositionUnits = PositionUnits.Percent;
listBaseLine[].CustomVertAxis = tChart.Axes.Left; double startPosition = tChart.Axes.Left.StartPosition;
double endPosition = tChart.Axes.Left.EndPosition;
Axis axis;
for (int i = ; i < count; i++)
{
axis = new Axis();
startPosition = endPosition + space;
endPosition = startPosition + single;
axis.StartPosition = startPosition;
axis.EndPosition = endPosition;
tChart.Axes.Custom.Add(axis);
listBaseLine[i + ].CustomVertAxis = axis;
}
} private void Init()
{
tChart.Dock = DockStyle.Fill;
tChart.Aspect.View3D = false;
tChart.Legend.LegendStyle = LegendStyles.Series;
tChart.Axes.Bottom.Labels.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
tChart.Axes.Bottom.Labels.ExactDateTime = true;
tChart.Axes.Bottom.Labels.Angle = ;
tChart.Axes.Bottom.Minimum = * Utils.GetDateTimeStep(DateTimeSteps.OneSecond);
tChart.Axes.Bottom.Minimum = * Utils.GetDateTimeStep(DateTimeSteps.OneSecond);
panel2.Controls.Add(tChart); DataSet data = HSQueryHistoryCurve.Instance.Search(new HSQueryCondition()
{
ChannelId = new List<int>() { ,,,},
DeviceId = "",
StartTime = DateTime.Now.AddMonths(-).ToString("yyyy-MM-dd HH:mm:ss"),
EndTime = DateTime.Now.AddMonths().ToString("yyyy-MM-dd HH:mm:ss"),
ProjectId =
}); DateTime time = DateTime.Now;
for (int j = ; j < data.Tables.Count; j++)
{
DataTable dataTable = data.Tables[j];
Line line = new Line();
tChart.Series.Add(line);
line.Title = string.Format("曲线{0}", j + );
line.XValues.DateTime = true;
for (int i = ; i < dataTable.Rows.Count; i++)
{
line.Add(time.AddSeconds(i).ToOADate(), (double)dataTable.Rows[i][]);
}
} AddCustomAxis();
}
}

ps:data是一个数据集,包含若干个数据表

每一个数据表对应一条曲线的数据,第一列为时间,第二列为数据。

这里的时间,直接从Datetime.Now重新处理的,仅仅使用了数据表中的第二列数据

需要注意的是:

必须使用控件本身的纵轴Left,纵轴的刻度才能正常显示。

如果多个纵轴,全部设置为自定义的。会导致无法正常显示。

获取所有可见曲线,添加到一个集合中返回

private List<BaseLine> GetVisibleSeries()
{
List<BaseLine> visibleSeries = new List<BaseLine>();
for (int i = ; i < zbmChart.Series.Count; i++)
{
zbmChart.Series[i].CustomVertAxis = null;
if (zbmChart.Series[i].Visible)
{
visibleSeries.Add((BaseLine)zbmChart.Series[i]);
}
}
return visibleSeries;
}

添加自定义纵轴

private void AddVisibleLineVerticalAxis()
{
List<BaseLine> visibleSeries = GetVisibleSeries(); zbmChart.ClearCustomAxis();//清除所有自定义的坐标轴 double singleAxisLengthPercent;//单个纵轴占据的百分比 //计算每个坐标轴占据的百分比
if (visibleSeries.Count < )
{
return;
}
else
{
singleAxisLengthPercent = Convert.ToDouble( - verticalAxisSpace * (visibleSeries.Count + )) / (visibleSeries.Count);
} //给可见的曲线加上纵轴
for (int i = ; i < visibleSeries.Count; i++)
{
Series s = visibleSeries[i];
Axis axis; //设置纵轴的起始位置
if (i == )
{
axis = zbmChart.zbmYAxis;
axis.StartPosition = verticalAxisSpace;
}
else
{
axis = new Axis(false, false, zbmChart.Chart);
if (i == )
{
axis.StartPosition = zbmChart.zbmYAxis.EndPosition + verticalAxisSpace;
}
else
{
axis.StartPosition = visibleSeries[i - ].CustomVertAxis.EndPosition + verticalAxisSpace;
}
}
//设置纵轴的结束位置
axis.EndPosition = axis.StartPosition + singleAxisLengthPercent;
//设置纵轴刻度的颜色
axis.Labels.Font.Color = dicColor[s.Tag.ToString()]; //设置网格的可见性以及颜色
axis.Grid.Visible = VisibleSettings.Default.Grid;
axis.Grid.Color = ColorSettings.Default.Grid; if (i == )
{
//曲线本身的纵轴,无需额外处理
}
else
{
//将自定义纵轴加入图表
zbmChart.AddCustomAxis(axis);
//将纵轴和对应的曲线关联
s.CustomVertAxis = axis;
}
}
}
上一篇:还在用 kill -9 停机?这才是最优雅的姿势(转)


下一篇:深入浅出数据结构C语言版(2)——简要讨论算法的时间复杂度