使用Google Analytics原始数据按月显示图表

时间:2016-07-06 15:02:46

标签: javascript php google-analytics chart.js

我做了什么:

我已使用API​​查询并通过PHP从Google Analytics获取json格式的数据。我使用chartjs.org来显示图表。它正如其文档中所描述的那样正常工作。

以下是上述结果: graph from cartjs

GA管理员:

但在GA管理员中,结果按月分组。这是因为如果时间跨度增加,则图表的x轴标签将变得拥挤。如何实现下图并按月分组数据?

goal graph

我的js数据:

var salesChartData = {
    labels: ["2016 Jun, 11","2016 Jun, 12","2016 Jun, 13","2016 Jun, 14","2016 Jun, 15","2016 Jun, 16","2016 Jun, 17","2016 Jun, 18","2016 Jun, 19","2016 Jun, 20","2016 Jun, 21","2016 Jun, 22","2016 Jun, 23","2016 Jun, 24","2016 Jun, 25","2016 Jun, 26","2016 Jun, 27","2016 Jun, 28","2016 Jun, 29","2016 Jun, 30","2016 Jul, 01","2016 Jul, 02","2016 Jul, 03"],
    datasets: [
      {
        label: "Electronics",
        fillColor: "rgba(5, 141, 199,.1)",
        strokeColor: "#058dc7",
        pointColor: "#058dc7",
        pointStrokeColor: "#058dc7",
        pointHighlightFill: "#0371a0",
        pointHighlightStroke: "#0371a0",
        data: ["18","4","39","25","19","23","19","11","7","21","14","19","24","29","6","8","29","29","15","18","13","3","4"],

      }
    ]
  };

上述代码需要更改哪些内容才能实现GA等图表?

1 个答案:

答案 0 :(得分:0)

我发现,最新版本的图表js可以选择showxLable来完成这项工作但不完全不需要。它只是显示不对数据进行分组的标签数量。

[demo] demo