首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用时间在x轴上显示正确的日期数字的flot

使用时间在x轴上显示正确的日期数字的flot
EN

Stack Overflow用户
提问于 2016-02-11 11:02:51
回答 1查看 289关注 0票数 0

我有13周的数据,我想显示为条形图。我要每周星期一开始日期。我已经设法得到了13个星期显示在xaxis和13条线显示。我不能让日期从星期一开始,并与酒吧排队。

代码语言:javascript
复制
var datasets = [{"label":"Amend Existing Report","data":[{"0":1446422400000,"1":0},{"0":1447027200000,"1":0},{"0":1447632000000,"1":0},{"0":1448236800000,"1":0},{"0":1448841600000,"1":0},{"0":1449446400000,"1":1},{"0":1450051200000,"1":0},{"0":1450656000000,"1":0},{"0":1451260800000,"1":0},{"0":1451865600000,"1":0},{"0":1452470400000,"1":0},{"0":1453075200000,"1":1},{"0":1453680000000,"1":1},{"0":1454284800000,"1":0}],"idx":0},{"label":"Investigate Report Problem","data":[{"0":1446422400000,"1":1},{"0":1447027200000,"1":0},{"0":1447632000000,"1":2},{"0":1448236800000,"1":4},{"0":1448841600000,"1":0},{"0":1449446400000,"1":1},{"0":1450051200000,"1":0},{"0":1450656000000,"1":2},{"0":1451260800000,"1":0},{"0":1451865600000,"1":0},{"0":1452470400000,"1":0},{"0":1453075200000,"1":5},{"0":1453680000000,"1":0},{"0":1454284800000,"1":0}],"idx":1},{"label":"New Request (One Off Report)","data":[{"0":1446422400000,"1":0},{"0":1447027200000,"1":0},{"0":1447632000000,"1":1},{"0":1448236800000,"1":0},{"0":1448841600000,"1":0},{"0":1449446400000,"1":0},{"0":1450051200000,"1":0},{"0":1450656000000,"1":0},{"0":1451260800000,"1":0},{"0":1451865600000,"1":0},{"0":1452470400000,"1":0},{"0":1453075200000,"1":0},{"0":1453680000000,"1":1},{"0":1454284800000,"1":0}],"idx":2},{"label":"New Request (Regular Report)","data":[{"0":1446422400000,"1":4},{"0":1447027200000,"1":0},{"0":1447632000000,"1":2},{"0":1448236800000,"1":2},{"0":1448841600000,"1":0},{"0":1449446400000,"1":1},{"0":1450051200000,"1":0},{"0":1450656000000,"1":0},{"0":1451260800000,"1":1},{"0":1451865600000,"1":1},{"0":1452470400000,"1":0},{"0":1453075200000,"1":3},{"0":1453680000000,"1":2},{"0":1454284800000,"1":0}],"idx":3},{"label":"Other","data":[{"0":1446422400000,"1":0},{"0":1447027200000,"1":0},{"0":1447632000000,"1":2},{"0":1448236800000,"1":4},{"0":1448841600000,"1":2},{"0":1449446400000,"1":0},{"0":1450051200000,"1":2},{"0":1450656000000,"1":0},{"0":1451260800000,"1":0},{"0":1451865600000,"1":0},{"0":1452470400000,"1":3},{"0":1453075200000,"1":0},{"0":1453680000000,"1":3},{"0":1454284800000,"1":0}],"idx":4},{"label":"Special Events","data":[{"0":1446422400000,"1":0},{"0":1447027200000,"1":0},{"0":1447632000000,"1":0},{"0":1448236800000,"1":1},{"0":1448841600000,"1":0},{"0":1449446400000,"1":3},{"0":1450051200000,"1":1},{"0":1450656000000,"1":0},{"0":1451260800000,"1":0},{"0":1451865600000,"1":0},{"0":1452470400000,"1":0},{"0":1453075200000,"1":0},{"0":1453680000000,"1":0},{"0":1454284800000,"1":0}],"idx":5}];

var options = {"legend":{"position":"ne","noColumns":6},"yaxis":{"min":0},"xaxis":{"mode":"time","timeformat":"%d %b","tickSize":[7,"day"],"min":1446163200000,"max":1454284800000},"grid":{"clickable":true,"hoverable":true},"series":{"stack":true,"bars":{"show":true,"barWidth":181440000.00000003}}};

$.plot($('#CAGraph'), datasets, options);


