如何在 Google 折线图中按年份分隔日期

2024-03-04

我有一份清单dates/values我喜欢显示在谷歌图表:折线图 https://developers.google.com/chart/interactive/docs/gallery/linechart.

    data.addRows([
        [new Date("2011-02-01T15:18:21+00:00"), 21.77],
        [new Date("2011-03-01T15:18:59+00:00"), 20.96],
        //[...],
        [new Date("2011-12-01T07:54:15+00:00"), 17.04],
        [new Date("2012-01-01T10:29:00+00:00"), 20.43],
        [new Date("2012-02-01T08:03:00+00:00"), 22.51],
        [new Date("2012-03-01T08:07:00+00:00"), 26.75],
        //[...],
        [new Date("2013-01-02T16:16:00+00:00"), 20.7],
        [new Date("2013-02-03T13:51:00+00:00"), 24.41],
        [new Date("2013-03-01T08:06:00+00:00"), 25.44],
        //[...],
        [new Date("2014-01-01T10:19:00+00:00"), 16.24],
        [new Date("2014-02-01T10:16:00+00:00"), 19.13],
        [new Date("2014-03-01T08:05:00+00:00"), 17.68],
        [new Date("2014-04-01T08:11:00+00:00"), 10.97]
        //[...],

这工作正常,但我喜欢按年份分隔值,例如每年一行来比较它们。我怎样才能设置x-axis没有年份的范围?

这是result http://jsfiddle.net/tee48gye/我喜欢实现这一目标,只有当我假装所有年份都一样时,这才有效。


我相信您想要一个离散轴。然而,每个日期都是唯一的,因此结果是每个数据点都是轴上的离散点。因此,诀窍是强制使用连续轴并通过格式进行修复。因此,您可以通过创建一个第一列是数字月份的视图来完成此操作。通过将 0-11 个月转换为刻度值并强制每一个刻度来修复格式。现在您可以保留日期的原始值。

顺便说一句,这就是我制作几乎所有图表的方式:1)使用数据表对行进行排序2)使用数据视图过滤行和列并生成导数值。 3) 使用选项来完成和/或抽象信息的表示。

jsFiddle 上的工作示例 http://jsfiddle.net/pnLv0umz/

