2014-01-26 49 views
1

我试图在我的网站的管理员端绘制一组Google Analytics图表。我正在使用javascript api获取数据并使用Flot绘制图表。谷歌分析:在X轴上绘制完整日期的一周数据和在Y轴上的访问

我的问题是,我必须显示Y轴访问(数字)与X轴每年的一周。

我可以得到图表,但我需要标签以月/日/年格式显示日期,但要按星期计算点数。

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

请帮我看看。

我现在的谷歌Analytics(分析)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

我得到它的工作。

我改变了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.

相关问题