vue 记账清单-饼图渲染

新建vue项目

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>记账清单</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        .container{
            width: 100%;
            height: 100vh;
        }
        .title{
            width: 100%;
            height: 60px;
            line-height: 60px;
        }
        .left{
            width: 50%;
            height: 90%;
            float: left;
        }
        .right{
            width: 49%;
            height: 90%;
            float: right;

        }
        .red{
            color: red;
        }

        .form {
            padding-bottom: 10px;
        }
        .form input[type=text]{
            width: 260px;
            height: 30px;
            line-height: 28px;
        }
        .form button{
            width: 180px;
            height: 35px;
            line-height: 34px;
            background-color: lightblue;
        }

        .echarts-box{
            width: 500px;
            height: 400px;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="container">
            <div class="title">
                <h2>记账清单</h2>
            </div>
            <div class="left">
                <div class="form">
                    消费名称:
                    <input type="text" placeholder="请输入消费名称" v-model.trim="name" @keyup.enter="btnAdd">
                    消费价格:
                    <input type="text" placeholder="请输入消费价格" v-model.number="price" @keyup.enter="btnAdd">
                    <button @click="btnAdd">添加账单</button>
                </div>
                <div class="table">
                    <table border="1" width="100%" style="text-align: center">
                        <thead>
                            <tr style="background-color: lightgrey">
                                <th>编号</th>
                                <th>消费名称</th>
                                <th>消费价格</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody v-if="list.length > 0">
                            <tr  v-for="(item,index) in list" :key="item.id">
                                <td>{{ index + 1 }}</td>
                                <td>{{ item.name }}</td>
                                <td :class="{ red : item.price > 500 }">{{ item.price.toFixed(2) }}</td>
                                <td><a href="#" @click="del(item.id)">删除</a></td>
                            </tr>
                        </tbody>
                        <tbody v-else>
                            <tr>
                                <td colspan="4">暂无数据</td>
                            </tr>
                        </tbody>
                        <tfoot v-if="list.length > 0">
                            <tr style="text-align: left">
                                <td colspan="4"><strong>消费总计:¥<span>{{ totalPrice.toFixed(2) }}</span></strong></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            <div class="right">
                <div class="echarts-box" id="main"></div>
            </div>
        </div>
    </div>
    <script src="../js/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://lib.baomitu.com/echarts/4.7.0/echarts.min.js"></script>
    <script>
        /*
        功能需求:
        1 基本渲染
            立刻发送请求获取数据 created
            拿到数据,存到data的响应数据中
            结合数据,进行渲染 v-for
            消费统计 使用计算属性
        2 添加功能
            收集表单数据 v-model
            给添加按钮注册点击事件,发送添加请求
            需要重新渲染数据 getList
        3 删除功能
            注册点击事件,传参数id
            根据id发送删除请求
            需要重新渲染 getList
        4 饼图渲染


         */
        // const defaultArr = [
        //     {id:1,name:'球鞋',price:99,creator:'小白'},
        //     {id:2,name:'裤子',price:88,creator:'小白'},
        //     {id:3,name:'上衣',price:599,creator:'小白'},
        //     {id:1,name:'袜子',price:19,creator:'小白'}
        // ];

        const app = new Vue({
            el: '#app',
            data: {
                list:[],
                name:'',
                price:''
            },
            methods:{
                //获取数据列表
                async getList(){
                    const res = await axios.get('https://applet-base-api-t.itheima.net/bill',{
                        params:{
                            creator: '虾米大王'
                        }
                    });
                    console.log(res);
                    this.list = res.data.data;

                    //更新图表
                    this.myChart.setOption({
                        series: [
                            {
                                data: this.list.map( item =>({
                                    value: item.price,
                                    name: item.name
                                }))
                            }
                        ]
                    });

                },
                //添加账单
                async btnAdd(){
                    if(!this.name){
                        alert("请输入消费名称");
                        return
                    }
                    if(typeof this.price !== 'number'){
                        alert("请输入消费价格");
                        return
                    }
                    const res = await axios.post('https://applet-base-api-t.itheima.net/bill',{
                        creator:'虾米大王',
                        name: this.name,
                        price: this.price
                    });
                    console.log(res);
                    this.name = '';
                    this.price = '';
                    this.getList();
                },
                //删除
                async del(id){
                    //console.log(id);
                    const res = await axios.delete(`https://applet-base-api-t.itheima.net/bill/${id}`);
                    this.getList();
                }
            },
            computed:{
              totalPrice() {
                  return this.list.reduce( (sum,item) => sum + item.price,0);
              }
            },
            created(){
                this.getList();
            },
            mounted(){
                this.myChart = echarts.init(document.querySelector('#main'));
                this.myChart.setOption({
                    //大标题
                    title: {
                        text: '消费账单列表',
                        subtext: '',
                        left: 'center'
                    },
                    //提示框
                    tooltip: {
                        trigger: 'item'
                    },
                    //图例
                    legend: {
                        orient: 'vertical',
                        left: 'left'
                    },
                    //数据项
                    series: [
                        {
                            name: '消费账单',
                            type: 'pie',
                            radius: '60%', //圆的半径
                            data: [],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                });
            }
        })
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值