<div>
<div id='testchart'></div>
<script type="text/javascript">
function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Date');
    data.addColumn('number', '2011');
    data.addColumn('number', '2012');
    data.addColumn('number', '2013');
    data.addColumn('number', '2014');

    data.addRows([
        [new Date("2011-02-01T15:18:21+00:00"), 21.77, null, null, null],
        [new Date("2011-03-01T15:18:59+00:00"), 20.96, null, null, null],
        [new Date("2011-04-01T15:19:22+00:00"), 15.75, null, null, null],
        [new Date("2011-05-01T15:19:44+00:00"), 6.92, null, null, null],
        [new Date("2011-06-01T08:12:00+00:00"), 4.46, null, null, null],
        [new Date("2011-07-06T07:54:00+00:00"), 2.54, null, null, null],
        [new Date("2011-08-01T15:30:21+00:00"), 2.96, null, null, null],
        [new Date("2011-09-01T15:30:35+00:00"), 2.94, null, null, null],
        [new Date("2011-10-01T15:30:58+00:00"), 3.3, null, null, null],
        [new Date("2011-11-01T15:31:37+00:00"), 10.72, null, null, null],
        [new Date("2011-12-01T07:54:15+00:00"), 17.04, null, null, null],
        [new Date("2012-01-01T10:29:00+00:00"), null, 20.43, null, null],
        [new Date("2012-02-01T08:03:00+00:00"), null, 22.51, null, null],
        [new Date("2012-03-01T08:07:00+00:00"), null, 26.75, null, null],
        [new Date("2012-04-01T12:58:04+00:00"), null, 13.09, null, null],
        [new Date("2012-05-01T12:58:59+00:00"), null, 13.52, null, null],
        [new Date("2012-06-01T08:09:00+00:00"), null, 4.71, null, null],
        [new Date("2012-07-01T13:00:06+00:00"), null, 3.92, null, null],
        [new Date("2012-08-01T08:02:00+00:00"), null, 2.85, null, null],
        [new Date("2012-09-01T09:52:18+00:00"), null, 3.05, null, null],
        [new Date("2012-10-01T17:13:00+00:00"), null, 4.2, null, null],
        [new Date("2012-11-01T10:03:00+00:00"), null, 10.79, null, null],
        [new Date("2012-12-01T10:52:00+00:00"), null, 16.68, null, null],
        [new Date("2013-01-02T16:16:00+00:00"), null, null, 20.7, null],
        [new Date("2013-02-03T13:51:00+00:00"), null, null, 24.41, null],
        [new Date("2013-03-01T08:06:00+00:00"), null, null, 25.44, null],
        [new Date("2013-04-01T09:58:00+00:00"), null, null, 23.15, null],
        [new Date("2013-05-01T20:26:00+00:00"), null, null, 12.13, null],
        [new Date("2013-06-01T13:19:00+00:00"), null, null, 6.77, null],
        [new Date("2013-07-01T18:09:00+00:00"), null, null, 4.26, null],
        [new Date("2013-08-23T14:32:00+00:00"), null, null, 2.67, null],
        [new Date("2013-09-01T09:41:00+00:00"), null, null, 3.25, null],
        [new Date("2013-10-01T12:42:14+00:00"), null, null, 5.2, null],
        [new Date("2013-11-01T12:43:17+00:00"), null, null, 8.95, null],
        [new Date("2013-12-01T12:43:38+00:00"), null, null, 17.02, null],
        [new Date("2014-01-01T10:19:00+00:00"), null, null, null, 16.24],
        [new Date("2014-02-01T10:16:00+00:00"), null, null, null, 19.13],
        [new Date("2014-03-01T08:05:00+00:00"), null, null, null, 17.68],
        [new Date("2014-04-01T08:11:00+00:00"), null, null, null, 10.97],
        [new Date("2014-05-01T09:06:00+00:00"), null, null, null, 6.57],
        [new Date("2014-07-01T07:57:00+00:00"), null, null, null, 4.25],
        [new Date("2014-08-03T11:26:00+00:00"), null, null, null, 2.14],
        [new Date("2014-09-02T08:04:00+00:00"), null, null, null, 2.95],
        [new Date("2014-10-01T07:18:00+00:00"), null, null, null, 3.75]
     ]);

    var dView = new google.visualization.DataView(data);
    dView.setColumns([
                        {calc:getmon, type:'number', label:"Month"}
                         ,1
                         ,2
                         ,3
                         ,4]);
    function getmon(dataTable, rowNum){
        var rd = dataTable.getValue(rowNum, 0);
        var rm = rd.getMonth();
        return {v:rm};
    }

    var options = {
            hAxis: {
            title: 'Month'
            ,showTextEvery: 1
            ,ticks: [{v:0, f:'Jan'}, {v:1, f:'Feb'}, {v:2, f:'Mar'}, {v:3, f:'Apr'}, {v:4, f:'May'}, {v:5, f:'Jun'}, {v:6, f:'Jul'}, {v:7, f:'Aug'}, {v:8, f:'Sep'}, {v:9, f:'Oct'}, {v:10, f:'Nov'}, {v:11, f:'Dec'}]
            }
}

    var chart = new google.visualization.LineChart(document.getElementById('testchart'));
    chart.draw(dView, options);
}
drawChart();
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Google 折线图中按年份分隔日期 的相关文章

  • Vue.js + Element UI:在更改时获取“event.target”

    我无法获取在事件处理程序中触发事件的 html 字段 在 javascript 中是event target 我有一个表格 附加到更改事件函数的输入元素 管理更改事件的函数 我的代码如下 var Main methods change pa
  • 将nodejs Express静态请求重定向到https

    我需要将所有 http 请求重定向到 https 包括对静态文件的请求 My code app use express static dirname public app get function req res if req secure
  • 如何避免多系列折线图d3.js的工具提示重叠

    我已经在多系列折线图上创建了工具提示 如下所示在这里回答 https stackoverflow com questions 34886070 d3 js multiseries line chart with mouseover tool
  • 如何使用 RSpec 测试 javascript 重定向?

    我正在使用 xhr post 与控制器交互 并且我期待重定向 在 js erb 中 我有 window location href address 手动测试 浏览器会正确重定向 我如何使用 RSpec 测试它 response should
  • 在多个 angular.js 应用程序之间共享单个服务

    我正在构建一个电子商务网站 基于 shopify 并且使用多个小型 angularjs 应用程序来处理诸如快速购物车 愿望清单 过滤产品和其他一些较小项目之类的事情 我最初使用了一个大型应用程序 具有路由和所有内容 但当我没有完整的 RES
  • 如何使用谷歌地图检测一个点是否在多边形内部?

    我想检测到google maps LatLng是在一个里面google maps Polygon 我怎样才能做到这一点 Cheers 你可以使用这个谷歌地图V3 google maps geometry poly containsLocat
  • 任何 JavaScript 代码都是有效的 TypeScript 代码吗?

    目前我已经开始学习TypeScript 从我研究过的文档来看TypeScript 我看到一些纯的样品JavaScript代码可以编译为TypeScript code 我的问题是 TypeScript 语言的设计方式是否使任何 JavaScr
  • 为 Promise 编写循环的正确方法。

    如何正确构造循环以确保满足以下条件承诺电话和被束缚的记录器 log res 通过迭代同步运行 蓝鸟 db getUser email then function res logger log res this is a promise 我尝
  • 如何从矩形点计算旋转角度?

    我有4分1 2 3 4闭合一个矩形 这些点按以下方式排列在数组中 x1 y1 x2 y2 x3 y3 x4 y4 我遇到的问题是矩形可以旋转一定角度 如何计算原始点 灰色轮廓 和角度 我试图在 javascript css3 transfo
  • 在javascript中通过window.location传递数据

    我试图通过 window location 传递数据 数据在 del id img album 中可用 我想通过 window location 发送多个值 window location save php type deldownload
  • 使用 ngx-translate 时更改 URL

    当有人使用 ngx translate 单击所选语言时 我尝试更改 URL 我想我应该通过订阅语言更改事件然后修改当前的 url 以反映所选的语言来做到这一点 因为我是新手 所以我不确定是否需要服务来做到这一点 或者可能是另一种解决方法 我
  • Immutable.js 推入嵌套对象中的数组

    假设有一个对象 const object foo bar 1 2 3 我需要推动4 to object foo bar array 现在我正在这样做 const initialState Immutable fromJS object co
  • 如何在 Vue.js 2 中使用事件总线通过自定义事件传递数据

    我在用着Vue js 2 5 x 在我的玩具项目中 我实现了一个事件总线 类似于所示的here https alligator io vuejs global event bus 事件总线在 Vue 原型中全局注册为 eventBus 然后
  • 用于验证网络路径的正则表达式 PHP、jQuery、JavaScript、Ruby

    尝试找出用于验证网络路径的正则表达式 即 comp xyz or comp or comp x y z storage或者所有部分都更长的东西 但希望能够传达其要点 我目前拥有的是一个简单的输入字段 用户可以通过它传递信息 事情是我不希望他
  • 为什么react中的组件需要大写? [复制]

    这个问题在这里已经有答案了 因此 当您声明一个组件以小写首字母进行反应时 它不会显示 也不会引发错误 当您将组件名称大写时 它确实有效 这样的实现是怎样的呢 为了避免与现有的 html 元素发生冲突 还是这是一个错误 var test Re
  • 如何防止 CSS 或 jQuery 中单词和标点符号之间的换行

    我在一个段落中有一些文字 我的问题是 当标点符号位于单词末尾时 有时可以换行到下一行 像这样 This is the text This is a new line 我可以用 CSS 或 jQuery 解决这个问题吗 如果您不在单词和标点符
  • javascript捕获文本区域中的粘贴事件

    我目前有一个文本区域 我需要控制已粘贴的文本 本质上 我需要能够将用户想要粘贴到文本区域中的任何内容并将其放入变量中 然后我将计算出他们粘贴文本的位置和字符串的大小 以将其从文本区域中删除 然后最后用我自己的方式处理变量中的文本 我的问题
  • 检测浏览器是否支持 contentEditable?

    There s 这个问题 https stackoverflow com questions 3497942 browser detect contenteditable features 但发布的解决方案是浏览器嗅探 我试图避免这种情况
  • 来自 ajax 的 Bootstrap 表 json

    我有 ajax 和 bootstrap 表的问题 我有一个 ajax JSON 我用这个方法调用 document ready function ajax url php process php method fetchdata dataT
  • 使用来自Processing-JS的JSON

    我想使用编写一个应用程序处理 JS http processingjs org 并且我希望能够使用服务器端数据加载它 我还没有编写服务器端 所以我可以使用任何东西 但似乎明显的 AJAX 事情是使用 JSON 将数据上传到页面中 如何从我的

随机推荐