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


    最后上代码

    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重新处理的,仅仅使用了数据表中的第二列数据

    需要注意的是:

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

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

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

    private List<BaseLine> GetVisibleSeries()
            {
                List<BaseLine> visibleSeries = new List<BaseLine>();
                for (int i = 0; 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 < 1)
                {
                    return;
                }
                else
                {
                    singleAxisLengthPercent = Convert.ToDouble(100 - verticalAxisSpace * (visibleSeries.Count + 1)) / (visibleSeries.Count);
                }
    
                //给可见的曲线加上纵轴
                for (int i = 0; i < visibleSeries.Count; i++)
                {
                    Series s = visibleSeries[i];
                    Axis axis;
    
                    //设置纵轴的起始位置
                    if (i == 0)
                    {
                        axis = zbmChart.zbmYAxis;
                        axis.StartPosition = verticalAxisSpace;
                    }
                    else
                    {
                        axis = new Axis(false, false, zbmChart.Chart);
                        if (i == 1)
                        {
                            axis.StartPosition = zbmChart.zbmYAxis.EndPosition + verticalAxisSpace;
                        }
                        else
                        {
                            axis.StartPosition = visibleSeries[i - 1].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 == 0)
                    {
                        //曲线本身的纵轴,无需额外处理
                    }
                    else
                    {
                        //将自定义纵轴加入图表
                        zbmChart.AddCustomAxis(axis);
                        //将纵轴和对应的曲线关联
                        s.CustomVertAxis = axis;
                    }
                }
            }
  • 相关阅读:
    fms服务器端呼叫客户端
    Linux C取整的方法
    fms客户端呼叫服务器端
    Tree Control DataProviders
    IE7下的css style display的兼容处理
    Maxthon,TheWorld,MyIE等多标签浏览器的Flash缓存问题
    Android深入浅出系列之实例应用—弹出消息Toast对象的使用纯文本方式(一)
    C#温故而知新学习系列之XML编程—Xml读取器XmlReader类(二)
    一步一个脚印学习WCF系列之WCF概要—生成元数据与代理(五)
    Eclipse 常用快捷键(转)
  • 原文地址:https://www.cnblogs.com/chucklu/p/4513954.html
Copyright © 2020-2023  润新知