【问题标题】:Flot tooltips appear on line graph but not bar chartFlot 工具提示出现在折线图上,但不出现在条形图上
【发布时间】:2015-05-26 15:37:57
【问题描述】:

我有一个 [Moment, integer] 对数组,我使用 flot 在图中绘制。当我将数据绘制为条形图时,工具提示不会出现。

但是,如果我显示点,或者如果我将图表转换为折线图,则会出现工具提示。

正如下面的 sn-p 所示,我实际上是在使用 flot 示例代码。当绘制为条形图时,可能导致工具提示无法显示的原因是什么?可编辑的 JSFiddle 可用here

var people_count_data = [
  [moment("2015-05-26T09:15:00+00:00"), 0],
  [moment("2015-05-26T09:30:00+00:00"), 0],
  [moment("2015-05-26T09:45:00+00:00"), 0],
  [moment("2015-05-26T10:00:00+00:00"), 0],
  [moment("2015-05-26T10:15:00+00:00"), 0],
  [moment("2015-05-26T10:30:00+00:00"), 0],
  [moment("2015-05-26T10:45:00+00:00"), 0],
  [moment("2015-05-26T11:00:00+00:00"), 0],
  [moment("2015-05-26T11:15:00+00:00"), 0],
  [moment("2015-05-26T11:30:00+00:00"), 0],
  [moment("2015-05-26T11:45:00+00:00"), 2],
  [moment("2015-05-26T12:00:00+00:00"), 51],
  [moment("2015-05-26T12:15:00+00:00"), 59],
  [moment("2015-05-26T12:30:00+00:00"), 72],
  [moment("2015-05-26T12:45:00+00:00"), 23],
  [moment("2015-05-26T13:00:00+00:00"), 50],
  [moment("2015-05-26T13:15:00+00:00"), 55],
  [moment("2015-05-26T13:30:00+00:00"), 52],
  [moment("2015-05-26T13:45:00+00:00"), 53],
  [moment("2015-05-26T14:00:00+00:00"), 39],
  [moment("2015-05-26T14:15:00+00:00"), 50],
  [moment("2015-05-26T14:30:00+00:00"), 51],
  [moment("2015-05-26T14:45:00+00:00"), 55],
  [moment("2015-05-26T15:00:00+00:00"), 39],
  [moment("2015-05-26T15:15:00+00:00"), 12],
  [moment("2015-05-26T15:30:00+00:00"), 0],
  [moment("2015-05-26T15:45:00+00:00"), 0],
  [moment("2015-05-26T16:00:00+00:00"), 0],
  [moment("2015-05-26T16:15:00+00:00"), 0],
  [moment("2015-05-26T16:30:00+00:00"), 0],
  [moment("2015-05-26T16:45:00+00:00"), 0],
  [moment("2015-05-26T17:00:00+00:00"), 0],
  [moment("2015-05-26T17:15:00+00:00"), 0],
];

var plotOptions = {
  //Options go here
  xaxis: {
    mode: "time",
    reserveSpace: true,
    tickLength: 5,
    autoscaleMargin: 0.01
  },
  yaxis: {
    min: 0
  },
  grid: {
    hoverable: true,
    clickable: true
  },
  series: {
    // If I comment out bars and turn the chart into a line graph, tooltips work(!)
    bars: {
      show: true
    },
    // If I show the points on the bar graph, tooltips work(!)
    //    points: {
    //      show: true
    //    }
  },
};

var plot1 = $.plot(
  '#store-people-count-plot', [{
    label: "People Count",
    color: "#FC8200",
    data: people_count_data
  }], plotOptions);

function showTooltip(x, y, contents) {
  $("<div id='tooltip'>" + contents + "</div>").css({
    position: "absolute",
    display: "none",
    top: y + 5,
    left: x + 5,
    border: "1px solid #fdd",
    padding: "2px",
    "background-color": "#fee",
    opacity: 0.80
  }).appendTo("body").fadeIn(200);
}

var previousPoint = null;
var hoverCallback = function(event, pos, item) {
  if (item) {
    if (previousPoint != item.dataIndex) {
      previousPoint = item.dataIndex;
      var x_moment = moment(item.datapoint[0]);
      $("#tooltip").remove();
      var y = item.datapoint[1];
      var tooltipString = x_moment.format("HH:mm") + ", " + y;
      showTooltip(item.pageX, item.pageY,
        tooltipString);
    }
  } else {
    $("#tooltip").remove();
    previousPoint = null;
  }
};

$('#store-people-count-plot').on("plothover", hoverCallback);
#store-people-count-plot {
  width: 400px;
  height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.3.1/moment-timezone.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.time.js"></script>

<div id="store-people-count-plot"></div>

