echarts柱图Top10排名+Ajax渲染

使用echarts实现横向条形Top排名

在这里插入图片描述
1.处理下接口数据:

listSum
: 
["0.56", "0", "0", "0", "0.252", "0", "0", "0", "0", "0", "0.118", "0", "0.005", "0", "0", "0", "0",]
listTrain
: 
["1601", "1602", "1603", "1604", "1605", "1606", "1607", "1608", "1609", "1610", "1611", "1612",]
var listTrain  =[];
var trianSum = [];
$.ajax({
	url:url,
	async: true,
	data:{
		lineId:lined,
		yearMonth:$("#monthNum").val();
	},
	success:funciton(data){
		if(data.code ===0){
			listTrain = data.listTrain;
            trainSum = data.listSum;
            var map = new Map();
            for(i = 0; i < listTrain .length; i++){
            	map.set(listTrain[i],trainSum[i]);
            };
			var sortMap = new Map([...map.entries()].sort((a,b)=>b[1] - a[1]));//固定写法降序
			var sortKey = [];
			var sortValue = [];
			var topN = 10;
			sortMap.forEach((v,k)=>{
				sortKey .push(k);
				sortValue.push(v);
			});
			listTrain = sortKey .splice(0,topN );
			trainSum  = sortValue.splice(0,topN);
			mileageStatisticsEacharts();
		}
	}
})

断点的图:
在这里插入图片描述

2.echarts图表配置:

