绘制与我的折线图重叠的标签

时间:2016-08-09 15:25:36

标签: jquery flot labels overlapping

我正在使用flot的折线图功能但是我在保持我的x和y轴标签重叠到图表上时遇到了一些困难。我的图表看起来像这样

enter image description here

理想情况下,我想将标签移到左侧和底部,这样它们就不会与图形重叠。我正在构建这样的图表

$(function() {

<%
  js_data = []
  ids = []
  @user_my_objects.each do |user_my_object|
    ids.push( user_my_object.id )
    my_object_day_time_in_ms = user_my_object.my_object.day.strftime('%Q')
    js_data.push( "[#{my_object_day_time_in_ms}, #{user_my_object.time_in_ms}]" )
  end
%>
        // <%= ids %>
        var data = [<%=h js_data.join(",") %>];

        $("<div id='tooltip'></div>").css({
                position: "absolute",
                display: "none",
                border: "1px solid #fdd",
                padding: "2px",
                "background-color": "#fee",
                opacity: 0.80
        }).appendTo("body");

        $.plot("#placeholder", [data], {
                yaxis: {
                        tickFormatter: formatTime
                },
                xaxis: { mode: "time" },
                points: {
                        show: true
                },
                lines: {
                        show: true
                },
                grid: {
                      hoverable: true,
                      clickable: true,
                      tickColor: "#efefef",
                      borderWidth: 0,
                      borderColor: "#efefef"
                },
                tooltip: true
        });

        $("#placeholder").bind("plothover", function (event, pos, item) {
                if (item) {
                        var x = item.datapoint[0].toFixed(2),
                                y = item.datapoint[1].toFixed(2);

                        console.log("x:" + x)
                        dateObj = new Date(parseInt(x))
                        var dateStr = $.datepicker.formatDate('MM dd, yy', dateObj)
                        $("#tooltip").html( dateStr + " - " + formatTime(y) )
                                .css({top: item.pageY+5, left: item.pageX+5})
                                .fadeIn(200);
                } else {
                        $("#tooltip").hide();
                }
        });

});

编辑:难以捉摸的小提琴 - http://jsfiddle.net/edc8jd31/1/

4 个答案:

答案 0 :(得分:3)

请将以下内容添加到 CSS

#placeholder div.xAxis div.tickLabel {
    transform: rotate(0deg) !important;
    margin-top:10px;
}

#placeholder div.yAxis div.tickLabel {
    margin-right: 10px !important;
}

<强>输出:

After making change to CSS

答案 1 :(得分:2)

从小提琴开始,将translateY(15px)添加到CSS中的transform行,并将labelHeight: 30添加到xaxis选项中。有关完整示例,请参阅updated fiddle

enter image description here

<强>解释
通过在轴标签上使用transform: rotate(45%),可以围绕它们的中心旋转它们,使左半部分位于轴上方。附加平移将标签移动到轴下方。 labelHeight选项是必需的,因此标签的右半部分不会低于图表的边缘。

答案 2 :(得分:1)

我不确定我是否理解你的问题,但你只是想在x和z轴上移动标签而不移动图表本身,对吗?

对于x-Axis,CSS3属性transform: translate(x, y);对您有用。

对于y轴你理论上可以做同样的事情,但是为了向下移动边距会更好。

对于标签的轮换,请使用transform: rotate(degrees)

所以这可能有用:

#placeholder div.yAxis div.tickLabel
{
  transform: translate(-10px, 0px);
}

#placeholder div.xAxis div.tickLabel
{
  margin-top: 20px;
  transform: rotate(45deg);
}

答案 3 :(得分:1)

我不确定我是否理解你的问题,但你是否期待这样的事情?
使用简单的jQuery来改变每个X轴div的css。

jQuery:

$('.xAxis.x1Axis div.tickLabel').each(function() {

   var newVal = $(this).css('left');
   newVal = parseInt(newVal.substring(0, newVal.indexOf("px"))) + 15;
   $(this).css('left', newVal);

   //$(this).css('top', "492px");
   // Instead of above commented line You can use css directly.

});

CSS:

.xAxis.x1Axis .tickLabel
{
  top :492px !important;
}

function formatTime(duration) {
  var milliseconds = parseInt((duration % 1000) / 100),
    seconds = parseInt((duration / 1000) % 60),
    minutes = parseInt((duration / (1000 * 60)) % 60),
    hours = parseInt((duration / (1000 * 60 * 60)) % 24);

  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return (hours > 0 ? hours + ":" : "") + minutes + ":" + seconds + (milliseconds > 0 ? "." + milliseconds : "");
}

$(function() {

  // [4775444, 4775496, 4775541]
  var data = [
    [1403913600000, 2915000],
    [1437782400000, 2788000],
    [1466812800000, 2759000]
  ];

  $("<div id='tooltip'></div>").css({
    position: "absolute",
    display: "none",
    border: "1px solid #fdd",
    padding: "2px",
    "background-color": "#fee",
    opacity: 0.80
  }).appendTo("body");

  $.plot("#placeholder", [data], {
    yaxis: {
      tickFormatter: formatTime
    },
    xaxis: {
      mode: "time"
    },
    points: {
      show: true
    },
    lines: {
      show: true
    },
    grid: {
      margin: 10,
      labelMargin: 5,
      labelWidth: 20,
      hoverable: true,
      clickable: true,
      tickColor: "#efefef",
      borderWidth: 0,
      borderColor: "#efefef"
    },
    tooltip: true
  });

  $("#placeholder").bind("plothover", function(event, pos, item) {
    if (item) {
      var x = item.datapoint[0].toFixed(2),
        y = item.datapoint[1].toFixed(2);

      console.log("x:" + x)
      dateObj = new Date(parseInt(x))
      var dateStr = $.datepicker.formatDate('MM dd, yy', dateObj)
      $("#tooltip").html(dateStr + " - " + formatTime(y))
        .css({
          top: item.pageY + 5,
          left: item.pageX + 5
        })
        .fadeIn(200);
    } else {
      $("#tooltip").hide();
    }
  });

  $('.xAxis.x1Axis div.tickLabel').each(function() {

    var newVal = $(this).css('left');
    newVal = parseInt(newVal.substring(0, newVal.indexOf("px"))) + 15;
    $(this).css('left', newVal);

    //$(this).css('top', "492px");
    // instead of hardcoding you can apply via css:

  });

});
.demo-container {
  padding: 20px;
  background-color: orange;
}
#placeholder div.xAxis div.tickLabel {
  transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  /* IE 9 */
  -moz-transform: rotate(45deg);
  /* Firefox */
  -webkit-transform: rotate(45deg);
  /* Safari and Chrome */
  -o-transform: rotate(-90deg);
  /* Opera */
  /*rotation-point:50% 50%;*/
  /* CSS3 */
  /*rotation:270deg;*/
  /* CSS3 */
}

.xAxis.x1Axis .tickLabel
{
  top :492px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://people.iola.dk/olau/flot/jquery.flot.js"></script>

<div class="demo-container">
  <div id="placeholder" class="demo-placeholder" style="width:800px; height:500px;"></div>
</div>