完整code<ul>
<!-- TODO:待补充代码 在下面的 li 标签中完成 12个月份 (即 monthList) 的渲染 -->
<li v-for="item,index in monthList" @click="setCurrentMonth(item,index)" :class="{'active':item == currentMonth}">
{{item}}
</li>
</ul>
</div>
<div class="chart">
<!-- TODO:待补充代码 -->
<!-- currentMonth 未来七天和本月 tab 切换,只有当前月才显示 -->
<div id="currentMonth" v-show="isShow">
<div class="title">
<h3>{{typeTitle}}</h3>
<div class="type">
<span id="seven" @click="changeStyle('未来7天天气')" :class="{active:typeTitle=='未来7天天气'}">未来7天</span>
<span id="current" @click="changeStyle('本月天气')" :class="{active:typeTitle=='本月天气'}" >本月</span >
</div>
</div>
</div>
<div id="chart"></div>
<script>
// TODO:待补充代码
var vm = new Vue({
el: "#app",
data: {
chart: null, // 图表
chartOptions: null, // 图表配置项
typeTitle: "本月天气",
monthList: {
January: "1月",
February: "2月",
March: "3月",
April: "4月",
May: "5月",
June: "6月",
July: "7月",
August: "8月",
September: "9月",
October: "10月",
November: "11月",
December: "12月",
},
currentMonth: "1月",
dataList: {},
date: new Date(),
isShow: false,
},
mounted: function () {
// 初始化 echarts
this.$nextTick(() => {
this.initChart();
});
axios.get("./js/weather.json").then((res) => {
// this.dataList = res.data;
for (let i = 0; i < res.data.length; i++) {
const element = res.data[i];
this.dataList[Object.keys(element)[0]] = Object.values(element)[0];
}
this.setCurrentMonth(this.currentMonth, "January");
});
},
methods: {
initChart() {
// 初始化图表
this.chart = echarts.init(document.getElementById("chart"));
// 配置项 去网站上看,这个地方就不写了会导致文档太长难以观看
this.chartOptions = {};
// 调用此方法设置 echarts 数据
this.chart.setOption(this.chartOptions);
},
setCurrentMonth(data, index) {
this.currentMonth = data;
let len = this.dataList[index].length;
let xdata = [];
for (let i = 1; i <= len; i++) xdata.push(i);
let sdata = this.dataList[index];
this.chartOptions.xAxis[0].data = xdata;
this.chartOptions.series[0].data = sdata;
this.chart.setOption(this.chartOptions);
this.isShow = data == this.date.getMonth() + 1 + "月";
},
changeStyle(data) {
this.typeTitle = data;
let aim;
for (const key in this.monthList) {
if (this.monthList[key] == this.currentMonth) aim = key;
}
let len = this.dataList[aim].length;
if (data == "本月天气") {
let xdata = [];
for (let i = 1; i <= len; i++) xdata.push(i);
let sdata = this.dataList[aim];
this.chartOptions.xAxis[0].data = xdata;
this.chartOptions.series[0].data = sdata;
this.chart.setOption(this.chartOptions);
} else {
let currentDate = this.date.getDate();
let currentmonth = this.date.getMonth() + 1;
let xdata = [], sdata = [];
if (currentDate + 7 <= len) {
for (let i = 0; i < 7; i++) {
xdata.push(`${currentmonth}/${currentDate + i}`);
sdata.push(this.dataList[aim][currentDate + i - 1]);
}
} else {
for (let i = 0; i < len - currentDate + 1; i++) {
xdata.push(`${currentmonth}/${currentDate + i}`);
sdata.push(this.dataList[aim][currentDate + i - 1]);
}
currentmonth++;
for (const key in this.monthList) {
if (this.monthList[key] == currentmonth + "月") aim = key;
}
for (let i = 0; xdata.length < 7; i++) {
xdata.push(`${currentmonth}/${i + 1}`);
sdata.push(this.dataList[aim][i]);
}
}
this.chartOptions.xAxis[0].data = xdata;
this.chartOptions.series[0].data = sdata;
this.chart.setOption(this.chartOptions);
}
},
},
});
</script>