mileageStatisticsEacharts=function () {
    var option = {
        tooltip: {
            trigger: 'axis',
            backgroundColor: 'rgba(0,0,0,0.6)',
            extraCssText: 'box-shadow: 0 0 8px rgba(0, 0, 0, 0.3);',
            textStyle: {
                color: '#BDC8DC',
                fontFamily:'PingFang'
            }
        },
        legend: {
            show:true,
            itemGap: 12,
            icon: "circle",
            right: '25',
            top: '0',
            textStyle: {
                color: '#BDC8DC',
                fontSize: 14
            }
        },
        //位置
        grid: {
            left: '3%',
            right: '13%',
            top: '0',
            bottom: '1%',
            containLabel: true
        },
        //x轴设置
        xAxis: {
            type: 'value',
            show:false,
            axisLabel: {
                color: '#E1EEFF'
            },
            //坐标轴样式
            axisLine: {
                lineStyle: {
                    color: '#2F4871'
                },
                width: 15
            },
            //name样式
            nameTextStyle:{
                color: '#BDC8DC',
                fontSize: 15,
                fontWeight: 200
            },
            axisTick: {
                show: false
            },
            //分割线样式
            splitLine: {
                show:false
            }
        },
        //y轴设置
        yAxis: {
            type: 'category',
            axisLabel: {
                color: '#E1EEFF',
                fontFamily:'PingFang'
            },
            //坐标轴颜色
            axisLine: {
                show:false,
                lineStyle: {
                    color: '#217FA3'
                },
                width: 15
            },
            axisTick: {
                show: false
            },
            data: listTrain,
            inverse: true
        },
        series: [
            {
                // data:[424242, 152352,854242,525235,264575,376346,742426,924242,693248,392545,551516,66666,551516],
                data: trainSum,
                type:'bar',
                barWidth:16,
                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(22, 69, 87, 0.4)'
                },
                label:{
                    formatter: "{c}"+"万公里",
                    show:true,
                    position: 'right',
                    distance:-22,
                    textStyle:{
                        color:"#E1F4FF",
                    },
                },
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(
                            0, 1, 1, 1,
                            [
                                {offset: 0, color: '#2988D9'},     //渐变头部色
                                {offset: 0.5, color: '#13B9F5'},
                                {offset: 1, color: '#13B9F5'}
                            ]
                        )
                    },

                },
            }
        ]
    };
    setTimeout(function(){
        var width = myChart.getWidth();
        var opt = myChart.getOption();
        var grid = opt.grid[0];
        var right = grid.right;
        // console.log(right);
        var left = grid.left;
        right = width*parseFloat(right)/100;
        left = width*parseFloat(left)/100;
        var x = width-left-right;

        myChart.setOption({
            series:[{
                label:{
                    show:true,
                    position:'left',
                    offset:[x+50,0]
                }
            }]
        })
    });

    var myChart = echarts.init(document.getElementById('systemMultiBar'));
    myChart.setOption(option);
    window.addEventListener("resize", function () {
        myChart.resize();
    });
};
使用展示任务描述 本关任务:使用展示城市订单量top10以及1-9月份销售金额 情况。 最终结果如1所示: 1 城市订单量top10及1-9月份销售金额结果 数据集介绍 MySQL 数据库: 用户名:root 密码:123123 本地 IP:0.0.0.0 数据库名:tmallsale cityorder 表结构: 字段名 类型 是否为空 说明 ID int 非空 主键 city varchar(255) 非空 城市名 quantity varchar(255) 非空 订单量 部分数据展示 city quantity 上海市 1125312 苏州市 1010450 重庆市 805434 深圳市 778322 泉州市 738150 杭州市 563136 北京市 561432 广州市 523038 monthsale 表结构: 字段名 类型 是否为空 说明 month int 非空 月份/主键 amountmoney varchar(255) 非空 销售金额 部分数据展示 month amountmoney 9 498623384 8 607935472 7 621952477 6 574130121 5 575270541 4 505195997 编程要求 项目框架已经搭建完成,现需要补充内容使得项目能够成功展示可视化像。 在右侧编辑器切换不同文件,补充每个文件 Begin-End 区间的代码,使用展示城市订单量top10以及月销售金额情况,具体要求如下: 在 models.py 在编写代码,根据表 cityorder 以及表 monthsale 创建数据模型。 在 views1.py 中编写代码,当用户访问 http://localhost:8080/city 时,将数据传送到 test4.html 页面的功能,从数据库里获取的数据,城市订单量只需要前10条数据,所以我们只需要将获取到的前10条数据传入到HTML中。 test4.html 文件主要负责前端表的渲染,文件中已经有一个 Echarts 模板,现在需要将后台传入的数据处理后放入模板中,可视化显示需要将城市字段,订单销量字段,月份字段以及月销量字段传入,效果如下2所示: 2 模板渲染效果 注意:后端传递数据可能会乱码导致像显示不成功,使用 jinjia2 方法可以通过 xxxx | tojson 方法避免乱码情况。 考核点说明 成功启动 Flask 程序; 模拟 HTTP 请求,访问 localhost:8080/city,将结果进行对比,如果结果对比一致,则通关;反之,则未通关。 一、step4/tmallordercity/app/model/models.py的代码: from app import db class tmallsale(db.Model): __tablename__ = "cityorder" #*********** Begin **********# #************ End ***********# class tmallsale1(db.Model): __tablename__ = "monthsale" #*********** Begin **********# #************ End ***********# 二、step4/tmallordercity/app/view/view1.py的代码: from flask import render_template from app.views import index from app import db from app.model.models import tmallsale, tmallsale1 @index.route("/city") def index1(): selectdata = db.session.query(tmallsale.city).all() selectdata1 = db.session.query(tmallsale.quantity).all() list1=[] list2=[] list3=[] list4=[] #*********** Begin **********# #************ End ***********# return render_template("test4.html",city=list1,quantity=list2,month=list3,amountmoney=list4) 三、step4/tmallordercity/app/templates/test4.html的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>城市订单量top10以及月销售金额情况</title> <script type="text/javascript" src="../static/js/echarts-all-3.js" ></script> </head> <body> <!--准备一个DOM容器--> <div id="main" style="width: 1500px;height: 660px;"></div> <script> var myChart = echarts.init(document.getElementById('main')); option = { title: [ { left: 'center', text: '订购数量' }, { top: '55%', left: 'center', text: '盈利金额' } ], tooltip: { trigger: 'axis' }, legend: { data: ['数量', '金额'], left: 10 }, xAxis: [ { data: ["北京","上海","杭州","南京","广州","湖南","河南","云南","湖北","重庆"] }, { data: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月"], gridIndex: 1 } ], yAxis: [ {}, { gridIndex: 1 } ], grid: [ { bottom: '60%' }, { top: '60%' } ], series: [ { name:'数量', type: 'bar', showSymbol: true, data: [123,243,454,767,878,456,987,543,753,234], }, { name:'金额', type: 'bar', showSymbol: true, data: [421,653,775,984,635,246,752,246,427,864], xAxisIndex: 1, yAxisIndex: 1, } ] }; myChart.setOption(option); </script> </body> </html> 在begin至end区域添加代码,尽量不修改其他区域的代码城市订单量top10以及月销售金额情况
最新发布
05-14
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值