Initial commit

This commit is contained in:
Your Name
2021-09-07 23:12:03 +08:00
commit 0f5e5327d8
354 changed files with 35438 additions and 0 deletions

0
chart/__init__.py Normal file
View File

3
chart/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
chart/apps.py Normal file
View File

@@ -0,0 +1,5 @@
from django.apps import AppConfig
class ChartConfig(AppConfig):
name = 'chart'

7414
chart/charts.py Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
# Generated by Django 3.1.7 on 2021-06-29 16:20
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='PlottingLibrary',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(blank=True, db_column='name', max_length=100)),
('example_chart', models.CharField(blank=True, db_column='example_chart', max_length=200)),
('example_file', models.CharField(blank=True, db_column='example_file', max_length=200)),
('show_chart', models.CharField(blank=True, db_column='show_chart', max_length=200)),
('sort', models.CharField(blank=True, db_column='sort', max_length=200)),
('visiting_time', models.IntegerField(db_column='visiting_time')),
('downlode_time', models.IntegerField(db_column='downlode_time')),
('page', models.IntegerField(db_column='page')),
],
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.7 on 2021-06-30 04:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('chart', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='plottinglibrary',
name='url',
field=models.CharField(blank=True, db_column='url', max_length=200),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.7 on 2021-06-30 05:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('chart', '0002_plottinglibrary_url'),
]
operations = [
migrations.AddField(
model_name='plottinglibrary',
name='example_file_img',
field=models.CharField(blank=True, db_column='example_file_img', max_length=200),
),
]

View File

