我试图将mysql查询传递到google折线图中,但是在将其转换为正确的格式时遇到了一些问题。这个图表显示了计算机内存利用率的百分比。
mysql查询:
SELECT DATE_FORMAT(date, 'new Date(%Y, %c, %d, %H, %i, %s)') AS date, percentComittedInUse, machineName FROM stsdata.hyperv_perf_memory
表格结果(简称):
+-----------------------------------+----------------------+-------------+
| date | percentComittedInUse | machineName |
+-----------------------------------+----------------------+-------------+
| new Date(2018, 8, 17, 17, 09, 03) | 53 | STSWKS15 |
| new Date(2018, 8, 17, 17, 32, 43) | 57 | STSWKS15 |
| new Date(2018, 8, 17, 18, 02, 53) | 55 | STSWKS15 |
| new Date(2018, 8, 17, 18, 33, 00) | 56 | STSWKS15 |
| new Date(2018, 8, 17, 19, 03, 05) | 57 | STSWKS15 |
| new Date(2018, 8, 20, 14, 02, 30) | 57 | STSWKS15 |
| new Date(2018, 8, 20, 14, 32, 46) | 57 | STSWKS15 |
| new Date(2018, 8, 20, 15, 02, 56) | 59 | STSWKS15 |
| new Date(2018, 8, 20, 15, 33, 08) | 60 | STSWKS15 |
| new Date(2018, 8, 17, 17, 32, 43) | 50 | FAKE_VHOST8 |
| new Date(2018, 8, 17, 18, 02, 53) | 48 | FAKE_VHOST8 |
| new Date(2018, 8, 17, 18, 33, 00) | 49 | FAKE_VHOST8 |
| new Date(2018, 8, 17, 19, 03, 05) | 50 | FAKE_VHOST8 |
| new Date(2018, 8, 17, 19, 33, 27) | 49 | FAKE_VHOST8 |
| new Date(2018, 8, 17, 20, 02, 33) | 49 | FAKE_VHOST8 |
| new Date(2018, 8, 17, 20, 32, 41) | 50 | FAKE_VHOST8 |
+-----------------------------------+----------------------+-------------+
我使用php在结果中呼应:
<?php
$row = mysqli_fetch_assoc($VMMemoryPerf);
if ($VMMemoryPerf->num_rows > 0) {
while ($row = mysqli_fetch_assoc($VMMemoryPerf)) {
echo "[".$row['date'].", '".$row['machineName']."', '".$row['percentComittedInUse']."'],";
}
}
?>
结果如预期,它将返回: [new Date(2018, 8, 17, 17, 32, 43), 'STSWKS15', '57'], [new Date(2018, 8, 17, 18, 02, 53), 'STSWKS15', '55'],
但我有多个计算机名,并希望在图表中的每一行。
我在探索一种在mysql中操作数据的方法,但是如果我必须在php中这样做,那也没关系。
图表的javascript:
<?php include($_SERVER['DOCUMENT_ROOT']. "/inc/SQL_stsdata.php"); ?>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(chart_line_hyperv_memory);
function chart_line_hyperv_memory() {
var data = google.visualization.arrayToDataTable([
[{ label: 'Date', type: 'datetime' }, { role: 'annotationText' }, { label: 'Memory % Comitted', type: 'number' }],
<?php
$row = mysqli_fetch_assoc($VMMemoryPerf);
if ($VMMemoryPerf->num_rows > 0) {
while ($row = mysqli_fetch_assoc($VMMemoryPerf)) {
echo "[".$row['date'].", '".$row['machineName']."', '".$row['percentComittedInUse']."'],";
}
}
?>
]);
var options = {
title: 'VM Memory Utilization', fontSize: 10, fontName: 'Consolas',
tooltip: { textStyle: { bold: false, color: '#000000', fontSize: 12 }, showColorCode: true, isHtml: true, ignoreBounds: false, text: 'both', trigger: 'hover', padding: '0px' },
animation: { duration: 1000, startup: true, easing: 'inAndOut' },
legend: { position: 'right' },
chartArea: { height: '95%' },
width: '100%', height: '100%',
annotations: { textStyle: { fontName: 'Consolas', color: '#000000', }, highContrast: 'true', alwaysOutside: 'true', },
};
var chart = document.getElementById('chart_line_hyperv_memory'), x = (parseFloat(chart.getAttribute('data-x')) || 0), y = (parseFloat(chart.getAttribute('data-y')) || 0);
var chartContainer = $(chart).parent();
chart.style.width = chartContainer.width() + 'px';
chart.style.height = chartContainer.height() + 'px';
x += chartContainer.left;
y += chartContainer.top;
chart.style.webkitTransform = chart.style.transform = 'translate(' + x + 'px,' + y + 'px)';
chart.setAttribute('data-x', x);
chart.setAttribute('data-y', y);
var chart = new google.visualization.LineChart(document.getElementById('chart_line_hyperv_memory'));
console.log(data);
chart.draw(data, options);
}
</script>
谢谢你的评论和帮助!
更新:图表需要一个表输出,例如:
+-------------------------------------+----------+-------------+
| date | STSWKS15 | FAKE_VHOST8 |
+-------------------------------------+----------+-------------+
| Date(year, date, day, hour, minute) | value | value |
| Date(year, date, day, hour, minute) | value | value |
| Date(year, date, day, hour, minute) | value | value |
| Date(year, date, day, hour, minute) | value | value |
| Date(year, date, day, hour, minute) | value | value |
+-------------------------------------+----------+-------------+
因此它可以构造如下数据 [Date(), value, value],
我对mysql中的连接没有问题,只是查询将每台机器的结果翻转到它们自己的列中。
请记住,如果将新机器添加到表中,查询可以动态地获取它。
1条答案
按热度按时间i86rm4rw1#
根据您在更新中添加的预期输出,我认为您基本上是在寻找pivot表。
这将返回如下内容:
这假设您事先知道您的机器名。如果没有,你可能需要更多的工作。
编辑:回复评论。
如果要动态获取列名,可以使用这样的动态查询。创建存储过程并调用以获取数据。
要从中获取数据,只需执行如下查询:
希望有帮助!!如果你有任何疑问,尽管问。