浙江网站建设哪家最好,魔鬼做交易网站,网站网页设计专业公司,网站建设与制作教学计划文章目录 一、JSON1.JSON介绍2.JSON格式数据转化3.示例 二、pyecharts1.安装pyecharts包2.查看官方示例 三、开发示例 一、JSON
1.JSON介绍
JSON是一种轻量级的数据交互格式#xff0c;采用完全独立于编程语言的文本格式来存储和表示数据#xff08;就是字符串#xff09;… 文章目录 一、JSON1.JSON介绍2.JSON格式数据转化3.示例 二、pyecharts1.安装pyecharts包2.查看官方示例 三、开发示例 一、JSON
1.JSON介绍
JSON是一种轻量级的数据交互格式采用完全独立于编程语言的文本格式来存储和表示数据就是字符串。 Python语言使用JSON有很大优势因为JSON无非就是一个单独的字典或一个内部元素都是字典的列表。 总结所以JSON可以直接和Python的字典或列表进行无缝转换。
2.JSON格式数据转化
通过 json.dumps(data) 方法把python数据转化为了json数据
data json.dumps(data)如果有中文可以带上:ensure asciiFalse参数来确保中文正常转换 通过 json.loads(data) 方法把json数据转化为了 python列表或字典
data json.loads(data)3.示例
echarts/__init__.pydata.txtjsonData.pydata.txt
{code: 10000,msg: null,error: true,data: {total: 1664,items: [{stat_date: 2024-01-09,genome: 1,industrial: 3,literature: 3,patent: 6},{stat_date: 2024-01-08,genome: 3,industrial: 8,literature: 6,patent: 9},{stat_date: 2024-01-07,genome: 3,industrial: 5,literature: 7,patent: 6},{stat_date: 2024-01-06,genome: 5,industrial: 7,literature: 3,patent: 8},{stat_date: 2024-01-05,genome: 9,industrial: 7,literature: 5,patent: 7},{stat_date: 2024-01-04,genome: 3,industrial: 5,literature: 8,patent: 5},{stat_date: 2024-01-03,genome: 8,industrial: 0,literature: 8,patent: 6},{stat_date: 2024-01-02,genome: 0,industrial: 9,literature: 4,patent: 4},{stat_date: 2024-01-01,genome: 7,industrial: 8,literature: 0,patent: 3},{stat_date: 2024-01-10,genome: 3,industrial: 7,literature: 4,patent: 6}],has_more: true}
}jsonData.py
import jsondef formatData():# 日期列表date_data {}date []# 数据genome_data []industrial_data []literature_data []patent_data []try:with open(D:/test/demo/echarts/data.txt,r,encodingutf-8) as file:for line in file:line line.strip()if len(line.split(:)) 1:continuedata line.split(:)[1].replace(,).strip( ,)if line.startswith(stat_date):date.append(data)elif line.startswith(genome):genome_data.append(data)elif line.startswith(industrial):industrial_data.append(data)elif line.startswith(literature):literature_data.append(data)elif line.startswith(patent):patent_data.append(data)except Exception as e:print(f出现异常啦{e})date_data[date] datedate_json json.dumps(date_data)genome_json json.dumps(genome_data)industrial_json json.dumps(industrial_data)literature_json json.dumps(literature_data)patent_json json.dumps(patent_data)print(f{type(date_json)},{date_json})print(f{type(genome_json)},{genome_json})print(f{type(industrial_json)},{industrial_json})print(f{type(literature_json)},{literature_json})print(f{type(patent_json)},{patent_json})return date_json,genome_json,industrial_json,literature_json,patent_json输出
class str,{date: [2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09, 2024-01-09]}
class str,[1, 3, 3, 5, 9, 3, 8, 0, 7, 3]
class str,[3, 8, 5, 7, 7, 5, 0, 9, 8, 7]
class str,[3, 6, 7, 3, 5, 8, 8, 4, 0, 4]
class str,[6, 9, 6, 8, 7, 5, 6, 4, 3, 6]二、pyecharts
开发可视化图表使用的技术栈是Echarts框架的python版本pyecharts包。
1.安装pyecharts包
通过pip下载pyecharts包在开发过程中直接引用即可。 下载命令如下
pip install pyecharts代码中引用示例
import pyecharts.options as opts
from pyecharts.charts import Line2.查看官方示例
pyecharts-gallery可通过官方示例详细学习和使用。
三、开发示例
下面我们就是用上面的JSON和pyecharts进行实践生成一个折线图且带有工具栏。通过浏览器访问html文件可查看统计图。例如 可通过工具栏中下载图片切换柱状图折线图数据等。
代码示例如下
echarts/__init__.pydata.txtjsonData.py# 运行后生成line.htmlline.htmlline.pyjsonData.py代码不变 line.py
import jsonimport pyecharts.options as opts
from pyecharts.charts import Line
import jsonDatadate_json,genome_json,industrial_json,literature_json,patent_json jsonData.formatData()
date json.loads(date_json)[date]
(Line().add_xaxis(xaxis_datadate).add_yaxis(series_namegenome,y_axisjson.loads(genome_json),symbolemptyCircle,is_symbol_showTrue,label_optsopts.LabelOpts(is_showTrue)).add_yaxis(series_nameindustrial,y_axisjson.loads(industrial_json),symbolemptyCircle,is_symbol_showTrue,label_optsopts.LabelOpts(is_showTrue)).add_yaxis(series_namepatent,y_axisjson.loads(patent_json),symbolemptyCircle,is_symbol_showTrue,label_optsopts.LabelOpts(is_showTrue)).add_yaxis(series_nameliterature,y_axisjson.loads(literature_json),symbolemptyCircle,is_symbol_showTrue,label_optsopts.LabelOpts(is_showTrue)).set_global_opts(title_optsopts.TitleOpts(title数统计, subtitle纯属虚构),tooltip_optsopts.TooltipOpts(triggeraxis),toolbox_optsopts.ToolboxOpts(is_showTrue),xaxis_optsopts.AxisOpts(type_category),yaxis_optsopts.AxisOpts(type_value,splitline_optsopts.SplitLineOpts(is_showTrue),)).render(D:/test/demo/echarts/line.html)
)
运行line.py之后生成line.html文件直接浏览器打开可以看到如图