【Apache ECharts】<病虫害致粮食损失统计>


实现


1. 设置 div (块级盒子),设置 id 为 chart

html 复制代码
 <div id="chart"></div>

2. css设置样式位置

css 复制代码
<style>
        #main{
            width: 30%;
            height: 40vh;
            /* background-color: red; */
            min-height: 100px;
            min-width: 150px;
            margin-top: 150px;
        }
        body{
            background-image: url("img/地球.png");
            background-size: 100% 100vh;
        }
        #chart{
            width:30%;
            height:40vh;
            /* background-color: aquamarine; */
            margin-right: 50px;
            margin-left: 800px;
            min-height: 100px;
            min-width: 150px;
        }
    </style>

3. 基于准备好的dom,初始化echarts实例

javascript 复制代码
var Chart=echarts.init(document.getElementById('chart'));

4. 指定图表的配置项和数据

4.1 对标题进行设置

题目,字体颜色

javascript 复制代码
 title:{
                text:'病虫害致粮食损失统计',
                textStyle:{
                    color:'#fff'
                }
            },

4.2 X轴

设置类目轴,坐标轴两边留白,轴线颜色为白色

javascript 复制代码
xAxis: {
                type: 'category',
                boundaryGap: true,
                data: ['2017', '2018', '2019', '2020', '2021', '2022', '2023'],
                axisLine:{
                    lineStyle:{
                        color:'#fff'
                    }
                }
                
            },

4.3 Y轴

Y轴名字,数值轴,轴线颜色为白色,分隔线隐藏

javascript 复制代码
 yAxis: {
                name:'损失(万吨)',
                type: 'value',
                axisLine:{
                    show: true,
                    lineStyle:{
                        color:'#fff'
                    }
                },
                splitLine:false
            },

4.4 折线设置

折线节点数值,阴影颜色渐变(从0%到100%)

javascript 复制代码
series:[{
                data: [820, 932, 901, 934, 1290, 1330, 1320],
                type: 'line',
                areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                    offset: 0,
                    color: '#090e2a'
                },
                {
                    offset: 1,
                    color: '#487d8f'
                }
                    ])
                }
                }
            ],
  1. 使用刚指定的配置项和数据显示图表。
javascript 复制代码
Chart.setOption(option2);
  1. 给整个窗口绑事件,只要窗口尺寸有变化,就触发。
javascript 复制代码
window.onresize=function(){
            Chart.resize()
            
        }
相关推荐
SoaringHeart35 分钟前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
IT_陈寒2 小时前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰3 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
山河木马3 小时前
渲染管线-计算得到gl_Position(顶点着色器)之后续GPU流程
javascript·webgl·图形学
竹林8184 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花4 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12275 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪5 小时前
Vue3-生命周期
前端
莪_幻尘5 小时前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4536 小时前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端