Ann*_*aya 1 html javascript google-visualization pygooglechart web
我使用谷歌图表API绘制图表.我得到这个图表:

我在这个图表中的问题是X轴.标签看起来不那么好.如果我在X轴上有更多的字符串,它看起来像这样:

我认为问题是因为X列类型是字符串而不是DATETIME.我如何更改谷歌图表中的列类型?或者我如何在不改变列类型的情况下更改X轴?我在下面添加脚本......
PHP代码:
$connection = mysql_connect('127.0.0.1','root','123456');
mysql_select_db('db_statmarket',$connection);
$result2 = mysql_query('select sum(`How much read from customer`) as Leads, Date from monitor group by Date;',$connection) or die('cannot show tables');
$json = array();
while($row = mysql_fetch_assoc($result2)) {
$json[] = $row;
}
$str='[\'Date\', \'Leads\'],';
foreach($json as $key=>$value){
$str = $str.'['.'\''.$value["Date"].'\''.', '.$value["Leads"].'],';
}
$str = substr($str, 0, -1);
$str = '['.$str.']';
$result1 = mysql_query('SELECT * FROM monitor ORDER BY customer_id DESC LIMIT 1;',$connection) or die('cannot show tables');
$row = mysql_fetch_assoc($result1);
Run Code Online (Sandbox Code Playgroud)
JS代码:
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(<?php echo $str?>);
var options = {
title: '',
curveType: 'function',
legend: { position: 'bottom' },
width: 1000,
backgroundColor: '#efeff0',
is3D: true,
chartArea: {
backgroundColor: {
stroke: '#efeff1',
strokeWidth: 1}},
height: 300
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
Run Code Online (Sandbox Code Playgroud)
HTML代码:
<div id="curve_chart" class="plot" style="width: 50% ; height: 400px ; float:left; margin-left:9%;"></div>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我找到了答案.
我为所有人分享它.所以,如果有人像我一样卡住了.将继续快速提供此帮助.
我创建了新的变量: var showEvery = parseInt(data.getNumberOfRows() / 4);
在选项属性中我添加: hAxis: {showTextEvery: showEvery}
所以JS代码看起来像这样:
var data = google.visualization.arrayToDataTable(<?php echo $str?>);
var show = parseInt(data.getNumberOfRows() / 4);
var options = {
title: '',
curveType: 'function',
legend: { position: 'bottom' },
width: 1000,
backgroundColor: '#efeff0',
is3D: true,
chartArea: {
backgroundColor: {
stroke: '#efeff1',
strokeWidth: 1}},
hAxis: {showTextEvery: showEvery},
height: 300
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
Run Code Online (Sandbox Code Playgroud)