【问题讨论】:

    标签: javascript jquery flot


    【解决方案1】:

    你的条太细了。如果放大浏览器窗口并将光标悬停在栏上,则会显示工具提示。尝试使用barwidth 选项扩大你的酒吧:

    "barWidth" 是条形的宽度,以 x 轴为单位(如果 "horizo​​ntal" 为真,则为 y 轴),与大多数其他以像素为单位的度量相反。例如,对于时间序列,单位是毫秒,因此 24 * 60 * 60 * 1000 会产生一天宽度的条形图。

    我已修改您的选项并将 barWidth 设置为 10 分钟,并且工具提示有效。

    var people_count_data = [
      [moment("2015-05-26T09:15:00+00:00"), 0],
      [moment("2015-05-26T09:30:00+00:00"), 0],
      [moment("2015-05-26T09:45:00+00:00"), 0],
      [moment("2015-05-26T10:00:00+00:00"), 0],
      [moment("2015-05-26T10:15:00+00:00"), 0],
      [moment("2015-05-26T10:30:00+00:00"), 0],
      [moment("2015-05-26T10:45:00+00:00"), 0],
      [moment("2015-05-26T11:00:00+00:00"), 0],
      [moment("2015-05-26T11:15:00+00:00"), 0],
      [moment("2015-05-26T11:30:00+00:00"), 0],
      [moment("2015-05-26T11:45:00+00:00"), 2],
      [moment("2015-05-26T12:00:00+00:00"), 51],
      [moment("2015-05-26T12:15:00+00:00"), 59],
      [moment("2015-05-26T12:30:00+00:00"), 72],
      [moment("2015-05-26T12:45:00+00:00"), 23],
      [moment("2015-05-26T13:00:00+00:00"), 50],
      [moment("2015-05-26T13:15:00+00:00"), 55],
      [moment("2015-05-26T13:30:00+00:00"), 52],
      [moment("2015-05-26T13:45:00+00:00"), 53],
      [moment("2015-05-26T14:00:00+00:00"), 39],
      [moment("2015-05-26T14:15:00+00:00"), 50],
      [moment("2015-05-26T14:30:00+00:00"), 51],
      [moment("2015-05-26T14:45:00+00:00"), 55],
      [moment("2015-05-26T15:00:00+00:00"), 39],
      [moment("2015-05-26T15:15:00+00:00"), 12],
      [moment("2015-05-26T15:30:00+00:00"), 0],
      [moment("2015-05-26T15:45:00+00:00"), 0],
      [moment("2015-05-26T16:00:00+00:00"), 0],
      [moment("2015-05-26T16:15:00+00:00"), 0],
      [moment("2015-05-26T16:30:00+00:00"), 0],
      [moment("2015-05-26T16:45:00+00:00"), 0],
      [moment("2015-05-26T17:00:00+00:00"), 0],
      [moment("2015-05-26T17:15:00+00:00"), 0],
    ];
    
    var plotOptions = {
      //Options go here
      xaxis: {
        mode: "time",
        reserveSpace: true,
        tickLength: 5,
        autoscaleMargin: 0.01
      },
      yaxis: {
        min: 0
      },
      grid: {
        hoverable: true,
        clickable: true
      },
      series: {
        // If I comment out bars and turn the chart into a line graph, tooltips work(!)
        bars: {
          show: true,
          barWidth: 60*10*1000
        },
        // If I show the points on the bar graph, tooltips work(!)
        //    points: {
        //      show: true
        //    }
      },
    };
    
    var plot1 = $.plot(
      '#store-people-count-plot', [{
        label: "People Count",
        color: "#FC8200",
        data: people_count_data
      }], plotOptions);
    
    function showTooltip(x, y, contents) {
      $("<div id='tooltip'>" + contents + "</div>").css({
        position: "absolute",
        display: "none",
        top: y + 5,
        left: x + 5,
        border: "1px solid #fdd",
        padding: "2px",
        "background-color": "#fee",
        opacity: 0.80
      }).appendTo("body").fadeIn(200);
    }
    
    var previousPoint = null;
    var hoverCallback = function(event, pos, item) {
      if (item) {
        if (previousPoint != item.dataIndex) {
          previousPoint = item.dataIndex;
          var x_moment = moment(item.datapoint[0]);
          $("#tooltip").remove();
          var y = item.datapoint[1];
          var tooltipString = x_moment.format("HH:mm") + ", " + y;
          showTooltip(item.pageX, item.pageY,
            tooltipString);
        }
      } else {
        $("#tooltip").remove();
        previousPoint = null;
      }
    };
    
    $('#store-people-count-plot').on("plothover", hoverCallback);
    #store-people-count-plot {
      width: 400px;
      height: 300px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.3.1/moment-timezone.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.min.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.time.js"></script>
    
    <div id="store-people-count-plot"></div>

    【讨论】:

    • 谢谢。这就是问题所在。
    猜你喜欢
    • 2017-03-20
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多