{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10.1 网络关系图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基础网络关系图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据准备\n",
    "nodes = [\n",
    "    {\"name\": \"结点1\", \"symbolSize\": 10},\n",
    "    {\"name\": \"结点2\", \"symbolSize\": 20},\n",
    "    {\"name\": \"结点3\", \"symbolSize\": 30},\n",
    "    {\"name\": \"结点4\", \"symbolSize\": 40},\n",
    "    {\"name\": \"结点5\", \"symbolSize\": 50},\n",
    "]\n",
    "links = []\n",
    "for i in nodes:\n",
    "    for j in nodes:\n",
    "        links.append({\"source\": i[\"name\"], \"target\": j[\"name\"]})\n",
    "        \n",
    "print(links)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Graph\n",
    "\n",
    "(\n",
    "    Graph()\n",
    "    .add(\"\", \n",
    "         nodes, \n",
    "         links, \n",
    "         repulsion=5000\n",
    "    )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"基础网络关系图\"))\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 带边属性的网络关系图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备数据\n",
    "nodes_data = [\n",
    "    opts.GraphNode(name=\"结点1\", symbol_size=10),\n",
    "    opts.GraphNode(name=\"结点2\", symbol_size=20),\n",
    "    opts.GraphNode(name=\"结点3\", symbol_size=30),\n",
    "    opts.GraphNode(name=\"结点4\", symbol_size=40),\n",
    "    opts.GraphNode(name=\"结点5\", symbol_size=50),\n",
    "]\n",
    "\n",
    "links_data = [\n",
    "    opts.GraphLink(source=\"结点1\", target=\"结点2\", value=2),\n",
    "    opts.GraphLink(source=\"结点2\", target=\"结点3\", value=3),\n",
    "    opts.GraphLink(source=\"结点3\", target=\"结点4\", value=4),\n",
    "    opts.GraphLink(source=\"结点4\", target=\"结点5\", value=5),\n",
    "    opts.GraphLink(source=\"结点5\", target=\"结点1\", value=6),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Graph()\n",
    "    .add(\n",
    "        \"\",\n",
    "        nodes_data,\n",
    "        links_data,\n",
    "        repulsion=4000,\n",
    "        edge_label=opts.LabelOpts(\n",
    "            is_show=True, position=\"middle\", formatter=\"{b} 的数据 {c}\"\n",
    "        ),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 带类别网络关系图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "# 准备数据\n",
    "with open(\"./weibo.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "    j = json.load(f)\n",
    "    \n",
    "nodes, links, categories = j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    Graph()\n",
    "    .add(\n",
    "        \"\",\n",
    "        nodes,\n",
    "        links,\n",
    "        categories,\n",
    "        repulsion=50,\n",
    "        linestyle_opts=opts.LineStyleOpts(curve=0.2),\n",
    "        label_opts=opts.LabelOpts(is_show=False),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        legend_opts=opts.LegendOpts(is_show=False),\n",
    "        title_opts=opts.TitleOpts(title=\"微博转发关系图\"),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10.2 主题河流图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据准备\n",
    "x_data = [\"类别1\", \"类别2\", \"类别3\"]\n",
    "y_data = [\n",
    "    [\"2015/11/08\", 10, \"类别1\"],\n",
    "    [\"2015/11/09\", 15, \"类别1\"],\n",
    "    [\"2015/11/10\", 35, \"类别1\"],\n",
    "    [\"2015/11/11\", 38, \"类别1\"],\n",
    "    [\"2015/11/12\", 22, \"类别1\"],\n",
    "    [\"2015/11/13\", 16, \"类别1\"],\n",
    "    [\"2015/11/14\", 7, \"类别1\"],\n",
    "    [\"2015/11/15\", 2, \"类别1\"],\n",
    "    [\"2015/11/16\", 17, \"类别1\"],\n",
    "    [\"2015/11/17\", 33, \"类别1\"],\n",
    "    [\"2015/11/18\", 40, \"类别1\"],\n",
    "    [\"2015/11/19\", 32, \"类别1\"],\n",
    "    [\"2015/11/20\", 26, \"类别1\"],\n",
    "    [\"2015/11/21\", 35, \"类别1\"],\n",
    "    [\"2015/11/22\", 40, \"类别1\"],\n",
    "    [\"2015/11/23\", 32, \"类别1\"],\n",
    "    [\"2015/11/24\", 26, \"类别1\"],\n",
    "    [\"2015/11/25\", 22, \"类别1\"],\n",
    "    [\"2015/11/26\", 16, \"类别1\"],\n",
    "    [\"2015/11/27\", 22, \"类别1\"],\n",
    "    [\"2015/11/28\", 10, \"类别1\"],\n",
    "    [\"2015/11/08\", 35, \"类别2\"],\n",
    "    [\"2015/11/09\", 36, \"类别2\"],\n",
    "    [\"2015/11/10\", 37, \"类别2\"],\n",
    "    [\"2015/11/11\", 22, \"类别2\"],\n",
    "    [\"2015/11/12\", 24, \"类别2\"],\n",
    "    [\"2015/11/13\", 26, \"类别2\"],\n",
    "    [\"2015/11/14\", 34, \"类别2\"],\n",
    "    [\"2015/11/15\", 21, \"类别2\"],\n",
    "    [\"2015/11/16\", 18, \"类别2\"],\n",
    "    [\"2015/11/17\", 45, \"类别2\"],\n",
    "    [\"2015/11/18\", 32, \"类别2\"],\n",
    "    [\"2015/11/19\", 35, \"类别2\"],\n",
    "    [\"2015/11/20\", 30, \"类别2\"],\n",
    "    [\"2015/11/21\", 28, \"类别2\"],\n",
    "    [\"2015/11/22\", 27, \"类别2\"],\n",
    "    [\"2015/11/23\", 26, \"类别2\"],\n",
    "    [\"2015/11/24\", 15, \"类别2\"],\n",
    "    [\"2015/11/25\", 30, \"类别2\"],\n",
    "    [\"2015/11/26\", 35, \"类别2\"],\n",
    "    [\"2015/11/27\", 42, \"类别2\"],\n",
    "    [\"2015/11/28\", 42, \"类别2\"],\n",
    "    [\"2015/11/08\", 21, \"类别3\"],\n",
    "    [\"2015/11/09\", 25, \"类别3\"],\n",
    "    [\"2015/11/10\", 27, \"类别3\"],\n",
    "    [\"2015/11/11\", 23, \"类别3\"],\n",
    "    [\"2015/11/12\", 24, \"类别3\"],\n",
    "    [\"2015/11/13\", 21, \"类别3\"],\n",
    "    [\"2015/11/14\", 35, \"类别3\"],\n",
    "    [\"2015/11/15\", 39, \"类别3\"],\n",
    "    [\"2015/11/16\", 40, \"类别3\"],\n",
    "    [\"2015/11/17\", 36, \"类别3\"],\n",
    "    [\"2015/11/18\", 33, \"类别3\"],\n",
    "    [\"2015/11/19\", 43, \"类别3\"],\n",
    "    [\"2015/11/20\", 40, \"类别3\"],\n",
    "    [\"2015/11/21\", 34, \"类别3\"],\n",
    "    [\"2015/11/22\", 28, \"类别3\"],\n",
    "    [\"2015/11/23\", 26, \"类别3\"],\n",
    "    [\"2015/11/24\", 37, \"类别3\"],\n",
    "    [\"2015/11/25\", 41, \"类别3\"],\n",
    "    [\"2015/11/26\", 46, \"类别3\"],\n",
    "    [\"2015/11/27\", 47, \"类别3\"],\n",
    "    [\"2015/11/28\", 41, \"类别3\"],\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import ThemeRiver\n",
    "\n",
    "(\n",
    "    ThemeRiver()\n",
    "    .add(\n",
    "        series_name=x_data,\n",
    "        data=y_data,\n",
    "        singleaxis_opts=opts.SingleAxisOpts(\n",
    "            pos_top=\"50\", \n",
    "            pos_bottom=\"50\", \n",
    "            type_=\"time\"\n",
    "        ),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"axis\", axis_pointer_type=\"line\")\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "sales_amount = pd.read_csv('./sale_amount.csv', encoding='gbk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sales_amount.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转换数据形式\n",
    "sales_amount.index = sales_amount['date']\n",
    "sales_amount.drop(columns=['date'], inplace=True)\n",
    "sales_amount.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "format_data = sales_amount.unstack().reset_index()\n",
    "format_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "format_data.columns = ['超市', '日期', '销售量']\n",
    "format_data = format_data[['日期', '销售量', '超市']]\n",
    "format_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    ThemeRiver()\n",
    "    .add(\n",
    "        series_name='',\n",
    "        data=format_data.values.tolist(),\n",
    "        singleaxis_opts=opts.SingleAxisOpts(\n",
    "            pos_top=\"50\", \n",
    "            pos_bottom=\"50\", \n",
    "            type_=\"time\"\n",
    "        ),\n",
    "    )\n",
    "    .set_global_opts(\n",
    "        tooltip_opts=opts.TooltipOpts(trigger=\"axis\", axis_pointer_type=\"line\")\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10.3 词云图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据准备\n",
    "data = [\n",
    "    (\"生活资源\", \"999\"),\n",
    "    (\"供热管理\", \"888\"),\n",
    "    (\"供气质量\", \"777\"),\n",
    "    (\"生活用水管理\", \"688\"),\n",
    "    (\"一次供水问题\", \"588\"),\n",
    "    (\"交通运输\", \"516\"),\n",
    "    (\"城市交通\", \"515\"),\n",
    "    (\"环境保护\", \"483\"),\n",
    "    (\"房地产管理\", \"462\"),\n",
    "    (\"城乡建设\", \"449\"),\n",
    "    (\"社会保障与福利\", \"429\"),\n",
    "    (\"社会保障\", \"407\"),\n",
    "    (\"文体与教育管理\", \"406\"),\n",
    "    (\"公共安全\", \"406\"),\n",
    "    (\"公交运输管理\", \"386\"),\n",
    "    (\"出租车运营管理\", \"385\"),\n",
    "    (\"供热管理\", \"375\"),\n",
    "    (\"市容环卫\", \"355\"),\n",
    "    (\"自然资源管理\", \"355\"),\n",
    "    (\"粉尘污染\", \"335\"),\n",
    "    (\"噪声污染\", \"324\"),\n",
    "    (\"土地资源管理\", \"304\"),\n",
    "    (\"物业服务与管理\", \"304\"),\n",
    "    (\"医疗卫生\", \"284\"),\n",
    "    (\"粉煤灰污染\", \"284\"),\n",
    "    (\"占道\", \"284\"),\n",
    "    (\"供热发展\", \"254\"),\n",
    "    (\"农村土地规划管理\", \"254\"),\n",
    "    (\"生活噪音\", \"253\"),\n",
    "    (\"供热单位影响\", \"253\"),\n",
    "    (\"城市供电\", \"223\"),\n",
    "    (\"房屋质量与安全\", \"223\"),\n",
    "    (\"大气污染\", \"223\"),\n",
    "    (\"房屋安全\", \"223\"),\n",
    "    (\"文化活动\", \"223\"),\n",
    "    (\"拆迁管理\", \"223\"),\n",
    "    (\"公共设施\", \"223\"),\n",
    "    (\"供气质量\", \"223\"),\n",
    "    (\"供电管理\", \"223\"),\n",
    "    (\"燃气管理\", \"152\"),\n",
    "    (\"教育管理\", \"152\"),\n",
    "    (\"医疗纠纷\", \"152\"),\n",
    "    (\"执法监督\", \"152\"),\n",
    "    (\"设备安全\", \"152\"),\n",
    "    (\"政务建设\", \"152\"),\n",
    "    (\"县区、开发区\", \"152\"),\n",
    "    (\"宏观经济\", \"152\"),\n",
    "    (\"教育管理\", \"112\"),\n",
    "    (\"社会保障\", \"112\"),\n",
    "    (\"生活用水管理\", \"112\"),\n",
    "    (\"物业服务与管理\", \"112\"),\n",
    "    (\"分类列表\", \"112\"),\n",
    "    (\"农业生产\", \"112\"),\n",
    "    (\"二次供水问题\", \"112\"),\n",
    "    (\"城市公共设施\", \"92\"),\n",
    "    (\"拆迁政策咨询\", \"92\"),\n",
    "    (\"物业服务\", \"92\"),\n",
    "    (\"物业管理\", \"92\"),\n",
    "    (\"社会保障保险管理\", \"92\"),\n",
    "    (\"低保管理\", \"92\"),\n",
    "    (\"文娱市场管理\", \"72\"),\n",
    "    (\"城市交通秩序管理\", \"72\"),\n",
    "    (\"执法争议\", \"72\"),\n",
    "    (\"商业烟尘污染\", \"72\"),\n",
    "    (\"占道堆放\", \"71\"),\n",
    "    (\"地上设施\", \"71\"),\n",
    "    (\"水质\", \"71\"),\n",
    "    (\"无水\", \"71\"),\n",
    "    (\"供热单位影响\", \"71\"),\n",
    "    (\"人行道管理\", \"71\"),\n",
    "    (\"主网原因\", \"71\"),\n",
    "    (\"集中供热\", \"71\"),\n",
    "    (\"客运管理\", \"71\"),\n",
    "    (\"国有公交（大巴）管理\", \"71\"),\n",
    "    (\"工业粉尘污染\", \"71\"),\n",
    "    (\"治安案件\", \"71\"),\n",
    "    (\"压力容器安全\", \"71\"),\n",
    "    (\"身份证管理\", \"71\"),\n",
    "    (\"群众健身\", \"41\"),\n",
    "    (\"工业排放污染\", \"41\"),\n",
    "    (\"破坏森林资源\", \"41\"),\n",
    "    (\"市场收费\", \"41\"),\n",
    "    (\"生产资金\", \"41\"),\n",
    "    (\"生产噪声\", \"41\"),\n",
    "    (\"农村低保\", \"41\"),\n",
    "    (\"劳动争议\", \"41\"),\n",
    "    (\"劳动合同争议\", \"41\"),\n",
    "    (\"劳动报酬与福利\", \"41\"),\n",
    "    (\"医疗事故\", \"21\"),\n",
    "    (\"停供\", \"21\"),\n",
    "    (\"基础教育\", \"21\"),\n",
    "    (\"职业教育\", \"21\"),\n",
    "    (\"物业资质管理\", \"21\"),\n",
    "    (\"拆迁补偿\", \"21\"),\n",
    "    (\"设施维护\", \"21\"),\n",
    "    (\"市场外溢\", \"11\"),\n",
    "    (\"占道经营\", \"11\"),\n",
    "    (\"树木管理\", \"11\"),\n",
    "    (\"农村基础设施\", \"11\"),\n",
    "    (\"无水\", \"11\"),\n",
    "    (\"供气质量\", \"11\"),\n",
    "    (\"停气\", \"11\"),\n",
    "    (\"市政府工作部门（含部门管理机构、直属单位）\", \"11\"),\n",
    "    (\"燃气管理\", \"11\"),\n",
    "    (\"市容环卫\", \"11\"),\n",
    "    (\"新闻传媒\", \"11\"),\n",
    "    (\"人才招聘\", \"11\"),\n",
    "    (\"市场环境\", \"11\"),\n",
    "    (\"行政事业收费\", \"11\"),\n",
    "    (\"食品安全与卫生\", \"11\"),\n",
    "    (\"城市交通\", \"11\"),\n",
    "    (\"房地产开发\", \"11\"),\n",
    "    (\"房屋配套问题\", \"11\"),\n",
    "    (\"物业服务\", \"11\"),\n",
    "    (\"物业管理\", \"11\"),\n",
    "    (\"占道\", \"11\"),\n",
    "    (\"园林绿化\", \"11\"),\n",
    "    (\"户籍管理及身份证\", \"11\"),\n",
    "    (\"公交运输管理\", \"11\"),\n",
    "    (\"公路（水路）交通\", \"11\"),\n",
    "    (\"房屋与图纸不符\", \"11\"),\n",
    "    (\"有线电视\", \"11\"),\n",
    "    (\"社会治安\", \"11\"),\n",
    "    (\"林业资源\", \"11\"),\n",
    "    (\"其他行政事业收费\", \"11\"),\n",
    "    (\"经营性收费\", \"11\"),\n",
    "    (\"食品安全与卫生\", \"11\"),\n",
    "    (\"体育活动\", \"11\"),\n",
    "    (\"有线电视安装及调试维护\", \"11\"),\n",
    "    (\"低保管理\", \"11\"),\n",
    "    (\"劳动争议\", \"11\"),\n",
    "    (\"社会福利及事务\", \"11\"),\n",
    "    (\"一次供水问题\", \"11\"),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import WordCloud\n",
    "\n",
    "(\n",
    "    WordCloud()\n",
    "    .add(\n",
    "        series_name=\"热点分析\", \n",
    "        data_pair=data, \n",
    "        word_size_range=[6, 66])\n",
    "    .set_global_opts(\n",
    "        title_opts=opts.TitleOpts(\n",
    "            title=\"热点分析\", title_textstyle_opts=opts.TextStyleOpts(font_size=23)\n",
    "        ),\n",
    "        tooltip_opts=opts.TooltipOpts(is_show=True),\n",
    "    )\n",
    ").render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "data_anal_env",
   "language": "python",
   "name": "data_anal_env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
