2017-08-11 87 views
0

我在數據庫和圖表中可以預覽它們的記錄。該圖是數據x以伏特爲單位的電壓。控制日期數據圖表上的X軸標記c#

當我在表格中有幾條記錄時,該圖形精確顯示了銀行在X軸上記錄的日期,但是隨着時間的增加,我添加了更多記錄,圖形不斷增長,並停止標記存在的所有日期記錄電壓的變化。

我把的圖表是怎樣一個畫面:

enter image description here

這個圖表正在採取50項從我的銀行有不同的時間和電壓值。

我想要一種方法來改善和能夠更好地控制X軸點,例如可能使它們每隔10分鐘出現一次。

下面是生成圖形代碼:

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(); 

回答

1

您是否嘗試過改變在X軸標籤樣式?

有相當多的選擇,也許像

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

可以爲您指出正確的方向。

+0

通過這種方式,圖表繼續顯示相同數量的條目,但現在不是顯示時間,而只是顯示一天中的日期。 –

+0

更新的答案是否有幫助? –

+0

完美!它的工作! –