javascript - 使用 twig 日期时间中的日期实现 googlechart x 轴

标签 javascript datetime twig google-visualization

我有一个过去 30 天的数组,每天有 2 个键:

-日期(日期时间)

-计数(整数)

enter image description here

我尝试使用 Xaxis 中的日期和 Yaxis 中的 count 实例化 google 图表,但是当我尝试使用 twig 循环数组时,我的日期时间值似乎发生了更改。

这是我在 drawGraph() 函数中循环在图表上添加行的方法:

 function drawChart() {

      var dataTable = new google.visualization.DataTable();
        dataTable.addColumn({ type: 'date', id: 'date', label: 'date'});
        dataTable.addColumn({ type: 'number', id: 'Nombre d OF', label: 'Nb of'});


    {% for value in chartData.data %}
        console.log({{ value.date.format('Y-m-d') }});
        dataTable.addRow([new Date("{{ value.date.format('Y-m-d')| date("Y-m-d") }}".replace(/-/g,"/")), {{ value.count }}]);
    {% endfor %}

    var options = {
      title: 'Nombre d\'ordre de fabrication crées sur les 30 derniers jours ('+{{ chartData.info }}+')',
      hAxis: {title: 'Jours',  titleTextStyle: {color: '#333'}},
      vAxis: {minValue: 0}
    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
    chart.draw(dataTable, options);
  }

我打印了每次循环迭代的日期,得到了这个结果:

enter image description here

我的问题是:为什么我的循环中的日期发生了变化?

最佳答案

好吧,2017 - 9 - 24 实际上是 1984,您需要用引号将您的语句括起来,否则它将被视为子分支

console.log('{{ value.date.format('Y-m-d') }}');

关于javascript - 使用 twig 日期时间中的日期实现 googlechart x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46364666/

相关文章:

javascript - 如何检测 ":"之前的单词并使用 jquery 包装在 "span"中?

symfony - 如何在 Symfony Controller 中获取 Assets img url

symfony - 如何从父包扩展 Twig 模板?

c# - 没有 DateTime?.ToString(string) 重载?

java - 如何在Java中解析格式为 "\/Date(1339638938087-0500)\/"的日期字符串?

24 小时前的 Javascript 相对时间等作为时间

twig - Symfony3 Twig 中的语言切换

javascript - 奇怪的闭包编译器问题

javascript - 如何在 Angular 中实现表单投影?

javascript - 如果 POST 响应仅包含 ID,如何将数据保存到我的 Ember 存储?