javascript - 将 Google 可视化折线图 hAxis origin 设置为其初始值

标签 javascript google-visualization linechart

这是一个sample fiddle使用谷歌折线图绘制图表,

 var Xmin = data.getValue(0, 0);

        var options = {
                title : 'Sample graph',
                legend : {
                    position : 'bottom'
                },
                height : 400,
                interpolateNulls : true,
                'pointSize' : 5,
                'vAxis' : {
                    title : "Count",
                    'minValue' : 0,
                },
                'hAxis' : {
                    title : "Month",
                    'minValue' : Xmin,
                },
                'animation' : {
                    'duration' : 1000,
                    'easing' : 'in'
                },
            };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }

如何将 hAxis 原点设置为 Jan-13 而不是 0

最佳答案

如果要将线条拉伸(stretch)到图表的边缘,则需要为域轴使用连续数据类型(numberdatedatetime, timeofday) 而不是离散的 (string) 类型。由于您的数据是月份和年份,您可以使用 date 类型:

function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Month', 'Sales', 'Expenses'],
        [new Date(2013, 0), 1000, 400],
        [new Date(2013, 1), 1170, 460],
        [new Date(2013, 2), 660, 1120],
        [new Date(2013, 3), 1030, 540]
    ]);

    // get the range of dates
    var range = data.getColumnRange(0);
    // pull back the start just a bit so the first axis label will show
    range.min = new Date(range.min);
    range.min.setMilliseconds(-1);

    // format the dates
    var dateFormatter = new google.visualization.DateFormat({pattern: 'MMM-yy'});
    dateFormatter.format(data, 0);

    var options = {
        title : 'Sample graph',
        legend : {
            position : 'bottom'
        },
        height : 400,
        interpolateNulls : true,
        pointSize : 5,
        vAxis : {
            title : "Count",
            minValue : 0,
        },
        hAxis : {
            title : "Month",
            format: 'MMM-yy',
            viewWindow: {
                min: range.min,
                max: range.max
            }
        },
        animation : {
            duration : 1000,
            easing : 'in'
        },
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}
google.load("visualization", "1", {packages:["corechart"], callback: drawChart});

参见示例:http://jsfiddle.net/asgallant/k3c9Q/

关于javascript - 将 Google 可视化折线图 hAxis origin 设置为其初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23193064/

相关文章:

javascript - 'app.router' 已弃用!在简单的网站上

javascript - HTML,CSS,JS 日历添加

javascript - Google 时间轴重叠时间表

javascript - 谷歌图表中的层次结构图

swift - Swift 2 中的 x 轴上没有显示所有标签

javascript - prevObject 是什么?为什么我的选择器返回它?

javascript - 谷歌甘特图高度检测

python - 如何在绘图/图表上显示 yticks(而不是在 y 轴上)。显示点附近的 yticks

对象的 Javafx 线图

javascript - WebGL 几何着色器等效?