Flot Data Labels

后端 未结 5 964
眼角桃花
眼角桃花 2020-12-05 04:21

I\'m trying to produce a line chart using Flot, but I want the data labels to show up on the chart - meaning, I want the value of each point to appear next to that point. I

相关标签:
5条回答
  • 2020-12-05 04:47

    Here is how I added the feature, including a pleasant animation effect:

    var p = $.plot(...);
    
    $.each(p.getData()[0].data, function(i, el){
      var o = p.pointOffset({x: el[0], y: el[1]});
      $('<div class="data-point-label">' + el[1] + '</div>').css( {
        position: 'absolute',
        left: o.left + 4,
        top: o.top - 43,
        display: 'none'
      }).appendTo(p.getPlaceholder()).fadeIn('slow');
    });
    

    You can move the position and display css to a stylesheet.

    0 讨论(0)
  • 2020-12-05 04:53

    If anyone else is looking for a quick solution, see this plugin:

    http://sites.google.com/site/petrsstuff/projects/flotvallab

    0 讨论(0)
  • 2020-12-05 04:54
    function redrawplot() {
       $('.tt1').remove();
       var points = plot.getData();
         var graphx = $('#placeholder').offset().left;
         graphx = graphx + 30; // replace with offset of canvas on graph
         var graphy = $('#placeholder').offset().top;
         graphy = graphy + 10; // how low you want the label to hang underneath the point
         for(var k = 0; k < points.length; k++){
              for(var m = 1; m < points[k].data.length-1; m++){
                if(points[k].data[m][0] != null && points[k].data[m][1] != null){
                      if ((points[k].data[m-1][1] < points[k].data[m][1] && points[k].data[m][1] > points[k].data[m+1][1]) && (points[k].data[m-1][1] - points[k].data[m][1] < -50 || points[k].data[m][1] - points[k].data[m+1][1] > 50)) {
                                     showTooltip1(graphx + points[k].xaxis.p2c(points[k].data[m][0]) - 15, graphy + points[k].yaxis.p2c(points[k].data[m][1]) - 35,points[k].data[m][1], points[k].color);
                  }
                                  if ((points[k].data[m-1][1] > points[k].data[m][1] && points[k].data[m][1] < points[k].data[m+1][1]) && (points[k].data[m-1][1] - points[k].data[m][1] > 50 || points[k].data[m][1] - points[k].data[m+1][1] < -50)) {
                                     showTooltip1(graphx + points[k].xaxis.p2c(points[k].data[m][0]) - 15, graphy + points[k].yaxis.p2c(points[k].data[m][1]) + 2,points[k].data[m][1], points[k].color);
                                  }
                                }
            }
          }
    
     }
    
    function showTooltip1(x,y,contents, colour){
      $('<div class=tt1 id="value">' +  contents + '</div>').css( {
            position: 'absolute',
            display: 'none',
            top: y,
            left: x,
            'border-style': 'solid',
            'border-width': '2px',
            'border-color': colour,
            'border-radius': '5px',
            'background-color': '#ffffff',
            color: '#262626',
            padding: '0px',
            opacity: '1'
      }).appendTo("body").fadeIn(200);
    } 
    
    0 讨论(0)
  • 2020-12-05 04:58

    The feature you want is requested here in the Flot Google group. It doesn't look like it was ever implemented (there's nothing in the API about putting any labels inside the chart itself). I think that the answer to your question is that No, it's not possible at this time to show values next to certain points on lines inside the graph.

    Ole Larson, head developer on Flot, mentioned that showing labels inside the chart is different than anything else on FLot and that they would have to think about how to extend the API / plot parameters to do it.

    That said, you might want to go post a question on the Flot forum or make a suggestion on the bug-tracker for the new feature. Ole Larson is actually really good at getting back to all the questions, bugs, and suggestions himself.

    0 讨论(0)
  • 2020-12-05 05:03

    Looks like the flot-valuelabels plugin is a fork of a previous flot plugin -- but the forked code renders the labels on the canvas. You can see a demo of what this looks like on the plugin's Github wiki page, here (it looks quite pleasing to the eye).

    0 讨论(0)
提交回复
热议问题