policy_visualization.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. {% extends "admin/base_site.html" %}
  2. {% block title %}传染病防治政策数据报表{% endblock %}
  3. {% block extrahead %}
  4. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
  5. <script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>
  6. {% endblock %}
  7. {% block content %}
  8. <script>
  9. function updateExcludedIds() {
  10. // 获取所有的复选框和输入框
  11. var checkboxes = document.getElementsByClassName('policy-checkbox');
  12. var input = document.getElementById('exclude_ids');
  13. var input_exclude_ids = "{{ exclude_ids | safe}}";
  14. var origin_exclude_ids = input_exclude_ids ? input_exclude_ids.split(',') : [];
  15. // 创建一个数组来保存被选中的ID
  16. var ids = [];
  17. // 遍历所有的复选框
  18. for (var i = 0; i < checkboxes.length; i++) {
  19. var checkbox = checkboxes[i];
  20. // 如果复选框被选中,将其值添加到数组中
  21. if (checkbox.checked) {
  22. ids.push(checkbox.value);
  23. }
  24. }
  25. // 更新输入框的值
  26. if(origin_exclude_ids && origin_exclude_ids.length >0){
  27. input.value = [...origin_exclude_ids, ...ids].join(',');
  28. }
  29. else if(ids && ids.length > 1){
  30. input.value = [...ids].join(',');
  31. }else if(ids && ids.length === 1){
  32. input.value = ids[0];
  33. }
  34. }
  35. </script>
  36. <div class="container">
  37. <h1 class="mt-4">传染病防治政策数据报表</h1>
  38. <div class="row">
  39. <div class="col-md-6">
  40. <form method="GET" action="{% url 'policy_visualization' %}">
  41. <div class="mb-3">
  42. <label for="diseaseTypes">传染病分类:</label>
  43. <select multiple class="form-control" id="diseaseTypes" name="disease_types">
  44. {% for disease_type in disease_types %}
  45. <option value="{{ disease_type.id }}" {% if disease_type.id in selected_disease_types %}selected{% endif %}>{{ disease_type.name }}</option>
  46. {% endfor %}
  47. </select>
  48. </div>
  49. <div class="mb-3">
  50. <label for="start_year" class="form-label">发布年份范围:</label>
  51. <input type="number" name="start_year" id="start_year" class="form-control" min="1949" max="{{ current_year }}" value="{{ start_year }}" placeholder="起始年份">
  52. <input type="number" name="end_year" id="end_year" class="form-control" min="1949" max="{{ current_year }}" value="{{ end_year }}" placeholder="结束年份">
  53. </div>
  54. <div class="mb-3">
  55. <label for="exclude_ids" class="form-label">排除政策 ID:</label>
  56. <input type="text" name="exclude_ids" id="exclude_ids" class="form-control" value="{{ exclude_ids }}" placeholder="政策 ID,多个以逗号分隔">
  57. </div>
  58. <div class="mb-3 form-check">
  59. <div class="form-check-inline">
  60. <input type="checkbox" name="group_by_category" id="group_by_category" class="form-check-input" {% if group_by_category %}checked{% endif %}>
  61. <label for="group_by_category" class="form-check-label">按传染病类别统计 &nbsp; &nbsp; </label>
  62. </div>
  63. <div class="form-check-inline">
  64. <input type="checkbox" name="show_raw_data" id="show_raw_data" class="form-check-input" {% if show_raw_data %}checked{% endif %}>
  65. <label for="show_raw_data" class="form-check-label">显示原始数据</label>
  66. </div>
  67. </div>
  68. <button type="submit" class="btn btn-primary">统计</button>
  69. </form>
  70. </div>
  71. </div>
  72. {% if show_raw_data %}
  73. <div class="row mt-4">
  74. <div class="col-md-6">
  75. <table class="table">
  76. <thead>
  77. <tr>
  78. <th></th> <!-- 新增的标题 -->
  79. <th>ID</th>
  80. <th>类别</th>
  81. <th>文件</th>
  82. <th>年份</th>
  83. <th>发布部门</th>
  84. <th>发布日期</th>
  85. <th>实施日期</th>
  86. <th>传染病类型列表</th>
  87. <th>时效性</th>
  88. <th>效力级别</th>
  89. </tr>
  90. </thead>
  91. <tbody>
  92. {% for item in policy_list %}
  93. <tr>
  94. <!-- 在每一行添加一个复选框,并设置其值为政策的ID -->
  95. <td><input type="checkbox" class="policy-checkbox" value="{{ item.id }}" onchange="updateExcludedIds()"></td>
  96. <td><a href="/admin/policies/policy/{{ item.id }}" target="_blank">{{ item.id }}</a></td>
  97. <td>{{ item.category }}</td>
  98. <td><a href="/admin/policies/policy/{{ item.id }}" target="_blank">{{ item.file_name }}</a></td>
  99. <td>{{ item.year }}</td>
  100. <td>{{ item.department }}</td>
  101. <td>{{ item.publish_date }}</td>
  102. <td>{{ item.implementation_date }}</td>
  103. <td>
  104. {% for disease_type in item.disease_types.all %}
  105. {{ disease_type.name }}
  106. {% if not forloop.last %}, {% endif %}
  107. {% endfor %}
  108. </td>
  109. <td>{{ item.timeliness }}</td>
  110. <td>{{ item.effectiveness_level }}</td>
  111. </tr>
  112. {% endfor %}
  113. </tbody>
  114. </table>
  115. </div>
  116. </div>
  117. {% endif %}
  118. <div class="row mt-4">
  119. <div class="col-md-6">
  120. {% if group_by_category %}
  121. <canvas id="categoryChart" width="400" height="200"></canvas>
  122. {% else %}
  123. <canvas id="policyChart" width="400" height="200"></canvas>
  124. {% endif %}
  125. <!-- <canvas id="policyChart"></canvas> -->
  126. </div>
  127. </div>
  128. <div class="row mt-4">
  129. <div class="col-md-6" >
  130. <div style="text-align:center;">数据总量:{{ total_count }}</div>
  131. </div>
  132. </div>
  133. </div>
  134. <script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>
  135. <script>
  136. {% if group_by_category %}
  137. var categoryChart = new Chart(document.getElementById('categoryChart'), {
  138. type: 'bar',
  139. data: {
  140. labels: [{% for item in policy_count_by_category %}"{{ item.disease_types__name }}",{% endfor %}],
  141. datasets: [{
  142. label: '政策文件数量',
  143. data: [{% for item in policy_count_by_category %}{{ item.count }},{% endfor %}],
  144. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  145. borderColor: 'rgba(54, 162, 235, 1)',
  146. borderWidth: 1
  147. }]
  148. },
  149. options: {
  150. scales: {
  151. y: {
  152. beginAtZero: true,
  153. precision: 0,
  154. stepSize: 1
  155. }
  156. }
  157. }
  158. });
  159. {% else %}
  160. // 获取后端传递的数据
  161. var policyData = {{ policy_count_by_year|safe }};
  162. // 处理数据
  163. var years = policyData.map(item => item.year);
  164. var counts = policyData.map(item => item.count);
  165. // 创建图表
  166. var ctx = document.getElementById('policyChart').getContext('2d');
  167. var chart = new Chart(ctx, {
  168. type: 'line',
  169. data: {
  170. labels: years,
  171. datasets: [{
  172. label: '政策数量',
  173. data: counts,
  174. backgroundColor: 'rgba(75, 192, 192, 0.2)',
  175. borderColor: 'rgba(75, 192, 192, 1)',
  176. borderWidth: 1
  177. }]
  178. },
  179. options: {
  180. scales: {
  181. y: {
  182. beginAtZero: true,
  183. stepSize: 1
  184. }
  185. }
  186. }
  187. });
  188. {% endif %}
  189. </script>
  190. {% endblock %}