 |
|
c# สอบถามเกี่ยวกับการสร้างกราฟแบบเรียลไทม์โดยดึงข้อมูลจากMySQLครับ |
|
 |
|
|
 |
 |
|
ผมอยากสอบถามเกี่ยวกับโค้ดทำกราฟแบบเรียลไทม์หน่อยครับ คือตอนนี้ผมทำกราฟโดยให้แสดงข้อมูลล่าสุดในดาต้าเบสเรื่อยๆครับ
แต่ตอนนี้ผมติดตรงที่ว่า ค่าแรกในกราฟยังอยู่ ทำให้เมื่อเรียกข้อมูลในดาต้าเบสเข้ามา กราฟก็ยังโชว์ทุกค่าที่ถูกเรียกเข้ามาอยู่
คืออยากให้ในกราฟโชว์ข้อมูลแค่ 10ค่า ถ้าค่าที่11เข้ามาก็ให้ค่าที่1เลื่อนหายไปอ่ะครับ

Code (C#)
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO.Ports;
using System.Data.OleDb;
using System.Threading;
namespace AlarmSystem2
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
}
private void LoadDatabase()
{
string sql = "SELECT Pilar1, Pilar2, Pilar3, TIME FROM `testschema`.`testtable` ORDER BY ID desc LIMIT 1";
MySqlConnection con = new MySqlConnection("host=localhost;user=root;password=Settha15;database=testschema");
MySqlCommand cmd = new MySqlCommand(sql, con);
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
chartP1.Series["Pilar1"].Points.AddXY(reader.GetString("TIME"), reader.GetString("Pilar1"));
chartP2.Series["Pilar2"].Points.AddXY(reader.GetString("TIME"), reader.GetString("Pilar2"));
chartP3.Series["Pilar3"].Points.AddXY(reader.GetString("TIME"), reader.GetString("Pilar3"));
}
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
LoadDatabase();
}
}
}
Tag : .NET, MySQL, Win (Windows App), C#, Windows
|
ประวัติการแก้ไข 2017-02-17 09:20:50
|
 |
 |
 |
 |
Date :
2017-02-17 01:51:58 |
By :
natthapol_helk |
View :
1567 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้ลอง Clear กราฟเดิมดูหรือยังครับ
|
 |
 |
 |
 |
Date :
2017-02-17 09:26:33 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอ.....................ปกติเรา select เข้าไป ยัดในกราฟ ไม่ใช่รึครับ รึว่าไม่ได้ดึงมาจากที่ select ไว้
รึไม่ก็ลอง เอา รูปกราฟ กับ โค้ด มาดูครับ
|
 |
 |
 |
 |
Date :
2017-02-17 12:17:13 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|