最后上代码
public partial class Test : Form { private TChart tChart = new TChart(); private int space = 3; public Test() { InitializeComponent(); Init(); } /// <summary> /// 添加若干个自定义坐标轴 /// </summary> /// <param name="count"></param> private void AddCustomAxis(int count) { List<BaseLine> listBaseLine = new List<BaseLine>(); for (int i = 0; i < tChart.Series.Count; i++) { listBaseLine.Add((BaseLine)tChart.Series[i]); } double single = (100 - space * (count + 2)) / (count + 1);//单个坐标轴的百分比 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[0].CustomVertAxis = tChart.Axes.Left; double startPosition = tChart.Axes.Left.StartPosition; double endPosition = tChart.Axes.Left.EndPosition; Axis axis; for (int i = 0; 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 + 1].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 = 90; tChart.Axes.Bottom.Minimum = 12 * Utils.GetDateTimeStep(DateTimeSteps.OneSecond); tChart.Axes.Bottom.Minimum = 60 * Utils.GetDateTimeStep(DateTimeSteps.OneSecond); panel2.Controls.Add(tChart); DataSet data = HSQueryHistoryCurve.Instance.Search(new HSQueryCondition() { ChannelId = new List<int>() { 1,2,3,4}, DeviceId = "112233445566", StartTime = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd HH:mm:ss"), EndTime = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss"), ProjectId = 1 }); DateTime time = DateTime.Now; for (int j = 0; j < data.Tables.Count; j++) { DataTable dataTable = data.Tables[j]; Line line = new Line(); tChart.Series.Add(line); line.Title = string.Format("曲线{0}", j + 1); line.XValues.DateTime = true; for (int i = 0; i < dataTable.Rows.Count; i++) { line.Add(time.AddSeconds(i).ToOADate(), (double)dataTable.Rows[i][1]); } } AddCustomAxis(3); } }
ps:data是一个数据集,包含若干个数据表
每一个数据表对应一条曲线的数据,第一列为时间,第二列为数据。
这里的时间,直接从Datetime.Now重新处理的,仅仅使用了数据表中的第二列数据
时间: 2024-11-03 22:35:37