Google Analytic:绘制一年中的数据,在x轴上显示完整日期,在y轴上绘制访问

时间:2014-01-26 05:12:06

标签: javascript graph google-analytics flot

我正在尝试在我网站的管理员端绘制一组Google Analytics图表。我使用javascript api获取数据并使用Flot绘制图形。

我的问题是我必须显示y轴访问次数(数字)与x轴一年中的一周。

我可以绘制图表但我需要标签以月/日/年格式显示日期,但是以星期为单位绘制点。

如果我按日期查询,我会得到每天不需要的数据。如果我按周查询,我就无法获得完整的日期。

请帮我查询。

我当前的Google AnalyticsAPI API调用:

// Use the Analytics Service Object to query the Core Reporting API
gapi.client.analytics.data.ga.get({
    'ids': 'ga:' + profileId,
    'dimensions':'ga:year,ga:month,ga:week',
    'start-date': '2013-03-03',
    'end-date': '2014-01-23',
    'metrics': 'ga:visits'
}).execute(handleCoreReportingResults);

我的Flot.js代码:

var coordinated =[["2013", "03", "10", "0"], ["2013", "03", "11", "0"], ["2013", "03", "12", "0"], ["2013", "03", "13", "0"], ["2013", "03", "14", "0"], ["2013", "04", "14", "0"], ["2013", "04", "15", "0"], ["2013", "04", "16", "0"], ["2013", "04", "17", "0"], ["2013", "04", "18", "0"], ["2013", "05", "18", "0"], ["2013", "05", "19", "0"], ["2013", "05", "20", "0"], ["2013", "05", "21", "0"], ["2013", "05", "22", "0"], ["2013", "06", "22", "0"], ["2013", "06", "23", "0"], ["2013", "06", "24", "0"], ["2013", "06", "25", "0"], ["2013", "06", "26", "0"], ["2013", "06", "27", "0"], ["2013", "07", "27", "0"], ["2013", "07", "28", "0"], ["2013", "07", "29", "0"], ["2013", "07", "30", "0"], ["2013", "07", "31", "29"], ["2013", "08", "31", "247"], ["2013", "08", "32", "647"], ["2013", "08", "33", "609"], ["2013", "08", "34", "589"], ["2013", "08", "35", "617"], ["2013", "09", "36", "697"], ["2013", "09", "37", "793"], ["2013", "09", "38", "665"], ["2013", "09", "39", "673"], ["2013", "09", "40", "214"], ["2013", "10", "40", "504"], ["2013", "10", "41", "648"], ["2013", "10", "42", "544"], ["2013", "10", "43", "564"], ["2013", "10", "44", "389"], ["2013", "11", "44", "170"], ["2013", "11", "45", "510"], ["2013", "11", "46", "558"], ["2013", "11", "47", "548"], ["2013", "11", "48", "580"], ["2013", "12", "49", "513"], ["2013", "12", "50", "533"], ["2013", "12", "51", "531"], ["2013", "12", "52", "677"], ["2013", "12", "53", "296"], ["2014", "01", "01", "386"], ["2014", "01", "02", "634"], ["2014", "01", "03", "633"], ["2014", "01", "04", "498"]];
var sin = []

for (var i = 0; i < coordinated.length; i++) {
    sin.push([ (new Date(coordinated[i][0]+"/"+coordinated[i][2]+'/'+coordinated[i][2]).getTime()), coordinated[i][3]]);
}

var plot = $.plot($(".chart"),
       [ { data: sin, label: "Visits"} ], {
           series: {
               lines: { show: true },
               points: { show: true }
           },
           grid: { hoverable: true, clickable: true },
           //yaxis: { min: 0, max: 1000 },
           xaxis: { 
            //min:(new Date(sin[0][3])).getTime(), max: (new Date(sin[sin.length-1][4])).getTime(), 
            mode: "time", timeformat: "%m/%d/%y", 
            minTickSize: [1, "day"], 
            tickSize: [1, "month"] },

         });

此代码后的视图:

The view is all wrong

我需要帮助。我真的很感激!

1 个答案:

答案 0 :(得分:1)

我得到了它的工作。

我将for循环更改为

for (var i = 0; i < coordinated.length; i++) {
    var myDate = new Date(coordinated[i][0],0,1);
    var newDate = myDate.getDate() + (coordinated[i][2] * 7);
    myDate.setDate(newDate);
    sin.push([myDate.getTime(), coordinated[i][3]]);
}

现在它有效。

我得到并想要的输出:

Correct output

Got help from this link.