【问题标题】:Stacked Bar Flot bar chart show values of every category in each series with tooltipStacked Bar Flot 条形图使用工具提示显示每个系列中每个类别的值
【发布时间】:2018-07-12 12:47:05
【问题描述】:

在下面使用这个实现:

$.fn.UseStackBarTooltip = function (module) {
$(this).bind("plothover", function (event, pos, item) {
    if (item) {
        if ((previousLabel != item.series.label) || (previousPoint != item.dataIndex)) {
            previousPoint = item.dataIndex;
            previousLabel = item.series.label;
            $("#tooltip").remove();

            var x = item.datapoint[0];
            var y = item.datapoint[1] - item.datapoint[2];
            var formated = y.toLocaleString();

            var color = item.series.color;

            showStackTooltip(item.pageX,
                item.pageY,
                color,
                "<strong>" + item.series.label + "</strong><br><strong>" + formated + "</strong>", module);
        }
    } else {
        $("#tooltip").remove();
        previousPoint = null;
    }
});};

当我将鼠标悬停在所选栏上时,我可以显示所选类别的值,我想做的是我只想在一次悬停中显示所选栏上每个类别的所有值。是否有可能做到这一点?谢谢!

【问题讨论】:

    标签: jquery bar-chart flot stacked-chart


    【解决方案1】:

    当您处于悬停事件并悬停在某个项目上时,您必须遍历 x 轴索引上的数据值以获取其余的堆叠值:

    $("#graph").bind("plothover", function(event, pos, item) {
    
      // if we're hovering over an item
      if (item) {
        // get the data in the plot
        var plotData = plot.getData();
        var valueString = "";
    
        // loop through the data and grab each value in the same stack
        for (var i = 0; i < plotData.length; ++i) {
          var series = plotData[i];
          for (var j = 0; j < series.data.length; ++j) {
            // item.datapoint[0] contains the x axis value of the hovered over item
            // ensure we get the other values at the same x axis value.
            if (series.data[j][0] === item.datapoint[0]) {
              valueString += series.data[j][1] + " ";
            }
          }
        }
    
        // show the tool tip here - the valueString object has all the values in the stack
    
      }
    });
    

    下面的示例在工具提示中显示了堆栈的值 - 您需要根据需要格式化字符串。

    $(function() {
      var data = [{
        data: [
          [0, 21.51],
          [1, 32.50],
          [2, 47.14],
          [3, 10]
        ],
        stack: 0,
        label: 'Bottom'
      }, {
        data: [
          [0, 37.77],
          [1, 24.65],
          [2, 7.67],
          [4, 15]
        ],
        stack: 0,
        label: 'Top'
      }];
    
      var options = {
        series: {
          bars: {
            show: true,
            barWidth: .5,
            align: "center"
          },
          points: {
            show: false
          }
        },
        grid: {
          show: true,
          hoverable: true,
        }
      };
    
      var plot = $.plot($('#graph'), data, options);
    
      $("#graph").bind("plothover", function(event, pos, item) {
      
        $("#tooltip").remove();
    
        if (item) {
          var plotData = plot.getData();
          var valueString = "";
    
          for (var i = 0; i < plotData.length; ++i) {
            var series = plotData[i];
            for (var j = 0; j < series.data.length; ++j) {
              if (series.data[j][0] === item.datapoint[0]) {
                valueString += series.data[j][1] + " ";
              }
            }
          }
    
          $("<div id='tooltip'>" + valueString + "</div>").css({
            position: "absolute",
            display: "none",
            top: pos.pageY + 5,
            left: pos.pageX + 5,
            border: "1px solid #fdd",
            padding: "2px",
            "background-color": "#fee",
            opacity: 0.80
          }).appendTo("body").fadeIn(200);
    
        }
      });
    });
    #graph {
      width: 600px;
      height: 400px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.js"></script>
    <script src="https://rawgit.com/flot/flot/master/jquery.flot.stack.js"></script>
    <div id="graph"></div>

    【讨论】:

    • 感谢您的回复!它对我来说很好,但我想问我是否可以获得类别名称而不仅仅是情节中的值?如果可能的话,还有每个类别的颜色。
    • @Mr.Hans 我相信你可以。您可能可以从系列对象中获取信息。这应该会给你一个好的开始。
    【解决方案2】:

    $("#graph").bind("plothover", function(event, pos, item) {
    
      // if we're hovering over an item
      if (item) {
        // get the data in the plot
        var plotData = plot.getData(); 
        var valueString = plotData[item.seriesIndex].data[item.dataIndex][1];
        // show the tool tip here - the valueString object has all the values in the stack
    
      }
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 2020-05-23
      相关资源
      最近更新 更多