1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| --- title: 数据可视化图表 avbrlink: charts date: 2024-01-01 00:00:00 type: page ---
<div class="echarts-container"> <h3>月访问量统计</h3> <div id="lineChart" class="chart"></div> <h3>文章分类统计</h3> <div id="barChart" class="chart"></div> <h3>访问来源分布</h3> <div id="pieChart" class="chart"></div> </div>
<style> .echarts-container { max-width: 1200px; margin: 0 auto; padding: 20px; }
.echarts-container h3 { color: #333; margin-top: 40px; margin-bottom: 20px; }
.chart { width: 100%; height: 400px; margin-bottom: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 10px; background-color: #fff; }
@media (max-width: 768px) { .chart { height: 300px; } } </style>
<script> // 等待页面加载完成 document.addEventListener('DOMContentLoaded', function() { // 初始化折线图 const lineChart = echarts.init(document.getElementById('lineChart')); lineChart.setOption({ title: { text: '博客月访问量趋势', left: 'center' }, tooltip: { trigger: 'axis' }, legend: { data: ['访问量', '独立访客'], bottom: 10 }, grid: { left: '3%', right: '4%', bottom: '15%', top: '15%', containLabel: true }, xAxis: { type: 'category', boundaryGap: false, data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'] }, yAxis: { type: 'value' }, series: [ { name: '访问量', type: 'line', stack: '总量', data: [1200, 1900, 3000, 5000, 8000, 10000, 12000, 14000, 16000, 18000, 20000, 22000], smooth: true, lineStyle: { width: 3, color: '#3498db' } }, { name: '独立访客', type: 'line', stack: '总量', data: [800, 1500, 2200, 3500, 5500, 7000, 8500, 10000, 11500, 13000, 14500, 16000], smooth: true, lineStyle: { width: 3, color: '#e74c3c' } } ] });
// 初始化柱状图 const barChart = echarts.init(document.getElementById('barChart')); barChart.setOption({ title: { text: '文章分类统计', left: 'center' }, tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, legend: { data: ['文章数量'], bottom: 10 }, grid: { left: '3%', right: '4%', bottom: '15%', top: '15%', containLabel: true }, xAxis: { type: 'category', data: ['技术博客', '生活随笔', '读书笔记', '教程分享', '经验总结', '其他'] }, yAxis: { type: 'value' }, series: [ { name: '文章数量', type: 'bar', data: [35, 12, 8, 15, 10, 5], itemStyle: { color: function(params) { const colorList = ['#3498db', '#2ecc71', '#e67e22', '#9b59b6', '#e74c3c', '#f39c12']; return colorList[params.dataIndex]; } }, label: { show: true, position: 'top', formatter: '{c}' } } ] });
// 初始化饼图 const pieChart = echarts.init(document.getElementById('pieChart')); pieChart.setOption({ title: { text: '访问来源分布', left: 'center' }, tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', left: 'left', bottom: 20, top: 'center' }, series: [ { name: '访问来源', type: 'pie', radius: ['40%', '70%'], avoidLabelOverlap: false, itemStyle: { borderRadius: 10, borderColor: '#fff', borderWidth: 2 }, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '20', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ { value: 45, name: '搜索引擎' }, { value: 25, name: '直接访问' }, { value: 15, name: '社交媒体' }, { value: 10, name: '外部链接' }, { value: 5, name: '其他' } ] } ] });
// 响应式调整 window.addEventListener('resize', function() { lineChart.resize(); barChart.resize(); pieChart.resize(); }); }); </script>
|