'commit'
This commit is contained in:
@@ -38,7 +38,7 @@
|
||||
margin-bottom: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
.controls input {
|
||||
.controls select, .controls input {
|
||||
padding: 8px 12px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
@@ -51,6 +51,7 @@
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
margin-right: 10px;
|
||||
}
|
||||
.controls button:hover {
|
||||
background-color: #66b1ff;
|
||||
@@ -60,6 +61,31 @@
|
||||
color: #666;
|
||||
margin: 20px 0;
|
||||
}
|
||||
.tabs {
|
||||
display: flex;
|
||||
margin-bottom: 20px;
|
||||
border-bottom: 1px solid #ddd;
|
||||
}
|
||||
.tab {
|
||||
padding: 10px 20px;
|
||||
cursor: pointer;
|
||||
background-color: #f5f7fa;
|
||||
border: 1px solid #ddd;
|
||||
border-bottom: none;
|
||||
border-radius: 4px 4px 0 0;
|
||||
margin-right: 5px;
|
||||
}
|
||||
.tab.active {
|
||||
background-color: #fff;
|
||||
border-bottom: 1px solid #fff;
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
.tab-content {
|
||||
display: none;
|
||||
}
|
||||
.tab-content.active {
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -69,39 +95,127 @@
|
||||
</div>
|
||||
|
||||
<div class="chart-container">
|
||||
<h2>柱状图示例</h2>
|
||||
<div class="controls">
|
||||
<input type="text" id="chartTitle" placeholder="输入图表标题" value="Bar-MarkLine(自定义)">
|
||||
<button id="loadChart">加载图表</button>
|
||||
<div class="tabs">
|
||||
<div class="tab active" data-tab="population">人口数据图表</div>
|
||||
<div class="tab" data-tab="urbanization">城镇化率图表</div>
|
||||
<div class="tab" data-tab="demo">示例图表</div>
|
||||
</div>
|
||||
|
||||
<div class="tab-content active" id="population-tab">
|
||||
<h2>云南省各州市人口分布</h2>
|
||||
<div class="controls">
|
||||
<select id="yearSelect">
|
||||
<option value="2020">2020年</option>
|
||||
<option value="2021">2021年</option>
|
||||
<option value="2022">2022年</option>
|
||||
<option value="2023">2023年</option>
|
||||
<option value="2024" selected>2024年</option>
|
||||
</select>
|
||||
<button id="loadPopulationChart">加载图表</button>
|
||||
</div>
|
||||
<div id="populationChart" class="chart"></div>
|
||||
<div class="message" id="populationMessage">点击"加载图表"按钮获取数据</div>
|
||||
</div>
|
||||
|
||||
<div class="tab-content" id="urbanization-tab">
|
||||
<h2>云南省各州市城镇化率变化趋势</h2>
|
||||
<div class="controls">
|
||||
<button id="loadUrbanizationChart">加载图表</button>
|
||||
</div>
|
||||
<div id="urbanizationChart" class="chart"></div>
|
||||
<div class="message" id="urbanizationMessage">点击"加载图表"按钮获取数据</div>
|
||||
</div>
|
||||
|
||||
<div class="tab-content" id="demo-tab">
|
||||
<h2>柱状图示例</h2>
|
||||
<div class="controls">
|
||||
<input type="text" id="chartTitle" placeholder="输入图表标题" value="Bar-MarkLine(自定义)">
|
||||
<button id="loadDemoChart">加载图表</button>
|
||||
</div>
|
||||
<div id="barChart" class="chart"></div>
|
||||
<div class="message" id="demoMessage">点击"加载图表"按钮获取数据</div>
|
||||
</div>
|
||||
<div id="barChart" class="chart"></div>
|
||||
<div class="message" id="message">点击"加载图表"按钮获取数据</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// 初始化图表
|
||||
var populationChart = echarts.init(document.getElementById('populationChart'));
|
||||
var urbanizationChart = echarts.init(document.getElementById('urbanizationChart'));
|
||||
var barChart = echarts.init(document.getElementById('barChart'));
|
||||
|
||||
// 显示加载中
|
||||
function showLoading() {
|
||||
barChart.showLoading();
|
||||
document.getElementById('message').textContent = '正在加载数据...';
|
||||
function showLoading(chart, messageElement) {
|
||||
chart.showLoading();
|
||||
messageElement.textContent = '正在加载数据...';
|
||||
}
|
||||
|
||||
// 隐藏加载中
|
||||
function hideLoading() {
|
||||
barChart.hideLoading();
|
||||
function hideLoading(chart) {
|
||||
chart.hideLoading();
|
||||
}
|
||||
|
||||
// 显示错误信息
|
||||
function showError(message) {
|
||||
document.getElementById('message').textContent = '错误: ' + message;
|
||||
function showError(messageElement, message) {
|
||||
messageElement.textContent = '错误: ' + message;
|
||||
console.error(message);
|
||||
}
|
||||
|
||||
// 加载图表数据
|
||||
function loadChartData() {
|
||||
showLoading();
|
||||
// 加载人口数据图表
|
||||
function loadPopulationChartData() {
|
||||
showLoading(populationChart, document.getElementById('populationMessage'));
|
||||
|
||||
// 获取选中的年份
|
||||
var year = document.getElementById('yearSelect').value;
|
||||
|
||||
// 调用后端接口获取数据
|
||||
fetch('/bigscreen/population/chart/' + year)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('网络响应异常');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
// 使用获取到的数据渲染图表
|
||||
populationChart.setOption(data);
|
||||
document.getElementById('populationMessage').textContent = '图表加载成功';
|
||||
})
|
||||
.catch(error => {
|
||||
showError(document.getElementById('populationMessage'), error.message);
|
||||
})
|
||||
.finally(() => {
|
||||
hideLoading(populationChart);
|
||||
});
|
||||
}
|
||||
|
||||
// 加载城镇化率图表
|
||||
function loadUrbanizationRateChartData() {
|
||||
showLoading(urbanizationChart, document.getElementById('urbanizationMessage'));
|
||||
|
||||
// 调用后端接口获取数据
|
||||
fetch('/bigscreen/population/urbanization')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('网络响应异常');
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
// 使用获取到的数据渲染图表
|
||||
urbanizationChart.setOption(data);
|
||||
document.getElementById('urbanizationMessage').textContent = '图表加载成功';
|
||||
})
|
||||
.catch(error => {
|
||||
showError(document.getElementById('urbanizationMessage'), error.message);
|
||||
})
|
||||
.finally(() => {
|
||||
hideLoading(urbanizationChart);
|
||||
});
|
||||
}
|
||||
|
||||
// 加载示例图表数据
|
||||
function loadDemoChartData() {
|
||||
showLoading(barChart, document.getElementById('demoMessage'));
|
||||
|
||||
// 获取图表标题
|
||||
var title = document.getElementById('chartTitle').value || 'Bar-MarkLine(自定义)';
|
||||
@@ -117,27 +231,49 @@
|
||||
.then(data => {
|
||||
// 使用获取到的数据渲染图表
|
||||
barChart.setOption(data);
|
||||
document.getElementById('message').textContent = '图表加载成功';
|
||||
document.getElementById('demoMessage').textContent = '图表加载成功';
|
||||
})
|
||||
.catch(error => {
|
||||
showError(error.message);
|
||||
showError(document.getElementById('demoMessage'), error.message);
|
||||
})
|
||||
.finally(() => {
|
||||
hideLoading();
|
||||
hideLoading(barChart);
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载完成后自动加载图表
|
||||
window.addEventListener('DOMContentLoaded', function() {
|
||||
// 绑定标签页切换事件
|
||||
document.querySelectorAll('.tab').forEach(tab => {
|
||||
tab.addEventListener('click', function() {
|
||||
// 移除所有标签页的active类
|
||||
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
|
||||
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
||||
|
||||
// 添加当前标签页的active类
|
||||
this.classList.add('active');
|
||||
document.getElementById(this.dataset.tab + '-tab').classList.add('active');
|
||||
|
||||
// 调整图表大小
|
||||
populationChart.resize();
|
||||
urbanizationChart.resize();
|
||||
barChart.resize();
|
||||
});
|
||||
});
|
||||
|
||||
// 绑定按钮点击事件
|
||||
document.getElementById('loadChart').addEventListener('click', loadChartData);
|
||||
document.getElementById('loadPopulationChart').addEventListener('click', loadPopulationChartData);
|
||||
document.getElementById('loadUrbanizationChart').addEventListener('click', loadUrbanizationRateChartData);
|
||||
document.getElementById('loadDemoChart').addEventListener('click', loadDemoChartData);
|
||||
|
||||
// 自动加载图表
|
||||
loadChartData();
|
||||
loadPopulationChartData();
|
||||
});
|
||||
|
||||
// 窗口大小变化时,重新调整图表大小
|
||||
window.addEventListener('resize', function() {
|
||||
populationChart.resize();
|
||||
urbanizationChart.resize();
|
||||
barChart.resize();
|
||||
});
|
||||
</script>
|
||||
|
Reference in New Issue
Block a user