echarts+thinkphp+sql實現的篩選時間,分組,對應在日曆中顯示該月份的對應天數巡查次數。
阿新 • • 發佈:2019-01-23
Array.prototype.unique = function(){
this.sort(); //先排序
var res = [this[0]];
for(var i = 1; i < this.length; i++){
if(this[i] !== res[res.length - 1]){
res.push(this[i]);
}
}
return res;
}
var leixin = null;
var s_year = (new Date()).getFullYear();
var s_month = (new Date()).getMonth()+1;
var s_groupid= -1;
var thisyear=null;
var thismonth=null;
var mydate=new Date();
thisyear=s_year;
thismonth=s_month;
var thisday=mydate.getDate();
var mydate1=new Date();
var thisyear1=mydate1.getFullYear();
var thismonth1=mydate1.getMonth()+1;
var thisday1=mydate1.getDate();
var selectday=thisday;
//標記日期
var indate=thisday;
//入住日期
var inmonth=thismonth;
//入住月份
var outdate=thisday+1;
//退房日期
var outmonth=thismonth;
//退房月份
var datetxt="datetoday";
var datefirst;
var datesecond;
var year_bool = false;
var group_bool = false;
/**
* 建立前十年
* */
function getYears(){
var date = new Date();
var noewYear = date.getFullYear();
var mm = [];
for(var i = 0 ; i < 10; i++){
var newyear = noewYear - i;
var mp = {
"id":i,
"text":newyear
}
mm.push(mp);
}
$("#select_year").combobox({
data:mm,
onSelect:function(record){
year_bool = true;
group_bool = false;
s_year = record.text;
if(s_groupid==-1){
getYearData(s_year);
thisyear = s_year
}else{
$('#group').combobox('setValue','-1');
getYearData(s_year);
thisyear = s_year
}
},
valueField:'id',
textField:'text'
});
}
/**
* 建立OPtion
* @param value
* @returns {*}
*/
function createSOption(value) {
var option = document.createElement("option");
option.value = value;
option.innerHTML = value;
return option;
}
function getYearData(year){
$.getJSON("__URL__/getDataFromYear_data",{"year":year},function(result){
var res = eval(result);
var sum=0;
for(var i=1;i<=res.length;i++){
sum+=res[i-1];
}
$('#group_number').text("總巡查頻數"+sum+"次");
var data_number = res;
var M_json = ["12","11","10","09","08","07","06","05","04","03","02","01"];
var option = createFutureOptionAll(M_json,data_number);
leixin.setOption(option);
})
}
function setTdValue(k,kv){
$("#data_table tbody td").each(function(){
var tb_v= this.innerHTML;
if(k==tb_v){
this.innerHTML = tb_v+"<span style='color: red; position: absolute; right: 0px;top: 0;color: #fff; height: 30px; width: 30px; display: block; line-height: 30px; text-align: ce nter; border-radius: 100px; background: #ff9900; font-size: 18px;'>"+kv+"</span>";
}
})
}
function getCount(arr,str){
var str_count = 0;
for(var i = 0; i <arr.length; i++) {
if (arr[i] == str) {
str_count = str_count + 1;
}
}
return str_count;
}
function string2date(str){
return new Date(Date.parse(str.replace(/-/g, "/")));
}
function convertDateFromString(dateString) {
if (dateString) {
var arr1 = dateString.split(" ");
var sdate = arr1[0].split('-');
var date = new Date(sdate[0], sdate[1]-1, sdate[2]);
return date;
}
}
$(function(){
year_bool = true;
group_bool = false;
initeMyData();
leixin = echarts.init(document.getElementById('main'));
//初始化年
getYears();
//獲取寬度
var width = parseInt($('#tyb-shuju').get(0).offsetWidth);
//將寬度賦值給檢視
document.getElementById('main').style.width = width;
$('#group').combobox({
url:'__URL__/group',
valueField:'id',
textField:'text',
});
$("#group").combobox({
onSelect: function () {
group_bool = true;
year_bool = false;
s_groupid = $('#group').combobox('getValue');
if(s_groupid==-1){
getYearData(s_year);
}else{
$.getJSON("__URL__/getDataFromYear_group",{"groupid":s_groupid,"year":s_year},function(result){
var res = eval(result);
var sum=0;
for(var i=1;i<=res.length;i++){
sum+=res[i-1];
}
$('#group_number').text("該組本年巡查頻數"+sum+"次");
var data_number = res;
var M_json = ["12","11","10","09","08","07","06","05","04","03","02","01"];
var option = createFutureOptionAll(M_json,data_number.reverse(data_number));
leixin.setOption(option);
})
}
}
});
getYearData((new Date()).getFullYear());
clickRightCaladar();
$('#group').combobox("setValue","-1");
leixin.on('click', function (params) {
var month = params.name;
s_month = parseInt(month);
jQuery(".selectdate").val(s_year+"年"+s_month+"月");
thisyear = s_year;
thismonth = s_month;
var groupid = $('#group').combobox('getValue');
initdata();
if(year_bool){
$.getJSON("__URL__/getDataFromYearAndMonth",{"year":s_year,"month":month,"groupid":groupid},function(result){
var res = eval(result);
var date_all = [];
for(var i =0 ;i < res.length; i ++){
var row_date = convertDateFromString(res[i]["uploaddate"]);
date_all.push(row_date.getDate());
}
var uniq_arr = date_all.unique();
var last_arr = [];
for(var j = 0 ; j < uniq_arr.length; j++){
var mp = {
"date":uniq_arr[j],
"num":getCount(date_all,uniq_arr[j])
}
last_arr.push(mp);
}
for(var x = 0 ;x< last_arr.length; x++ ){
var r = last_arr[x];
setTdValue(r['date'], r["num"]);
}
})
}
if(group_bool){
$.getJSON("__URL__/getDataFromYearAndMonth",{"year":s_year,"month":month,"groupid":groupid},function(result){
var res = eval(result);
var date_all = [];
for(var i =0 ;i < res.length; i ++){
var row_date = convertDateFromString(res[i]["uploaddate"]);
date_all.push(row_date.getDate());
}
var uniq_arr = date_all.unique();
var last_arr = [];
for(var j = 0 ; j < uniq_arr.length; j++){
var mp = {
"date":uniq_arr[j],
"num":getCount(date_all,uniq_arr[j])
}
last_arr.push(mp);
}
for(var x = 0 ;x< last_arr.length; x++ ){
var r = last_arr[x];
setTdValue(r['date'], r["num"]);
}
})
}
});
//設定點選事件
});
function initeMyData() {
jQuery(".selectdate").val(s_year + "年" + s_month + "月");
s_month = parseInt(s_month);
s_month = s_month < 10 ? "0" + s_month : s_month;
thisyear = s_year;
thismonth = s_month;
initdata();
$.getJSON("__URL__/getDataFromYearAndMonth", {
"year": s_year,
"month": s_month,
"groupid": "-1"
}, function (result) {
var res = eval(result);
var date_all = [];
for (var i = 0; i < res.length; i++) {
var row_date = convertDateFromString(res[i]["uploaddate"]);
date_all.push(row_date.getDate());
}
var uniq_arr = date_all.unique();
var last_arr = [];
for (var j = 0; j < uniq_arr.length; j++) {
var mp = {
"date": uniq_arr[j],
"num": getCount(date_all, uniq_arr[j])
}
last_arr.push(mp);
}
for (var x = 0; x < last_arr.length; x++) {
var r = last_arr[x];
setTdValue(r['date'], r["num"]);
}
})
}
function orderabledate(thisyear,thismonth,thisday){
//能預訂的日期
if(thisyear<thisyear1){
jQuery(".data_table tbody td").addClass("orderdate");
jQuery(".data_table tbody td").removeClass("usedate");
}else if(thisyear==thisyear1){
if(thismonth<thismonth1){
jQuery(".data_table tbody td").addClass("orderdate");
jQuery(".data_table tbody td").removeClass("usedate");
}else if(thismonth==thismonth1){
for(var j=0;j<6;j++){
for(var i=0;i<7;i++){
var tdhtml=jQuery(".data_table tbody tr").eq(j).find("td").eq(i).html();
if(tdhtml<thisday){
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).addClass("orderdate");
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).removeClass("usedate");
}else{
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).removeClass("orderdate");
}
}
}
}else{
jQuery(".data_table tbody td").removeClass("orderdate");
}
}else{
jQuery(".data_table tbody td").removeClass("orderdate");
}
}
function markdate(thisyear,thismonth,thisday){
//標記日期
var datetxt=thisyear+"年"+thismonth+"月";
var thisdatetxt=thisyear1+"年"+thismonth1+"月";
jQuery(".data_table td").removeClass("tdselect");
if(datetxt==thisdatetxt){
for(var j=0;j<6;j++){
for(var i=0;i<7;i++){
var tdhtml=jQuery(".data_table tbody tr").eq(j).find("td").eq(i).html();
if(tdhtml==thisday){
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).addClass("tdselect");
}
}
}
}
}
function initdata(){
//日期初始填充
var tdheight=jQuery(".data_table tbody tr").eq(0).find("td").height();
jQuery(".data_table tbody td").css("height",tdheight);
jQuery(".selectdate").val(thisyear+"年"+thismonth+"月");
var days=getdaysinonemonth(thisyear,thismonth);
var weekday=getfirstday(thisyear,thismonth);
setcalender(days,weekday);
markdate(thisyear,thismonth,selectday);
orderabledate(thisyear,thismonth,thisday);
}
function getdaysinonemonth(year,month){
//算某個月的總天數
month=parseInt(month,10);
var d=new Date(year,month,0);
return d.getDate();
}
function getfirstday(year,month){
//算某個月的第一天是星期幾
month=month-1;
var d=new Date(year,month,1);
return d.getDay();
}
function setcalender(days,weekday){
//往日曆中填入日期
var a=1;
for(var j=0;j<6;j++){
for(var i=0;i<7;i++){
if(j==0&&i<weekday){
jQuery(".data_table tbody tr").eq(0).find("td").eq(i).html("");
jQuery(".data_table tbody tr").eq(0).find("td").eq(i).removeClass("usedate");
}else{
if(a<=days){
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).html(a);
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).addClass("usedate");
a++;
}else{
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).html("");
jQuery(".data_table tbody tr").eq(j).find("td").eq(i).removeClass("usedate");
a=days+1;
}
}
}
}
}
function errorreset(){
//日期報錯後,資料重置
thisyear=thisyear1;
thismonth=thismonth1;
thisday=thisday1;
selectday=thisday1;
indate=thisday1;
inmonth=thismonth1;
outdate=thisday1+1;
outmonth=thismonth1;
initdata();
}
function clickRightCaladar(){
initdata();
jQuery(".datetoday").val(thisyear+"-"+thismonth+"-"+thisday);
jQuery(".dateendday").val(thisyear+"-"+thismonth+"-"+(thisday+1));
jQuery(".data_table tbody td").live("click",function(){ //點選日期的效果
var thishtml=parseInt(jQuery(this).html());
jQuery(".data_table td").removeClass("tdselect");
jQuery(this).addClass("tdselect");
selectday=thishtml;
if(datetxt=="datetoday"){
jQuery(".datetoday").val(thisyear+"-"+thismonth+"-"+selectday);
indate=selectday;
inmonth=thismonth;
}else{
jQuery(".dateendday").val(thisyear+"-"+thismonth+"-"+selectday);
outdate=selectday;
outmonth=thismonth;
if(outmonth<inmonth){
alert("日期填寫錯誤");
jQuery(".datetoday").val(thisyear1+"-"+thismonth1+"-"+thisday1);
jQuery(".dateendday").val(thisyear1+"-"+thismonth1+"-"+(thisday1+1));
errorreset();
}else if(outmonth==inmonth){
if(outdate<=indate){
alert("日期填寫錯誤");
jQuery(".datetoday").val(thisyear1+"-"+thismonth1+"-"+thisday1);
jQuery(".dateendday").val(thisyear1+"-"+thismonth1+"-"+(thisday1+1));
errorreset();
}
}
}
});
jQuery(".datetoday").click(function(){
//選擇入住日期
datetxt="datetoday";
jQuery(".datetext").removeClass("inputdate");
jQuery(this).addClass("inputdate");
});
jQuery(".lastmonth").click(function(){
//上一個月
thismonth--;
if(thismonth==0){
thismonth=12;
thisyear--;
}
initdata();
});
jQuery(".nextmonth").click(function(){
//上一個月
thismonth++;
if(thismonth==13){
thismonth=1;
thisyear++;
}
initdata();
});
jQuery(".btsure").click(function(){
//確定日期
var start = new Date($(".datetoday").val());
var end = new Date($(".dateendday").val());
var diff = parseInt((end - start) / (1000*3600*24));
jQuery(".bookdate").html(inmonth+"月"+indate+"日至"+outmonth+"月"+outdate+"日("+diff+")晚")
});
jQuery(".caltline1").toggle(
function(){
//jQuery(".caltline2").slideDown(500);
jQuery(".calender").fadeIn(500);
errorreset();
jQuery(".caltline1").find("img").attr("src","images/iconpointup.png");
},
function(){
//jQuery(".caltline2").slideUp(500);
jQuery(".calender").fadeOut(500);
jQuery(".caltline1").find("img").attr("src","images/iconpoint.png");
}
);
}
function createFutureOptionAll(M_json,data_number){
// 基於準備好的dom,初始化echarts例項
var option = {
tooltip : {
trigger: 'axis',
axisPointer : { // 座標軸指示器,座標軸觸發有效
type : 'shadow' // 預設為直線,可選為:'line' | 'shadow'
}
},
// color:['#f8b22f','#eb3524','#88d431','#00f'],
color:['#3398db'],
legend: {
data: ['']
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: M_json,
},
series: [
{
name: '日常巡查頻次統計',
type: 'bar',
stack: '總量',
label: {
normal: {
show: true,
position: 'insideRight'
}
},
data: data_number
},
]
};
return option;
}
var myCalendar = new SimpleCalendar('#calendar');