[英]Flot bar “tool-tip” graph with codeigniter
I am using flot bar graph with my codeigniter project. 我在我的codeigniter项目中使用了flt条形图。
The way my graph works is I count the number of users joined by month and then I echo count to the view by $data
. 我的图表的工作方式是,我按月计算加入的用户数,然后按
$data
计数回显到视图。
Question: When I hover over each bar I would like to be able to have a bootstrap tool tip show with count belonging to that month if that is possible. 问题:当我将鼠标悬停在每个栏上时,如果可能的话,我希望能够显示带有该月计数的引导工具提示 。
Script On welcome_message view 脚本在welcome_message视图上
<script type="text/javascript">
var bar_data = {
data: [
["January", <?php echo $count_jan;?>],
["February", <?php echo $count_feb;?>],
["March", <?php echo $count_mar;?>],
["April", <?php echo $count_apr;?>],
["May", <?php echo $count_may;?>],
["June", <?php echo $count_jun;?>],
["July", <?php echo $count_july;?>],
["August", <?php echo $count_aug;?>],
["September", <?php echo $count_sept;?>],
["October", <?php echo $count_oct;?>],
["November", <?php echo $count_nov;?>],
["December", <?php echo $count_dec;?>]
],
color: "#3c8dbc"
};
$.plot("#bar-chart", [bar_data], {
grid: {
borderWidth: 1,
borderColor: "#f3f3f3",
tickColor: "#f3f3f3"
},
points: {
show: true
},
series: {
bars: {
show: true,
fill: true,
barWidth: 0.4,
align: "center"
}
},
xaxis: {
mode: "categories",
tickLength: 0
}
});
</script>
Example 例
public function count_jan() {
$this->db->where('month_joined_up', 'January');
$query = $this->db->get('user');
if ($query->num_rows() > 0) {
return $query->num_rows();
} else {
return 0;
}
}
Controller 调节器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$data['count_jan'] = $this->count_jan();
$this->load->view('welcome_message', $data);
}
public function count_jan() {
$this->db->where('month_joined_up', 'January');
$query = $this->db->get('user');
if ($query->num_rows() > 0) {
return $query->num_rows();
} else {
return 0;
}
}
}
The easiest way to do this is to add the Flot Tooltip Plugin to your page and add this to your options object: 最简单的方法是将Flot Tooltip插件添加到页面并将其添加到options对象:
grid: {
hoverable: true,
...
},
tooltip: {
show: true,
content: '%y new users in %x',
cssClass: 'name of the class you want to use for the tooltip' // optional
}
Thanks for @Raidri for giving me a idea on tool tips for my flot bar graph I now got it working with code below 感谢@Raidri给我关于浮动条形图的工具提示的想法,现在我可以在下面的代码中使用它
<script type="text/javascript">
$( document ).ready(function() {
$('#bar-chart').bind('plothover', function(event, pos, item) {
$('.tooltip').remove();
if (item) {
$('<div id="tooltip" class="tooltip top in"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + item.datapoint[1].toFixed(2) + '</div></div>').prependTo('body');
$('#tooltip').css({
position: 'absolute',
left: item.pageX - ($('#tooltip').outerWidth() / 2),
top: item.pageY - $('#tooltip').outerHeight(),
pointer: 'cusror'
}).fadeIn('slow');
$('#bar-chart').css('cursor', 'pointer');
} else {
$('#bar-chart').css('cursor', 'auto');
}
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.