Google 图表 X 轴看起来不太好

Google charts X-axis don't look so good

我使用 google 图表 API 绘制图表。我得到这张图表:

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

我认为问题是因为 X 列类型是字符串而不是 DATETIME。 我如何更改 google 图表中的列类型? 或者如何在不更改列类型的情况下更改 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);

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);

HTML代码:

<div id="curve_chart" class="plot" style="width: 50% ; height: 400px ; float:left; margin-left:9%;"></div>

谢谢!

您可以强制条形图的 x 轴设置 hAxis.viewWindow.minhAxis.viewWindow.max:

hAxis: {
    viewWindow: {
        min: 0,
        max: 100
    },
    ticks: [0, 50, 100] // display labels every 50
}

这将适用于数字或百分比,与日期一起使用有点复杂:检查 Google API 中的 Customizing Axes,有两种解决方案:

  • 您可以将 主要 X 轴 更改为 离散 和日期类型

The major axis of a chart can be either discrete or continuous. When using a discrete axis, the data points of each series are evenly spaced across the axis, according to their row index. When using a continuous axis, the data points are positioned according to their domain value.

  • 阅读以 Help! My chart has gone wonky! 开头的 Customizing Axes link 部分,其中解释了如何从 date 更改为 string 然后自定义的步骤它...

我找到了答案。

我分享给大家。所以如果有人卡住了,比如我。将很快继续使用此帮助。

我创建了新变量:var showEvery = parseInt(data.getNumberOfRows() / 4);

并在选项属性中添加:hAxis: {showTextEvery: showEvery}

所以JS代码是这样的:

    var data = google.visualization.arrayToDataTable(<?php echo $str?>);

    var showEvery = parseInt(data.getNumberOfRows() / 4);
    
    var options = {
        title: '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);