$("#CAGraph").bind("plothover",function(event, pos, item) {
		if (item) {
			// console.log(event);
			// console.log(pos);
			//console.log(item);
			var epoch = new Date(item.datapoint[0]);
			var percent = item.datapoint[1] - item.datapoint[2];
			$("#tooltip").html(
					moment(epoch).format("DD MMM") + " - "
							+ item.series.label + " " + (percent)
							+ "<br>Total: " + item.datapoint[1]).css({
				top : item.pageY - 25,
				left : item.pageX + 10
			}).fadeIn(200);
		} else {
			$("#tooltip").hide();
		}
	});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://rawgit.com/flot/flot/master/jquery.flot.js"></script>
<script src="https://rawgit.com/Codicode/flotanimator/master/jquery.flot.animator.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.time.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.stack.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot.tooltip/0.8.5/jquery.flot.tooltip.js"></script>
<div id ="choices_CAGraph">

</div>
<div id="CAGraph" style="width:910px;height:400px">
  <div id='tooltip' style="position:absolute;display:none"></div>

X轴的My min和max胼胝体

代码语言:javascript
复制
min : (new Date(moment().subtract(14,'week').day(0).subtract(2,"day").subtract(6,"hour").format('YYYY/MM/DD')).getTime()),
        max : (new Date(moment().subtract(1,'week').startOf('isoweek').format('YYYY/MM/DD')).getTime())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-11 11:42:14

当来自Flot的自动滴答生成器不符合您的要求时,您总是可以手动定义滴答(在这里,从数据中获取它们):

代码语言:javascript
复制
var ticks = [];
for (var i = 0; i < datasets[0].data.length; i++) {
  ticks.push(datasets[0].data[i][0]);
}

在为条形图指定align: 'center'之后,条形线和x轴刻度线现在对齐。

PS:您的数据有14个数据点,而不是13个。我还调整了x轴的最大值,以便最后一个条是可见的。

代码语言:javascript
复制
var datasets = [{
  "label": "Amend Existing Report",
  "data": [{
    "0": 1446422400000,
    "1": 0
  }, {
    "0": 1447027200000,
    "1": 0
  }, {
    "0": 1447632000000,
    "1": 0
  }, {
    "0": 1448236800000,
    "1": 0
  }, {
    "0": 1448841600000,
    "1": 0
  }, {
    "0": 1449446400000,
    "1": 1
  }, {
    "0": 1450051200000,
    "1": 0
  }, {
    "0": 1450656000000,
    "1": 0
  }, {
    "0": 1451260800000,
    "1": 0
  }, {
    "0": 1451865600000,
    "1": 0
  }, {
    "0": 1452470400000,
    "1": 0
  }, {
    "0": 1453075200000,
    "1": 1
  }, {
    "0": 1453680000000,
    "1": 1
  }, {
    "0": 1454284800000,
    "1": 0
  }],
  "idx": 0
}, {
  "label": "Investigate Report Problem",
  "data": [{
    "0": 1446422400000,
    "1": 1
  }, {
    "0": 1447027200000,
    "1": 0
  }, {
    "0": 1447632000000,
    "1": 2
  }, {
    "0": 1448236800000,
    "1": 4
  }, {
    "0": 1448841600000,
    "1": 0
  }, {
    "0": 1449446400000,
    "1": 1
  }, {
    "0": 1450051200000,
    "1": 0
  }, {
    "0": 1450656000000,
    "1": 2
  }, {
    "0": 1451260800000,
    "1": 0
  }, {
    "0": 1451865600000,
    "1": 0
  }, {
    "0": 1452470400000,
    "1": 0
  }, {
    "0": 1453075200000,
    "1": 5
  }, {
    "0": 1453680000000,
    "1": 0
  }, {
    "0": 1454284800000,
    "1": 0
  }],
  "idx": 1
}, {
  "label": "New Request (One Off Report)",
  "data": [{
    "0": 1446422400000,
    "1": 0
  }, {
    "0": 1447027200000,
    "1": 0
  }, {
    "0": 1447632000000,
    "1": 1
  }, {
    "0": 1448236800000,
    "1": 0
  }, {
    "0": 1448841600000,
    "1": 0
  }, {
    "0": 1449446400000,
    "1": 0
  }, {
    "0": 1450051200000,
    "1": 0
  }, {
    "0": 1450656000000,
    "1": 0
  }, {
    "0": 1451260800000,
    "1": 0
  }, {
    "0": 1451865600000,
    "1": 0
  }, {
    "0": 1452470400000,
    "1": 0
  }, {
    "0": 1453075200000,
    "1": 0
  }, {
    "0": 1453680000000,
    "1": 1
  }, {
    "0": 1454284800000,
    "1": 0
  }],
  "idx": 2
}, {
  "label": "New Request (Regular Report)",
  "data": [{
    "0": 1446422400000,
    "1": 4
  }, {
    "0": 1447027200000,
    "1": 0
  }, {
    "0": 1447632000000,
    "1": 2
  }, {
    "0": 1448236800000,
    "1": 2
  }, {
    "0": 1448841600000,
    "1": 0
  }, {
    "0": 1449446400000,
    "1": 1
  }, {
    "0": 1450051200000,
    "1": 0
  }, {
    "0": 1450656000000,
    "1": 0
  }, {
    "0": 1451260800000,
    "1": 1
  }, {
    "0": 1451865600000,
    "1": 1
  }, {
    "0": 1452470400000,
    "1": 0
  }, {
    "0": 1453075200000,
    "1": 3
  }, {
    "0": 1453680000000,
    "1": 2
  }, {
    "0": 1454284800000,
    "1": 0
  }],
  "idx": 3
}, {
  "label": "Other",
  "data": [{
    "0": 1446422400000,
    "1": 0
  }, {
    "0": 1447027200000,
    "1": 0
  }, {
    "0": 1447632000000,
    "1": 2
  }, {
    "0": 1448236800000,
    "1": 4
  }, {
    "0": 1448841600000,
    "1": 2
  }, {
    "0": 1449446400000,
    "1": 0
  }, {
    "0": 1450051200000,
    "1": 2
  }, {
    "0": 1450656000000,
    "1": 0
  }, {
    "0": 1451260800000,
    "1": 0
  }, {
    "0": 1451865600000,
    "1": 0
  }, {
    "0": 1452470400000,
    "1": 3
  }, {
    "0": 1453075200000,
    "1": 0
  }, {
    "0": 1453680000000,
    "1": 3
  }, {
    "0": 1454284800000,
    "1": 0
  }],
  "idx": 4
}, {
  "label": "Special Events",
  "data": [{
    "0": 1446422400000,
    "1": 0
  }, {
    "0": 1447027200000,
    "1": 0
  }, {
    "0": 1447632000000,
    "1": 0
  }, {
    "0": 1448236800000,
    "1": 1
  }, {
    "0": 1448841600000,
    "1": 0
  }, {
    "0": 1449446400000,
    "1": 3
  }, {
    "0": 1450051200000,
    "1": 1
  }, {
    "0": 1450656000000,
    "1": 0
  }, {
    "0": 1451260800000,
    "1": 0
  }, {
    "0": 1451865600000,
    "1": 0
  }, {
    "0": 1452470400000,
    "1": 0
  }, {
    "0": 1453075200000,
    "1": 0
  }, {
    "0": 1453680000000,
    "1": 0
  }, {
    "0": 1454284800000,
    "1": 0
  }],
  "idx": 5
}];

