【发布时间】:2014-01-13 13:19:55
【问题描述】:
我创建了一个结合日期选择器的图表。从 SQL 数据库中获取数据,其中包含所选日期的 datetime、open、high、low、close。
我测试了格式化 xAxis 的每一种方法,以便未缩放的刻度间隔为 1 小时,例如 7:00 8:00 9:00...
我希望能够放大,以便最小间隔为 1 分钟,如 7:00 7:01 7:02...
第一个问题是,每个点的格式总是看起来像 yyyy-mm-dd hh:mm:ss。更改 tickInvterval 后,它看起来更好但仍然错误,并且无法进行 1 分钟间隔的缩放功能。 将数据库拆分为单个日期和时间列后,我解决了日期问题,但显然 highchart 仍然没有意识到它是一个时间轴,因为 xAxis 设置如下所示:(应该显示 hh:mm 但图表显示 hh:mm: ss)
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
minute: '%H:%M'},
tickInterval: 60},
至少这不是拆分数据库的意图,因为应该有一种方法可以在查询过程中将 sql datetime 更改为 javatime 格式。 我现在搜索了几天,但没有找到正确的答案......也许是因为我对这些东西太陌生...... :)
这是生成图表的代码:
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
options = {
chart:{
zoomType: 'x',
renderTo: 'container'
},
title: {
text: 'Charts',
style: {
fontFamily: 'Verdana',
fontSize: '20px',
fontWeight: 'bold'
}
},
subtitle: {
text: 'Chart 1',
style: {
fontFamily: 'Verdana',
fontSize: '15px',
fontWeight: 'bold'
}
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
minute: '%H:%M',
},
tickInterval: 60
},
yAxis: [{
labels: {
format: '{value}',
style: {
color: '#eeeeee'
}
},
title: {
text: 'Realtime',
style: {
color: '#eeeeee'
}
}
}, {
title: {
enabled: false,
text: 'Graph1',
style: {
color: '#4572A7'
}
},
labels: {
enabled: false,
format: '',
style: {
color: '#4572A7'
}
},
opposite: true
},{
title: {
enabled: false,
text: 'Graph2',
style: {
color: '#89A54E'
}
},
reversed: true,
labels: {
enabled: false,
format: '',
style: {
color: '#89A54E'
}
},
opposite: true
}],
tooltip: {
backgroundColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
stops: [
[0, 'rgba(96, 96, 96, .8)'],
[1, 'rgba(16, 16, 16, .8)']
]
},
borderWidth: 1,
borderColor: '#666666',
crosshairs: true,
shared: true,
useHTML: true,
dateTimeLabelFormats: {
minute: '%H:%M'},
style: {
padding: 10,
fontWeight: 'bold'
},
formatter: function() {
var s = '<b><span style="font-size: 12px; color: #cccccc">'+ this.x +'</span></b>';
$.each(this.points, function(i, point) {
s += '<br/><br/><span style="font-size: 16px; color: #cccccc">'+ point.series.name +': '+ point.y +'</span>';
});
return s;
},
shared: true
},
legend:
{
enabled: true,
itemStyle: {
color: '#eeeeee',
}
},
credits:
{
enabled: false
},
plotOptions: {
spline: {
lineWidth: 4,
states: {
hover: {
lineWidth: 5
}
},
marker: {
enabled: false
},
},
area: {
fillColor: {
linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1},
stops: [
[0, 'rgb(204, 204, 204)'],
[1, 'rgba(204, 204, 204,0)']
]
},
lineWidth: 1,
marker: {
enabled: false
},
shadow: false,
states: {
hover: {
lineWidth: 1
}
},
threshold: null
}
},
series: [{
name: 'Realtime',
type: 'area',
color: '#cccccc',
data: []
}, {
name: 'Graph1',
type: 'spline',
yAxis: 1,
color: '#0077cc',
enableMouseTracking: false,
data: []
}, {
name: 'Graph2',
type: 'spline',
yAxis: 2,
color: '#89A54E',
enableMouseTracking: false,
data: []
}]
}
$.getJSON('data.php', function(json) {
val1 = [];
val2 = [];
val3 = [];
$.each(json, function(key,value) {
val1.push([value[0], value[1]]);
val2.push([value[0], value[2]]);
val3.push([value[0], value[3]]);
});
options.series[0].data = json['data1'];
options.series[1].data = json['data2'];
options.series[2].data = json['data3'];
options.xAxis.categories = json['datetime'];
chart = new Highcharts.Chart(options);
});
});
$(function() {
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends,
dateFormat: "yy-mm-dd",
onSelect: function(dateText, inst) {
$.getJSON("data.php?dateParam="+dateText, function(json){
val1 = [];
val2 = [];
val3 = [];
$.each(json, function(key,value) {
val1.push([value[0], value[1]]);
val2.push([value[0], value[2]]);
val3.push([value[0], value[3]]);
});
options.series[0].data = json['data1'];
options.series[1].data = json['data2'];
options.series[2].data = json['data3'];
options.xAxis.categories = json['datetime'];
chart = new Highcharts.Chart(options);
});
}
});
});
这里是php:
<?php
session_start();
?>
<?php
define('DB_SERVER',"localhost");
define('DB_NAME',"db");
define('DB_USER',"");
define('DB_PASSWORD',"");
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db(DB_NAME, $con);
if (isset($_GET["dateParam"])) {
$sql = mysql_query("SELECT datetime, open, high, close FROM data WHERE datetime LIKE '".$_GET["dateParam"]."%'");
} else {
$sql = mysql_query("SELECT datetime, open, high, close FROM data WHERE DATE(datetime) = CURDATE()");
}
while($r = mysql_fetch_array($sql)) {
$result['datetime'][] = $r['datetime'];
$result['data1'][] = $r['open'];
$result['data2'][] = $r['close'];
$result['data3'][] = $r['high'];
}
print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
【问题讨论】:
标签: php sql datetime highcharts format