简体   繁体   中英

Google Analytic: Plotting week of year data with full date on x-axis and visits in y-axis

I'm trying to draw a set of Google Analytics graph in the admin side of my site. I am using javascript api to get the data and using Flot to draw the graphs.

My problem is that I have to show y-axis visits (numbers) vs x-axis week of year.

I can get the graph to plot but I need label to show date in Month/day/year format but the dots to be plot in terms of weeks.

If I query by date, I'd get data of each day which I don't need. If I query by week, I can't get the full date.

Please help me with the query.

My current Google Analytics API call:

// 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);

My Flot.js code:

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 after this code:

观点全错了

I need help. I'd really appreciate it!

I got it to work.

I changed the for loop to

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]]);
}

And now it works.

Output that I got and wanted:

正确的输出

Got help from this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM