Remove blank space below and above google graph?

廉价感情. 提交于 2019-12-13 05:54:48

问题


I have used google graph on a particular page after few contents. But as the number of rows(data) in google graph increases, space between content and graph increases. I have tried to modify the height, but no result.

Here is my code -

function drawChart() {
    var data = new google.visualization.DataTable();
        data.addColumn('string', 'Projects');
        data.addColumn('number', 'Hours');
        var monthData = jQuery(".slick-active").html();
        var monthTemp = monthData.toString().split(" ");
        var month = getMonthFromString(monthTemp[0]);
        var year = monthTemp[1];
        var tag = $("#tagVal").val();
        var jsonData = jQuery.ajax({
        url: "/report/additionalProjectHours",
                type: "POST",
                dataType: "json",
                async: false,
                data: {month: month, year: year, tag:tag},
                beforeSend: function (request) {
                return request.setRequestHeader('X-CSRF-Token', $("meta[name='token']").attr('content'));
                },
                error: function () {
                //alert("ERRORRRRRR");
                }
        }).responseText;
        var options;
        var chartHeight;
        if ((jsonData.length) > 134) {

            if (jsonData.length < 200){
                 chartHeight = 250;
            }else{
                chartHeight =jsonData.length ;
            }


            var options = {
                height: chartHeight / 1.3 - 100,
                color: "#0F00FF",
                fontName: "Times-Roman",
                fontSize: 15,
                bold: true,
                series: {
                    0: {
                        visibleInLegend: false
                    }
                },
                bar: {
                    groupWidth: '20%'
                }
            };
            var data = new google.visualization.DataTable(jsonData);
            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.BarChart(document.getElementById('chart'));
            chart.draw(data, options);
        }else {
          $("#chart").html('<p style="margin-right: 70px;font-family: Times-Roman; text-align: center; padding: 20px;">No Data Available</p>');
        }
        $('defs').nextAll("g").eq(1).hide();
   }


回答1:


I expect that your problem is here:

chartHeight =jsonData.length ;

jsonData is a string, so you are setting the height of the chart to be equal to the number of characters in the string. Try setting the height based on the number of rows of data in the DataTable instead:

var data = new google.visualization.DataTable(jsonData);
options.height = data.getNumberOfRows() * <height per row> + <offset for chart header and footer>;


来源:https://stackoverflow.com/questions/32304413/remove-blank-space-below-and-above-google-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!