Google图表-轴值重叠以及如何避免

时间:2018-07-11 15:48:48

标签: charts google-visualization

enter image description here

如何避免该轴在Google图表中重叠?我正在使用大型数据集,但不确定如何解决此问题。我在x轴上使用了大量日期。我用于图表的选项是

var options = {
    tooltip: {
        trigger: 'both',
    },
    width: 1900,
    height: 400,
    vAxis: { 'title': 'Volume' },
    crosshair: { trigger: 'both'}
};

编辑:: PHP创建容器

if( isset($db_graph_query)){
    while($row = mysqli_fetch_array($db_graph_query)) {
        $rowcount2++;
        // removed the hard coded column set and made it driven off of the array below
        // could have pulled it from the $cols array above, but there might be columns that we don't wish to show
        echo "                                <tr>";
        $colindex = 0;
        foreach( $cols as $column_name ) {
            $style = "";
            $val = $row[$column_name];
            if ( isset($column_callback)) {
                $style=$column_callback($colindex, $val);
            }
            if ($colindex == 0) {
                echo "<td style='text-align: left;  width: 1pt;'><a href='#' class='toggle' onClick='drawChart(\"$val\");'>$val</a></td>";
                $tempval = $val;
            } else {
                echo "<td $style>$val</td>";
            }
            $colindex++;
        }
        echo "</tr>";
        echo "<tr class='tablesorter-childRow'>";
            echo "<td colspan='12'>";
                echo "<div id='$tempval' style='height: 400px;'></div>";
            echo "</td>";
        echo "</tr>";
    }
}

编辑:: 绘制图表脚本,创建图表选项,从SQL DB获取数据并将其添加到数据中:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
    google.charts.load('current', {'packages':['corechart']});
    function drawChart(inputname) {
        var data = new google.visualization.DataTable();
            data.addColumn('string', 'Name');
            data.addColumn('string', 'RunDate');
            data.addColumn('number', 'Runs');
            data.addColumn('number', 'Fails');
            data.addRows([
                <?php
                    $dbName = "mydb";
                    $config = parse_ini_file("configfile.ini",true);
                    $dbUser = $config["DB"]["db_user"];
                    $dbServer = $config["DB"]["db_ip"];
                    $dbPassword = $config["DB"]["db_pass"];

                    $con = mysql_connect($dbServer, $dbUser, $dbPassword);

                    if (!$con) {
                        die('Could not connect: ' . mysql_error());
                    }

                    mysql_select_db($dbName, $con);
                    $sql = mysql_query("SELECT * From MyTestTable");

                    $output = array();

                    while($row = mysql_fetch_array($sql)) {
                        // create a temp array to hold the data
                        $temp = array();

                        // add the data
                        $temp[] = '"' . $row['Name'] . '"';
                        $temp[] = '"' . $row['RunDate'] . '"';
                        $temp[] = (int) $row['Runs'];
                        $temp[] = (int) $row['FailCount'];
                        // implode the temp array into a comma-separated list and add to the output array
                        $output[] = '[' . implode(', ', $temp) . ']';
                    }
                    // implode the output into a comma-newline separated list and echo
                    echo implode(",\n", $output);

                    mysql_close($con);
                ?>
        ]);
        var view = new google.visualization.DataView(data);
        view.setRows(data.getFilteredRows([
            {column: 0, value: inputname}
        ]));
        view.setColumns([1,2,3]);

            var options = {
                tooltip: {
                    trigger: 'both',
                },
                vAxis: { 'title': 'Volume' },
                crosshair: { trigger: 'both'},
                width: 1900,
                height: 400
            };

        var chart = new google.visualization.LineChart(document.getElementById(inputname));
        chart.draw(view, options);
    }
</script>

2 个答案:

答案 0 :(得分:2)

尝试使用倾斜文本选项...

hAxis: {slantedText: true}

答案 1 :(得分:0)

水平轴刻度线完全不应该重叠,并且我相信可能发生的唯一方法是在绘制时图表不可见,在这种情况下,图表认为刻度线没有用可以占用任何空间,并且可以尽可能密集地打包。直到达到最小间距约束时,倾斜或交替不一定会有所帮助。

因此,目前唯一真正的解决方案是确保在绘制图表时将其可见。