I have created a MySQL database which receives data from an arduino, now what I want to do is place that data into a nice chart in Winforms using ScottPlot. It does this correctly except for the fact that it keeps placing the starting point in the year 1900 temperature 0.

The data in the table looks like this:

temperature time
-------------------------------
11          2022-05-10 15:05:21
11          2022-05-10 15:04:66
10          2022-05-10 15:03:44
11          2022-05-10 15:02:51

My code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ScottPlot;
using MySql.Data.MySqlClient;

namespace data_visualization
{
    public partial class Form1 : Form
    {
        MySqlConnection con = new MySqlConnection(@"Data Source=localhost;Initial Catalog=goblins;User ID=root;Password=àààà");
        MySqlCommand cmd = new MySqlCommand();
        MySqlDataReader dr;

        DateTime[] times = new DateTime[500];

        double[] temp = new double[500];

        int counter;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            counter = 0;
            //connect sql
            con.Open();
            cmd.Connection = con;

            // Query
            cmd.CommandText = "SELECT temperature, time FROM goblin";
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                temp[counter] = Convert.ToDouble(dr["temperature"]);
                times[counter] = Convert.ToDateTime(dr["time"]);
                counter = counter + 1;
            }

            con.Close();

            double[] time = times.Select(x => x.ToOADate()).ToArray();

            formsPlot1.Plot.XAxis.DateTimeFormat(true);
            formsPlot1.Plot.AddScatter(time, temp);

            formsPlot1.Refresh();
        }
    }
}
0

There are 0 answers