var ticks = [];
for (var i = 0; i < datasets[0].data.length; i++) {
  ticks.push(datasets[0].data[i][0]);
}

var options = {
  "legend": {
    "position": "ne",
    "noColumns": 6
  },
  "yaxis": {
    "min": 0
  },
  "xaxis": {
    "mode": "time",
    "timeformat": "%d %b",
    //    "tickSize": [7, "day"],
    ticks: ticks,
    "min": 1446163200000,
    "max": 1454544000000 // 1454284800000
  },
  "grid": {
    "clickable": true,
    "hoverable": true
  },
  "series": {
    "stack": true,
    "bars": {
      "show": true,
      "barWidth": 181440000.00000003,
      align: 'center'
    }
  }
};

$.plot($('#CAGraph'), datasets, options);

$("#CAGraph").bind("plothover", function(event, pos, item) {
  if (item) {
    // console.log(event);
    // console.log(pos);
    //console.log(item);
    var epoch = new Date(item.datapoint[0]);
    var percent = item.datapoint[1] - item.datapoint[2];
    $("#tooltip").html(
      moment(epoch).format("DD MMM") + " - " + item.series.label + " " + (percent) + "<br>Total: " + item.datapoint[1]).css({
      top: item.pageY - 25,
      left: item.pageX + 10
    }).fadeIn(200);
  } else {
    $("#tooltip").hide();
  }
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://rawgit.com/flot/flot/master/jquery.flot.js"></script>
<script src="https://rawgit.com/Codicode/flotanimator/master/jquery.flot.animator.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.time.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.stack.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot.tooltip/0.8.5/jquery.flot.tooltip.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>

<div id="choices_CAGraph"></div>
<div id="CAGraph" style="width:910px;height:400px"></div>
<div id='tooltip' style="position:absolute;display:none"></div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35337551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档