微信搜索【大奇测试开】,关注这个坚持分享测试开发干货的家伙。
npm install echarts --save
执行完成后可以在 package.json 的 dependencies 配置有出现 "echarts": "^5.2.2"
<template> <div > <div ref="pieChartDemo" ></div> </div></template><script>import * as echarts from 'echarts'...</script>
export default { name: 'EchartsDemo', // 使用mounted在页面控件加载在完成后mounted方法进行echart初始化非created mounted() { this.initPieChart() }, methods: { initPieChart() { // 采用的是vue ref的方式获取容器 var chartDom = this.$refs['pieChartDemo'] var myChart = echarts.init(chartDom) var option = { title: { text: '大奇测试开发', subtext: '文章类型分布', left: 'center' }, tooltip: { trigger: 'item' }, legend: { orient: 'vertical', left: 'left' }, series: [ { name: 'Access From', type: 'pie', radius: '50%', data: [ { value: 20, name: '提测平台' }, { value: 2, name: '性能测试' }, { value: 1, name: '流量回放' }, { value: 3, name: '好文分享' }, { value: 5, name: '杂谈' } ] } ] } option && myChart.setOption(option); } }}
<div id="pieChartDemo" ></div>...略...var chartDom = document.getElementById('pieChartDemo')
# dashboard.py@test_dashboard.route("/api/dashboard/stacked", methods=['POST'])def get_request_stacked(): # body = json.loads(request.get_data()) connection = pool.connection() with connection.cursor() as cursor: sql_select = 'SELECT DATE_FORMAT(request.createDate,"%Y%u") weeks, apps.note, COUNT(apps.id) counts FROM request LEFT JOIN apps ON request.appId = apps.id GROUP BY weeks, apps.note;' cursor.execute(sql_select) table_data = cursor.fetchall() # 第一次循环过滤生成week和notes,并生成做临时关键词储备数据, # 用户第二次循环生成 series 需要数据 weeks = [] notes = [] key_value = {} for row in table_data: week = row['weeks'] note = row['note'] if not week in weeks: weeks.append(week) if not note in notes: notes.append(note) key_value[week+note] = row['counts'] # 做一个排序 小到大 weeks.sort() # 做对应日期下应用数据列表生成,没有数据的week用0填充,保证顺序长度一致 series = {} for note in notes: series[note] = [] for week in weeks: if week+note in key_value: series[note].append(key_value[week+note]) else: series[note].append(0) resp_data = { 'weeks': weeks, 'note': notes, 'series': series } resp = format.resp_format_success resp['data'] = resp_data return resp
<template> <div > <div ref="LineChartBoard" ></div> </div></template><script>import * as echarts from 'echarts'import { requestStacked } from '@/api/board'export default { name: 'Dashboard', mounted() { this.getApList() }, methods: { getApList() { requestStacked().then(resp => { this.initStackedChart(resp.data) }) }, initStackedChart(data) { const chartDom = this.$refs['LineChartBoard'] const myChart = echarts.init(chartDom) const series = [] // 唯一处理需要额外逻辑处理的地方,根据接口数据动态生成series数据 for (var key in data.series) { series.push( { name: key, type: 'line', stack: 'Total', areaStyle: {}, emphasis: { focus: 'series' }, data: data.series[key] } ) } var option = { title: { text: '周需求提测趋势' }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross', label: { backgroundColor: '#6a7985' } } }, legend: { // 数据标题展示 data: data.note }, toolbox: { feature: { saveAsImage: {} } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [ { type: 'category', boundaryGap: false, data: data.weeks } ], yAxis: [ { type: 'value' } ], series: series } option && myChart.setOption(option) } }}</script>
关于后续更多进展和分享欢迎持续关注公众号或博客。
本文来自博客园,作者:MrZ大奇,转载请注明原文链接:https://www.cnblogs.com/mrzcode/p/15841356.html