javascript - 遍历每个工具提示以添加不同的时间戳

标签 javascript chart.js tooltip

第一项: First Chart 第二项: Second chart

正如您在图表中圈出的内容所见,这不应该是同一时间。 我正在尝试遍历我的工具提示以向每个工具提示打印不同的日期,我尝试循环但它似乎不起作用,这是我尝试过的:

tooltips: {
  callbacks: {
    title: function(tooltipItem, data) {
      return data.labels[tooltipItem[0].index];
    },
    //Problem probably here:
    afterTitle: function(tooltipItem, data) {
      if (data.labels[tooltipItem[0].index]) {
        return '<?php print_r($tempo[$mudarHorario][0]); $mudarHorario++;?>';
      }
    },
    label: function(tooltipItem, data) {
      var label = data.datasets[tooltipItem.datasetIndex].label || '';
      if (label) {
        label += ': ';
      }
      label += (tooltipItem.yLabel);
      return label;
    }
  }
}

这是我从数据库中收集的日期的一部分,所以你可以看看:

if ($resultCheck > 0) {
  while ($row = mysqli_fetch_assoc($result)) {
    $horario = (date('H:i:s', strtotime(str_replace('.', '-', $row['dias']))));

    $tempo[] = explode(',', $horario);

    $geral = $geral.
    '"'.(date('d/m/y', strtotime(str_replace('.', '-', $row['dias'])))).
    '",';
  }
}

$labels = trim($geral, ",");
$mudarHorario = 0;

更新:

我可以让数据正确显示,我尝试添加一个循环以便更改日期,但我不知道如何访问每个标签以更改日期时间。 这是图表的最小版本,因此您可以尝试这样做,在此示例中,由于 while 循环,标签将不会显示,链接:Chart example

最佳答案

一直在研究一些文档并寻求有关数组的帮助,并遇到了这个非常简单的解决方案:
将 PHP 数组传递给 javascript 数组:

var date = <?php echo json_encode($yourVariabel) ?>
//In my case:
var date = <?php echo json_encode($tempo) ?>

这是将值打印到每个标签的方法:

tooltips: {
              enabled: true, //Add this true
              mode: 'single', //Add this single
              callbacks:{
//Dont need this title stuf if dont want to
                title: function(tooltipItem, data) {
                    return data.labels[tooltipItem[0].index];
                },
//This is the good part:
                afterTitle: function(tooltipItem, data) {
                  var date = <?php echo json_encode($tempo) ?>

                  return date[tooltipItem[0]['index']]; 
                },
//Dont need this title stuf if dont want to
                label: function(tooltipItem, data) {
                      var label = data.datasets[tooltipItem.datasetIndex].label || '';                  
                      if (label) {
                          label += ': ';
                      }
                      label += (tooltipItem.yLabel)+"A";                  
                      return label;
                }
              }
            },

关于javascript - 遍历每个工具提示以添加不同的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58702355/

相关文章:

javascript - 仅显示天数时间刻度

chart.js - 使用 chart.js 悬停在图表上时移动垂直线

Angular 5 Chart.js onclick 返回空数组

java - 从大量条目中实现选择框的最佳方法

r - 将工具提示格式化为货币并打开比较模式

javascript - 将文本输入推送到带 Angular 数组

php - 加载太多图片,减慢我的网站

javascript - 粘性标题在向上滚动时向下滑动

javascript - 如何在没有鼠标悬停的情况下始终在 chartjs 中显示标签?

javascript - 删除标题标签工具提示