Ajax渲染+图片服务器
let mockURL = "https://www.fastmock.site/mock/3673f58d6ada912cec83594fb7fee88a/fitness";
let currentMonth = "6月"; //默认月份
// Echart自适配
window.addEventListener("resize", function(event) {
myChart.resize();
myChart_2.resize();
myChart_3.resize();
myChart_4.resize();
})
$(document).click(function() {
$(".choice").hide();
})
$(".month").tap(function(event) {
event.stopPropagation();
$(".choice").toggle();
})
// 第二个月份切换
$(document).click(function() {
$(".choice-2").hide();
})
$(".month-2").tap(function(event) {
event.stopPropagation();
$(".choice-2").toggle();
})
// 第三个月份切换
$(document).click(function() {
$(".choice-3").hide();
})
$(".month-3").tap(function(event) {
event.stopPropagation();
$(".choice-3").toggle();
})
// 第一个Ecahrts图表
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;
$(function() {
let _currentMonth = sessionStorage.getItem("currentMonth") || "6月";
// 确认当前是否有默认值,有就选中
$(".choice li").each(function(index, el) {
if ($(el).text() == _currentMonth) {
$(".month").text(_currentMonth);
}
})
// 首次进入加载页面
updateDate(_currentMonth);
// 点击li切换月份
$(".choice li").tap(function() {
currentMonth = $(this).text()
console.log(currentMonth);
$(".month").text(currentMonth);
$(".choice").hide();
updateDate(currentMonth);
sessionStorage.setItem("currentMonth", currentMonth);
})
})
function updateDate(currentMonth) {
$.ajax({
url: mockURL + "/api/chart_data",
type: "post",
data: {
month: currentMonth
},
success(res) {
myData = res.data.list[0];
console.log(myData);
console.log(myData.date);
console.log("2020", myData.data_2020);
console.log("2021", myData.data_2021);
console.log("2022", myData.data_2022);
console.log(myChart);
// 第一个Ecahrt表格
option = {
color: ['#00DDFF', '#37A2FF', '#FFBF00'],
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
},
textStyle: {
fontSize: 16,
}
},
legend: {
data: ['2020', '2021', '2022']
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: '1%',
right: '2%',
bottom: '0%',
containLabel: true
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisTick: {
show: false, //刻度线
},
axisLabel: {
inside: false,
textStyle: {
color: '#000',
fontSize: '14', //调节字体
itemSize: ''
}
},
data: myData.date
}],
yAxis: [{
type: 'value',
axisLine: {
show: false, //隐藏y轴
},
axisLabel: {
show: false, //隐藏刻度值
},
}],
series: [{
name: '2020',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(128, 255, 165)'
},
{
offset: 1,
color: 'rgb(1, 191, 236)'
}
])
},
emphasis: {
focus: 'series'
},
data: myData.data_2020
},
{
name: '2021',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(55, 162, 255)'
},
{
offset: 1,
color: 'rgb(116, 21, 219)'
}
])
},
emphasis: {
focus: 'series'
},
data: myData.data_2021
},
{
name: '2022',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
label: {
show: true,
position: 'top',
},
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(255, 191, 0)'
},
{
offset: 1,
color: 'rgb(224, 62, 76)'
}
])
},
emphasis: {
focus: 'series'
},
data: myData.data_2022
}
]
};
option && myChart.setOption(option);
// 显示你的体重
let sum = 0;
myData.data_2022.forEach((item) => {
sum += item;
})
$(".weight").text(parseInt(sum / myData.data_2022.length));
// 显示锻炼天数+显示增加体重
if (currentMonth == "6月") {
$(".sumDay").text(`448`);
$(".sumWeight").text(`8`);
} else if (currentMonth == "7月") {
$(".sumDay").text(`478`);
$(".sumWeight").text(`6`);
} else {
$(".sumDay").text(`515`);
$(".sumWeight").text(`2`);
}
//输入值
$(".cancel-btn").tap(function() {
$(".mask").css("display", "none");
$(".form-box").css("display", "none");
});
$(".confirm-btn").tap(function() {
$(".inputWeight").val("kg");
$(".mask").css("display", "none");
$(".form-box").css("display", "none");
});
}
})
}
// 轮播图
let orderSwiper = new Swiper('.swiper', {
on: {
slideChangeTransitionEnd: function(swiper) {
console.log(this.activeIndex);
updateNavStyle(this.activeIndex);
},
},
})
document.querySelector('#nav').onclick = function(event) {
Array.from(this.children).forEach(function(item, index) {
if (item.classList.contains('active')) {
item.classList.remove('active');
}
});
if (event.target.nodeName == 'DIV') {
event.target.classList.add('active');
let index = event.target.getAttribute('data-index');
//swiper提供的根据索引切换
orderSwiper.slideTo(index);
}
}
function updateNavStyle(slideActiveIndex) {
let navItems = document.querySelectorAll("#nav div");
for (let i = 0; i < navItems.length; i++) {
let dataIndex = navItems[i].getAttribute('data-index');
if (slideActiveIndex == dataIndex) {
navItems[i].classList.add('active');
} else {
navItems[i].classList.remove('active');
}
}
}
// 第二部分:
// 点击li切换月份
$(".choice-2 li").tap(function() {
currentMonth_calo = $(this).text()
console.log(currentMonth_calo);
$(".month-2").text(currentMonth_calo);
$(".choice-2").hide();
})
//第二个图表
let chartDom_2 = document.getElementById('main_2');
let myChart_2 = echarts.init(chartDom_2);
let option_2;
option_2 = {
color: ['#FFBF00'],
tooltip: {
formatter: '{a} <br/>{b} : {c}' //表盘显示格式
},
series: [{
name: '卡路里消耗',
type: 'gauge',
progress: {
show: true
},
min: 0, // 最小的数据值,默认 0 。映射到 minAngle。
max: 2500, // 最大的数据值,默认 100 。映射到 maxAngle。
detail: {
valueAnimation: true,
formatter: '{value}'
},
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(255, 191, 0)'
},
{
offset: 1,
color: 'rgb(224, 62, 76)'
}
])
},
data: [{
value: `1980`,
// name: 'Cal'
}]
}]
};
option_2 && myChart_2.setOption(option_2);
// 第三个echart图表
var chartDom_3 = document.getElementById('main_3');
var myChart_3 = echarts.init(chartDom_3);
var option_3;
option_3 = {
color: ['#00DDFF', '#37A2FF', '#FFBF00'],
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
},
textStyle: {
fontSize: 20,
}
},
legend: {
data: ['2020', '2021', '2022']
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: '1%',
right: '2%',
bottom: '0%',
containLabel: true
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisTick: {
show: false, //刻度线
},
axisLabel: {
inside: false,
textStyle: {
color: '#000',
fontSize: '14', //调节字体
itemSize: ''
}
},
data: ['06/01', '06/05', '06/10', '06/15', '06/20', '06/25', '06/30']
}],
yAxis: [{
type: 'value',
axisLine: {
show: false, //隐藏y轴
},
axisLabel: {
show: false, //隐藏刻度值
},
}],
series: [{
name: '2022',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
label: {
show: true,
position: 'top',
},
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(255, 191, 0)'
},
{
offset: 1,
color: 'rgb(224, 62, 76)'
}
])
},
emphasis: {
focus: 'series'
},
data: [1982, 2315, 1876, 1980, 2368, 1500, 2567]
}]
};
option_3 && myChart_3.setOption(option_3);
// 第四部分
// 切换月份
$(".choice-3 li").tap(function() {
currentMonth_size = $(this).text()
console.log(currentMonth_size);
$(".month-3").text(currentMonth_size);
$(".choice-3").hide();
})
//输入值
$(".cancel-btn").tap(function() {
$(".mask").css("display", "none");
$(".form-box-3").css("display", "none");
});
$(".confirm-btn").tap(function() {
$(".input_size").val("cm");
$(".mask").css("display", "none");
$(".form-box-3").css("display", "none");
});
// 第四个Ecahrt图表
var chartDom_4 = document.getElementById('main_4');
var myChart_4 = echarts.init(chartDom_4);
var option_4;
option_4 = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985'
}
},
textStyle: {
fontSize: 20,
}
},
legend: {
data: ['2020', '2021', '2022']
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: '1%',
right: '2%',
bottom: '0%',
containLabel: true
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisTick: {
show: false, //刻度线
},
axisLabel: {
inside: false,
textStyle: {
color: '#000',
fontSize: '14', //调节字体
itemSize: ''
}
},
data: ['06/01', '06/05', '06/10', '06/15', '06/20', '06/25', '06/30']
}],
yAxis: [{
type: 'value',
axisLine: {
show: false, //隐藏y轴
},
axisLabel: {
show: false, //隐藏刻度值
},
}],
series: [{
name: '胸围',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(128, 255, 165)'
},
{
offset: 1,
color: 'rgb(1, 191, 236)'
}
])
},
emphasis: {
focus: 'series'
},
data: [18, 25, 16, 19, 17, 29, 20]
},
{
name: '腰围',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(255, 191, 0)'
},
{
offset: 1,
color: 'rgb(224, 62, 76)'
}
])
},
emphasis: {
focus: 'series'
},
data: [27, 15, 32, 15, 29, 18, 22]
},
{
name: '臀围',
type: 'line',
stack: 'Total',
smooth: true,
lineStyle: {
width: 0
},
showSymbol: false,
label: {
show: true,
position: 'top',
},
areaStyle: {
opacity: 0.8,
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(55, 162, 255)'
},
{
offset: 1,
color: 'rgb(116, 21, 219)'
}
])
},
emphasis: {
focus: 'series'
},
data: [17, 25, 15, 27, 21, 25, 18]
}
]
};
option_4 && myChart_4.setOption(option_4);
// 遮罩层-1
$(".menu").tap(function() {
$(".mask").css("display", "block");
$(".form-box").css("display", "block");
})
$(".mask").tap(function() {
$(".mask").css("display", "none");
$(".form-box").css("display", "none");
});
// 遮罩层-3
$(".menu-3").tap(function() {
$(".mask").css("display", "block");
$(".form-box-3").css("display", "block");
})
$(".mask").tap(function() {
$(".mask").css("display", "none");
$(".form-box-3").css("display", "none");
});