I'd like to create an Annotation Chart using pool temperature data from the database. You can take a look at the database structure here on sqlfiddle or here on rextester, but to save you the click, here's the structure I'm working with:
DROP TABLE IF EXISTS `temperatures`;
DROP TABLE IF EXISTS `pools`;
CREATE TABLE `pools` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `temperatures` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pool_id` int(10) unsigned NOT NULL,
`temperature` double(8,1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `temperatures_pool_id_foreign` (`pool_id`),
CONSTRAINT `temperatures_pool_id_foreign` FOREIGN KEY (`pool_id`) REFERENCES `pools` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3173 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `pools` (`id`, `name`, `created_at`)
VALUES
(1,'Pool #1','2017-04-08 22:48:03'),
(2,'Pool #2','2017-04-08 22:48:03'),
(3,'Pool #3','2017-04-08 22:48:03');
INSERT INTO `temperatures` (`id`, `pool_id`, `temperature`, `created_at`)
VALUES
(31,1,100.1,'2017-04-09 02:44:56'),
(32,2,104.2,'2017-04-09 02:44:56'),
(33,3,97.0,'2017-04-09 02:44:56'),
(34,1,100.1,'2017-04-09 03:00:04'),
(35,2,98.4,'2017-04-09 03:00:04'),
(36,3,96.6,'2017-04-09 03:00:04'),
(37,1,100.1,'2017-04-09 03:37:13'),
(38,2,101.8,'2017-04-09 03:37:13'),
(39,3,96.4,'2017-04-09 03:37:13'),
(40,1,100.1,'2017-04-09 04:00:04'),
(41,2,101.8,'2017-04-09 04:00:04'),
(42,3,96.5,'2017-04-09 04:00:04'),
(43,1,100.1,'2017-04-09 05:00:04'),
(44,2,101.8,'2017-04-09 05:00:04');
Okay, so essentially, I'm created a controller that will return properly formatted JSON for use with ajax and google.visualization.DataTable(), like this:
var jsonData = $.ajax({
url: "/data/pool-temperature-timeline",
dataType: "json",
async: false
}).responseText;
data = new google.visualization.DataTable(jsonData);
chart.draw(data, options);
Of course, looking at the documentation, the annotation chart is expecting things to follow this format:
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Kepler-22b mission');
data.addColumn('string', 'Kepler title');
data.addColumn('string', 'Kepler text');
data.addColumn('number', 'Gliese 163 mission');
data.addColumn('string', 'Gliese title');
data.addColumn('string', 'Gliese text');
data.addRows([
[new Date(2314, 2, 15), 12400, undefined, undefined,
10645, undefined, undefined],
[new Date(2314, 2, 16), 24045, 'Lalibertines', 'First encounter',
12374, undefined, undefined],
[new Date(2314, 2, 17), 35022, 'Lalibertines', 'They are very tall',
15766, 'Gallantors', 'First Encounter'],
[new Date(2314, 2, 18), 12284, 'Lalibertines', 'Attack on our crew!',
34334, 'Gallantors', 'Statement of shared principles'],
[new Date(2314, 2, 19), 8476, 'Lalibertines', 'Heavy casualties',
66467, 'Gallantors', 'Mysteries revealed'],
[new Date(2314, 2, 20), 0, 'Lalibertines', 'All crew lost',
79463, 'Gallantors', 'Omniscience achieved']
]);
var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
Right, so that's the setup, now comes the question. What's the best way to organize the data so that 1.) there is always temperature data for pool 1, 2, AND 3 for the same datetime (I worry the dataset might not be complete for the given timestamp)? Should I organize it starting at the SQL layer by using some clever query? Or do I organize it in the controller by using a bunch of foreach loops? This is the goal I'm striving for:
$dataTable->addRow(['created_at',
'temperature1', 'title1', 'text1',
'temperature2', 'title2', 'text2',
'temperature2', 'title2', 'text2',
]);
I could see the clever query would be a nice way to go to avoid doing a bunch of logic and foreach loops in the controller. Maybe if the data was organized in the columns, like:
created_at, pool_1_temperature, pool_2_temperature, pool_3_temperature
------------------------------------------------
2017-04-09 02:44:56, 100.1, 104.2, 97.0
2017-04-09 03:00:04, 100.1, 98.4, 96.6
2017-04-09 03:37:13, 100.1, 101.8, 96.4
Then I could pretty easily go through that and create the DataTable. I'm not sure how to do this in MySQL though or even if it's a good idea.
Thanks for taking the time thus far and thanks in advance for any help. I hope I was clear enough.
PS. I guess the closest thing I've come across so far is Mysql query to dynamically convert rows to columns. I'm going to play around with this some more...