@@ -0,0 +1,53 @@
# Generated by Django 3.1.7 on 2021-07-01 06:22
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('chart', '0003_plottinglibrary_example_file_img'),
]
operations = [
migrations.AlterField(
model_name='plottinglibrary',
name='downlode_time',
field=models.IntegerField(blank=True, db_column='downlode_time', null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='example_chart',
field=models.CharField(blank=True, db_column='example_chart', max_length=200, null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='example_file_img',
field=models.CharField(blank=True, db_column='example_file_img', max_length=200, null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='page',
field=models.IntegerField(db_column='page', null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='show_chart',
field=models.CharField(blank=True, db_column='show_chart', max_length=200, null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='sort',
field=models.CharField(blank=True, db_column='sort', max_length=200, null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='url',
field=models.CharField(blank=True, db_column='url', max_length=200, null=True),
),
migrations.AlterField(
model_name='plottinglibrary',
name='visiting_time',
field=models.IntegerField(blank=True, db_column='visiting_time', null=True),
),
]

View File

@@ -0,0 +1,54 @@
# Generated by Django 3.1.7 on 2021-07-10 06:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('chart', '0004_auto_20210701_1422'),
]
operations = [
migrations.RemoveField(
model_name='plottinglibrary',
name='example_chart',
),
migrations.RemoveField(
model_name='plottinglibrary',
name='example_file_img',
),
migrations.RemoveField(
model_name='plottinglibrary',
name='page',
),
migrations.RemoveField(
model_name='plottinglibrary',
name='show_chart',
),
migrations.AddField(
model_name='plottinglibrary',
name='Advanced_setting',
field=models.TextField(blank=True, db_column='Advanced_setting', null=True),
),
migrations.AddField(
model_name='plottinglibrary',
name='Basic_settings',
field=models.TextField(blank=True, db_column='Basic_settings', null=True),
),
migrations.AddField(
model_name='plottinglibrary',
name='index_img',
field=models.CharField(blank=True, db_column='index_img', max_length=200, null=True),
),
migrations.AddField(
model_name='plottinglibrary',
name='js',
field=models.TextField(blank=True, db_column='js', null=True),
),
migrations.AddField(
model_name='plottinglibrary',
name='show_img',
field=models.CharField(blank=True, db_column='show_img', max_length=200, null=True),
),
]

View File

@@ -0,0 +1,30 @@
# Generated by Django 3.1.7 on 2021-07-25 01:52
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('chart', '0005_auto_20210710_1443'),
]
operations = [
migrations.RemoveField(
model_name='plottinglibrary',
name='Advanced_setting',
),
migrations.RemoveField(
model_name='plottinglibrary',
name='Basic_settings',
),
migrations.RemoveField(
model_name='plottinglibrary',
name='js',
),
migrations.AddField(
model_name='plottinglibrary',
name='parameter',
field=models.JSONField(blank=True, db_column='parameter', null=True),
),
]

View File

22
chart/models.py Normal file
View File

@@ -0,0 +1,22 @@
from django.db import models
class PlottingLibrary(models.Model):
id = models.IntegerField(primary_key=True) # 主键id
name = models.CharField(db_column='name', max_length=100, blank=True, null=False) # 图的名字
index_img = models.CharField(db_column='index_img', max_length=200, blank=True, null=True) # 主页展示的图片
show_img = models.CharField(db_column='show_img', max_length=200, blank=True, null=True) # 图标主页展示的图片
example_file = models.CharField(db_column='example_file', max_length=200, blank=True, ) # 示例文件
sort = models.CharField(db_column='sort', max_length=200, blank=True, null=True) # 分类
visiting_time = models.IntegerField(db_column='visiting_time', blank=True, null=True) # 查看次数
downlode_time = models.IntegerField(db_column='downlode_time', blank=True, null=True) # 使用次数/下载次数
url = models.CharField(db_column='url', max_length=200, blank=True, null=True) # 链接地址
parameter = models.JSONField(db_column='parameter', blank=True, null=True) # 参数

3
chart/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

388
chart/tools.py Normal file
View File

@@ -0,0 +1,388 @@
import os
import random
import time
import pandas as pd
from Simple_Drawing.settings import BASE_DIR
url_ = "127.0.0.1:8000"
# 保存单个文件
def keep_file(file_data, file_path, file_name=None, ui=0):
if file_name is not None:
file_path = os.path.join(file_path, file_name)
if ui:
with open(file_path, 'w') as f:
f.write(file_data)
else:
with open(file_path, 'wb+') as f:
for chunk1 in file_data.chunks():
f.write(chunk1)
# 保存多个文件
def keep_files(na, c):
cmd = '''
cd %s/file_keep/%s
mkdir morefile
''' % (BASE_DIR, c)
os.system(cmd)
for file in na:
with open('%s' % os.path.join(f'{BASE_DIR}/file_keep/{c}/morefile/', file.name), 'wb') as f:
for i in file.chunks():
f.write(i)
f.close()
# 返回唯一文件编号
def generate_id():
a = time.asctime(time.localtime(time.time()))
b = a[-15:] + str(random.randint(0, 1000))
c = b.replace(" ", "")
c = c.replace(":", "0")
return c
# 给出文件路径
def path_out(sort, name=generate_id(), show=None, out=None):
if show == 1:
return os.path.join(BASE_DIR, "static", "img_eg", "show_chart", name)
if out == 1:
return os.path.join(BASE_DIR, 'file_keep', sort, "img", name)
return os.path.join(BASE_DIR, 'file_keep', sort, "file", name), name
# 选出需要的列
def pick_col(file_path, col, sep=","):
midwest = pd.read_csv(file_path, sep=sep)
col = set(midwest[col])
return col
# 返回页数
def return_pages(su: int) -> int:
if su % 12 != 0:
return (su // 12) + 1
else:
return su // 12
class ChartParameter(object):
def __init__(self, data):
if 'name' in data:
self.name = data['name']
if 'html_name' in data:
self.html_name = data['html_name']
if 'value' in data:
self.value = data['value']
if 'astrict' in data:
self.astrict = data['astrict']
if 'input_type' in data:
self.input_type = data['input_type']
if 'Basics' in data:
self.Basics = data['Basics']
if 'Senior' in data:
self.Senior = data['Senior']
self.colors = 0
self.colors_dict = {}
def html_out_Basics(self):
code = ""
for title in self.Basics:
code += f"""
<dd style="overflow: hidden">
<div class="color">
<label style="" class="label_meg"><b>{title}&nbsp;&nbsp;</b></label>
</div>
</dd>
"""
for d in self.Basics[title]:
parameter = ChartParameter(d)
if parameter.input_type == "text":
parameter.input_type = f"""
<input {parameter.astrict} type="text" id="{parameter.name}" lay-verify="required" value="{parameter.value}"
autocomplete="off" class="layui-input"
style="height: 25px; width:80px;margin-top: 6px">
"""
if parameter.input_type == "number":
parameter.input_type = f"""
<input {parameter.astrict} type="number" id="{parameter.name}" lay-verify="required" value="{parameter.value}"
autocomplete="off" class="layui-input" min="0" max="999"
style="height: 25px; width:80px;margin-top: 6px">
"""
if parameter.input_type == "date":
parameter.input_type = f"""
<input {parameter.astrict} type="date" id="{parameter.name}" lay-verify="required" value="{parameter.value}"
autocomplete="off" class="layui-input"
style="height: 25px; width:80px;margin-top: 6px">
"""
if parameter.input_type == "color_name":
self.colors_dict[f"color{self.colors}"] = "color_name"
parameter.input_type = f"""
<div style="margin-left: 30px;">
<div id="color{self.colors}" style="margin-left: 30px; display: inline-block"></div>
</div>
"""
self.colors += 1
if parameter.input_type == "rgb":
self.colors_dict[f"color{self.colors}"] = "rgb"
f"""
<div style="margin-left: 30px;">
<div id="color{self.colors}" style="margin-left: 30px; display: inline-block"></div>
</div>
"""
self.colors += 1
if parameter.input_type == "palettes":
parameter.input_type = f"""
<select {parameter.astrict} id="{parameter.name}" lay-verify="required" value="{parameter.value}" autocomplete="off"
class="layui-input" style="height: 25px; width:80px;margin-top: 6px">
<option value="YlOrRd">YlOrRd</option>
<option value="YlOrBr">YlOrBr</option>
<option value="YlGnBu">YlGnBu</option>
<option value="Reds">Reds</option>
<option value="RdPu">RdPu</option>
<option value="Purples">Purples</option>
<option value="PuRd">PuRd</option>
<option value="PuBuGn">PuBuGn</option>
<option value="PuBu">PuBu</option>
<option value="OrRd">OrRd</option>
<option value="Oranges">Oranges</option>
<option value="Greys">Greys</option>
<option value="Greens">Greens</option>
<option value="GnBu">GnBu</option>
<option value="BuPn">BuPn</option>
<option value="BuGn">BuGn</option>
<option value="Blues">Blues</option>
<option value="Set3">Set3</option>
<option value="Set2">Set2</option>
<option value="Set1">Set1</option>
<option value="Pastel2">Pastel2</option>
<option value="Pastel1">Pastel1</option>
<option value="Paired">Paired</option>
<option value="Dark2">Dark2</option>
<option value="Accent">Accent</option>
<option value="Spectral">Spectral</option>
<option value="RdYlGn">RdYlGn</option>
<option value="RdYlBn">RdYlBn</option>
<option value="RdGy">RdGy</option>
<option value="RdBu">RdBu</option>
<option value="PuOr">PuOr</option>
<option value="PRGn">PRGn</option>
<option value="PiYG">PiYG</option>
<option value="BrBG">BrBG</option>
</select>
"""
if parameter.input_type == "add":
parameter.input_type = f"""
<select {parameter.astrict} id="{parameter.name}" lay-verify="required" value="{parameter.value}" autocomplete="off"
class="layui-input" style="height: 25px; width:80px;margin-top: 6px">
<option value="TRUE">YES</option>
<option value="FALSE">NO</option>
</select>
"""
if parameter.input_type == "position":
parameter.input_type = f"""
<select {parameter.astrict} id="{parameter.name}" lay-verify="required" value="{parameter.value}" autocomplete="off"
class="layui-input" style="height: 25px; width:80px;margin-top: 6px">
<option value="right">Right</option>
<option value="left">Left</option>
<option value="top">Top</option>
<option value="bottom">Bottom</option>
</select>
"""
code += f"""
<dd style="overflow: hidden">
<div class="color">
<label class="label_meg">{parameter.html_name}&nbsp;&nbsp;</label>
<div class="layui-input-inline" style="float: right">
{parameter.input_type}
</div>
</div>
</dd>
"""
return code
def html_out_Senior(self):
code = ""
for title in self.Senior:
code += f"""
<dd style="overflow: hidden">
<div class="color">
<label style="" class="label_meg"><b>{title}&nbsp;&nbsp;</b></label>
</div>
</dd>
"""
for d in self.Senior[title]:
parameter = ChartParameter(d)
if parameter.input_type == "text":
parameter.input_type = f"""
<input {parameter.astrict} type="text" id="{parameter.name}" lay-verify="required" value="{parameter.value}"
autocomplete="off" class="layui-input"
style="height: 25px; width:80px;margin-top: 6px">
"""
if parameter.input_type == "number":
parameter.input_type = f"""
<input {parameter.astrict} type="number" id="{parameter.name}" lay-verify="required" value="{parameter.value}"
autocomplete="off" class="layui-input" min="0" max="999"
style="height: 25px; width:80px;margin-top: 6px">
"""
if parameter.input_type == "date":
parameter.input_type = f"""
<input {parameter.astrict} type="date" id="{parameter.name}" lay-verify="required" value="{parameter.value}"
autocomplete="off" class="layui-input
style="height: 25px; width:80px;margin-top: 6px">
"""
if parameter.input_type == "color_name":
self.colors_dict[f"color{self.colors}"] = "color_name"
parameter.input_type = f"""
<div style="margin-left: 30px;">
<div id="color{self.colors}" style="margin-left: 30px; display: inline-block"></div>
</div>
"""
self.colors += 1
if parameter.input_type == "rgb":
self.colors_dict[f"color{self.colors}"] = "rgb"
f"""
<div style="margin-left: 30px;">
<div id="color{self.colors}" style="margin-left: 30px; display: inline-block"></div>
</div>
"""
self.colors += 1
if parameter.input_type == "palettes":
parameter.input_type = f"""
<select {parameter.astrict} id="{parameter.name}" lay-verify="required" value="{parameter.value}" autocomplete="off"
class="layui-input" style="height: 25px; width:80px;margin-top: 6px">
<option value="YlOrRd">YlOrRd</option>
<option value="YlOrBr">YlOrBr</option>
<option value="YlGnBu">YlGnBu</option>
<option value="Reds">Reds</option>
<option value="RdPu">RdPu</option>
<option value="Purples">Purples</option>
<option value="PuRd">PuRd</option>
<option value="PuBuGn">PuBuGn</option>
<option value="PuBu">PuBu</option>
<option value="OrRd">OrRd</option>
<option value="Oranges">Oranges</option>
<option value="Greys">Greys</option>
<option value="Greens">Greens</option>
<option value="GnBu">GnBu</option>
<option value="BuPn">BuPn</option>
<option value="BuGn">BuGn</option>
<option value="Blues">Blues</option>
<option value="Set3">Set3</option>
<option value="Set2">Set2</option>
<option value="Set1">Set1</option>
<option value="Pastel2">Pastel2</option>
<option value="Pastel1">Pastel1</option>
<option value="Paired">Paired</option>
<option value="Dark2">Dark2</option>
<option value="Accent">Accent</option>
<option value="Spectral">Spectral</option>
<option value="RdYlGn">RdYlGn</option>
<option value="RdYlBn">RdYlBn</option>
<option value="RdGy">RdGy</option>
<option value="RdBu">RdBu</option>
<option value="PuOr">PuOr</option>
<option value="PRGn">PRGn</option>
<option value="PiYG">PiYG</option>
<option value="BrBG">BrBG</option>
</select>
"""
if parameter.input_type == "add":
parameter.input_type = f"""
<select {parameter.astrict} id="{parameter.name}" lay-verify="required" value="{parameter.value}" autocomplete="off"
class="layui-input" style="height: 25px; width:80px;margin-top: 6px">
<option value="TRUE">YES</option>
<option value="FALSE">NO</option>
</select>
"""
if parameter.input_type == "position":
parameter.input_type = f"""
<select {parameter.astrict} id="{parameter.name}" lay-verify="required" value="{parameter.value}" autocomplete="off"
class="layui-input" style="height: 25px; width:80px;margin-top: 6px">
<option value="right">Right</option>
<option value="left">Left</option>
<option value="top">Top</option>
<option value="bottom">Bottom</option>
</select>
"""
code += f"""
<dd style="overflow: hidden">
<div class="color">
<label class="label_meg">{parameter.html_name}&nbsp;&nbsp;</label>
<div class="layui-input-inline" style="float: right">
{parameter.input_type}
</div>
</div>
</dd>
"""
return code
def js_out(self):
code = ""
for title in self.Basics:
for d in self.Basics[title]:
parameter = ChartParameter(d)
code += f"""{parameter.name}: (document.getElementById("{parameter.name}").value),
"""
for title in self.Senior:
for d in self.Senior[title]:
parameter = ChartParameter(d)
code += f"""{parameter.name}: (document.getElementById("{parameter.name}").value),
"""
return code
def color_js(self):
code = ""
for i in self.colors_dict.keys():
if self.colors_dict[i] == "rgb":
fl = "rgb"
else:
fl = "hex"
code += """
layui.use('colorpicker', function()
{
var $ = layui.$
, colorpicker = layui.colorpicker;
colorpicker.render({
elem: '%s'
, color: '#2ec770' //设置默认色
, size: "xs"
, format: '%s'
, done: function (color) {
layer.tips('选择了:' + color, this.elem);
}
});
})
""" % (i, fl)
return code
Treemap = {
"Basics": {
"title": [
{"name": "title", "value": "title", "astrict": "", "input_type": "text", "html_name": "Title"},
{"name": "title_font_size", "value": "12", "astrict": "", "input_type": "number", "html_name": "Title Size"}
],
"data": [
{"name": "group_by_column", "value": "group_by_column", "astrict": "", "input_type": "text",
"html_name": "Group By Column"},
{"name": "data_column", "value": "data_column", "astrict": "", "input_type": "text",
"html_name": "Data Column"}
]
},
"Senior": {
}
}

13
chart/urls.py Normal file
View File

@@ -0,0 +1,13 @@
from django.conf.urls import url
from django.urls import path
from chart import views
urlpatterns = [
path('down', views.download),
url("(.*\.html$)", views.out_page),
url('up_file', views.up_file),
url('tools$', views.tools),
url('tools$', views.tools),
url('(.*)_run', views.chart_run),
]

150
chart/views.py Normal file
View File

@@ -0,0 +1,150 @@
from django.http import JsonResponse, StreamingHttpResponse, HttpResponse
from django.shortcuts import render
from django.utils.safestring import mark_safe
from chart import charts
from chart.models import PlottingLibrary
from chart.tools import *
url_ = 'http://127.0.0.1:8000'
# 处理页面返回问题 页面重定向
def out_page(request, page):
if page in ["index.html", 'about.html', 'tools.html']:
return render(request, f'./page/{page}', {"url": url_})
# 验证访问的网页是否存在 防止eval漏洞
temp = page.replace("_", ' ')
print(temp.replace('.html', ''))
db = PlottingLibrary.objects.get(name=f"{temp.replace('.html', '')}")
print(temp)
if db is None:
return HttpResponse("no")
else:
a = ChartParameter(db.parameter)
data = {
'show_img': f"/{db.show_img}.png",
'name': db.name.replace(' ', '_'),
'title': db.name,
'Basics': mark_safe(a.html_out_Basics()),
'Senior': mark_safe(a.html_out_Senior()),
'example_file': db.name.replace(" ", "_"),
'js': mark_safe(a.js_out()),
'url': url_,
"color":mark_safe(a.color_js())
}
return render(request, f'./chart/Basic.html', data)
# 处理上传文件 返回颜色列表
def up_file(request):
file = request.FILES.get("file")
col_name = request.POST.get("col_name", "color")
if col_name == "NO":
file_path, file_id = path_out("chart")
keep_file(file, file_path)
data = {"flag": file_id}
return JsonResponse(data)
file_path, file_id = path_out("chart")
keep_file(file, file_path)
col_list = pick_col(file_path, col_name)
data = {"flag": file_id, 'data': [{"title": f"color_{i}", 'name': i, } for i in col_list][:8]}
return JsonResponse(data)
def download(request):
file_name = request.GET.get("name", None)
sort = request.GET.get("sort", "chart")
if sort == "tools":
file_id = request.GET.get("file_id") # 工作编号
name_path = os.path.join(BASE_DIR, "file_keep", "tools", "file", f"{file_id}")
in_file = f"out.txt"
elif file_name is None:
file_downlode_type = request.GET.get("file_downlode_type") # 文件类型
file_id = request.GET.get("file_id") # 工作编号
name_path = os.path.join(BASE_DIR, "file_keep", "chart", "img", f"{file_id}.{file_downlode_type}")
in_file = f"out.{file_downlode_type}"
else:
name_path = os.path.join(
BASE_DIR, "file_keep", "eg_file", "img", file_name)
in_file = file_name
print(name_path)
response = StreamingHttpResponse(readFile(name_path))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}"'.format(
in_file)
return response
def readFile(filename, chunk_size=512):
with open(filename, 'rb') as f:
while True:
c = f.read(chunk_size)
if c:
yield c
else:
break
def chart_run(request, get_chart_sort):
chart_name = get_chart_sort.replace("_run", '')
file_id = request.GET.get("file_id")
k = request.GET.get("k")
data = {}
temp = chart_name.replace("_", ' ')
db = PlottingLibrary.objects.get(name=temp)
if db is None:
return HttpResponse("no")
for i in request.GET.lists():
if i[0] not in ['_', 'file_id', 'k']:
data[i[0]] = i[1][0]
file_path = path_out("chart", file_id) # 返回文件路径
chart_out_path = path_out("chart", file_id, show=1) # 返回图片路径
out_path = path_out("chart", file_id, out=1) # 返回图片路径
# file_path 输入文件的路径 file
# chart 展示图片的路径
# out_path 输出文件的路径
eval(f'charts.{chart_name}')(file_path[0], f"{chart_out_path}_{k}.png", out_path, **data)
return JsonResponse({"img": f"/static/img_eg/show_chart/{file_id}_{k}.png"})
def tools(request):
file = request.FILES.get("file", None) # 标题
mg = request.POST.get('mg', None)
data = ""
if file is None and mg is None:
return JsonResponse({"http": "内容为空"})
if file is None:
data = 反向互补序列(mg)
else:
p = file.read()
p.decode(encoding='utf-8')
data = 反向互补序列(p.decode(encoding='utf-8'))
file_path, file_id = path_out("tools")
keep_file(data, file_path, ui=1)
return JsonResponse({"work_id": file_id})