简体   繁体   English

控制日期数据图表上的X轴标记c#

[英]Controlling X-Axis Marks on Date Data Charts c#

I have records in the database and a chart where I can preview them. 我在数据库中有记录,在图表中可以预览它们。 The graph is data x Voltage in volts. 该图是数据x电压(伏)。

When I have few records in the table the graph shows exactly the date recorded in the bank on the X axis, but over the time I add more records, the graph grows and stops marking all the date records where there was a change in the voltage. 当表中的记录很少时,该图会准确地显示X轴上银行中记录的日期,但是随着时间的推移,我会添加更多的记录,该图会增大并停止标记所有日期记录,其中电压发生了变化。

I am putting a picture of how the chart is: 我正在画一张图表的图片:

在此处输入图片说明

This chart is taking 50 entries from my bank with different times and voltage values. 该图表从我的银行中提取了50条不同时间和电压值的条目。

I would like a way to improve and be able to better control the X-axis points, perhaps making them appear every 10 minutes for example. 我想要一种改进并能够更好地控制X轴点的方法,例如使它们每10分钟出现一次。

Below is the code that generates the graphics: 下面是生成图形的代码:

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringSQL"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT Log.ValorEntrada, Log.DataHoraEvento, Log.NumeroEntrada FROM Log INNER JOIN Equipamento ON Log.IDEquipamento = Equipamento.IDEquipamento INNER JOIN EntradaEstado ON Equipamento.IDEquipamento = EntradaEstado.IDEquipamento INNER JOIN Entrada ON EntradaEstado.IDEntrada = Entrada.IDEntrada WHERE Log.NumeroEntrada = N'" + descricao.SelectedItem.Value + "' AND Log.ValorEntrada IS NOT NULL AND DATEDIFF(day, Log.DataHoraEvento, GETDATE()) = 1 AND EntradaEstado.IDEquipamento = N'" + equip.Text + "' AND Entrada.Descricao = N'" + descricao.SelectedItem.Text + "' ORDER BY DataHoraEvento ASC");
    cmd.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        Chart2.DataSource = dt;

        if (descricao.SelectedItem.Text == "Corrente da bomba de graxa")
        {
            Chart2.ChartAreas["ChartArea1"].AxisY.Title = "Corrente (A)";
        }
        else
        {

            Chart2.ChartAreas["ChartArea1"].AxisY.Title = "Tensão (V)";
        }

        Title title = Chart2.Titles.Add("Gráfico do dia: " + DateTime.Now.AddDays(-1).ToString("dd/MM/yyyy"));
        title.ForeColor = System.Drawing.Color.DarkBlue;
        title.Font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);




        Chart2.ChartAreas["ChartArea1"].AxisX.Title = "Hora";
        Chart2.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "HH:mm:ss";
        Chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = System.Drawing.Color.DarkGray;
        Chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
        Chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = System.Drawing.Color.DarkGray;
        Chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;


        Chart2.Series["Series1"].XValueType = ChartValueType.DateTime;
        Chart2.Series["Series1"].BorderWidth = 2;
        Chart2.Series["Series1"].XValueMember = "DataHoraEvento";
        Chart2.Series["Series1"].YValueMembers = "ValorEntrada";
        Chart2.Series["Series1"].IsXValueIndexed = true;

        Chart2.Width = 1200;
        Chart2.Height = 400;

        Chart2.DataBind();
        con.Close();

Have you tried to alter the label style on the X-axis? 您是否尝试过更改X轴上的标签样式?

There are quite a few options, perhaps something like 有很多选择,也许像

Chart2.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
Chart2.ChartAreas["ChartArea1"].AxisX.LabelStyle = new LabelStyle() { IntervalType = DateTimeIntervalType.Minutes, Interval = 10, Format = "HH:mm:ss"};

can point you in the right direction. 可以为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM