0

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();
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    You're doing `times = new DateTime[500]`, but not filling that array fully, which results in passing junk data to the graph. You'll need to either prune your `time` (and `temp`) array to only contain the data you actually read from the database, or better yet, use a `List` and only add those items that you actually read to that list – MindSwipe May 20 '22 at 07:43
  • Or even better, use Dapper or Entity Framework, then this code becomes about 2 lines long, and is all nicely strongly typed objects in lists (well, the db access bit at least) - the "have an array of string first names, an array of string last names, an array of int weights, and array of datetime birthdates... and relate them all by position so person 1's details are index 0 in every array" is a very C-style way of programming. In C#, which is OO, you have a class Person that holds all these details and a sole array/list of Person – Caius Jard May 20 '22 at 08:32

0 Answers0