Skip to content

Commit 1d4f09f

Browse files
committed
using matplotlib to plot
1 parent c84ca68 commit 1d4f09f

3 files changed

Lines changed: 45 additions & 15 deletions

File tree

seiya/seiya/web/app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Flask, render_template, jsonify
1+
from flask import Flask, render_template, jsonify, Response
22

33
import seiya.web.job as job
44

@@ -50,6 +50,11 @@ def job_hot_tags_json():
5050
return jsonify(job.hot_tags())
5151

5252

53+
@app.route('/job/hot-tags.png')
54+
def job_hot_tags_plot():
55+
return Response(job.hot_tags_plot(format='png'), content_type='image/png')
56+
57+
5358
@app.route('/job/experience-stat')
5459
def job_experience_stat():
5560
return render_template('job/experience-stat.html', rows=job.experience_stat())

seiya/seiya/web/job.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from io import BytesIO
2+
13
from sqlalchemy import func, Float, select, and_
24
import pandas as pd
5+
import matplotlib as mpl
6+
import matplotlib.pyplot as plt
37

48
from seiya.db import engine, Session, JobModel
59

@@ -24,7 +28,7 @@ def salary_top10():
2428
return [row._asdict() for row in rows]
2529

2630

27-
def hot_tags():
31+
def _hot_tags():
2832
df = pd.read_sql(select([JobModel.tags]), engine)
2933

3034
df = pd.concat([pd.Series(row['tags'].split(' '))
@@ -35,15 +39,32 @@ def hot_tags():
3539
df = df[df['tag'] != '""']
3640
df = df[df['tag'] != '']
3741

38-
s = df.groupby(['tag']).size().sort_values(ascending=False)
42+
return df.groupby(['tag']).size().sort_values(ascending=False)
43+
3944

45+
def hot_tags():
4046
rows = []
41-
for item in s.items():
47+
for item in _hot_tags().items():
4248
rows.append({'tag': item[0], 'count': item[1]})
4349

4450
return rows
4551

4652

53+
def hot_tags_plot(format='png'):
54+
mpl.rcParams['font.sans-serif'] = ['SimHei']
55+
mpl.rcParams['axes.unicode_minus'] = False
56+
mpl.rcParams['figure.figsize'] = 10, 5
57+
58+
s = _hot_tags()
59+
60+
plt.bar(s.index[:10], s.values[:10])
61+
62+
img = BytesIO()
63+
plt.savefig(img, format=format)
64+
65+
return img.getvalue()
66+
67+
4768
def experience_stat():
4869
session = Session()
4970
rows = session.query(

seiya/seiya/web/templates/job/hot-tags.html

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
</ol>
1212
</nav>
1313

14-
<div class="my-5" id="chart"></div>
14+
<div class="text-center my-5">
15+
<img src="/job/hot-tags.png" >
16+
</div>
17+
18+
<!-- <div class="my-5" id="chart"></div> -->
1519

1620
<table class="table table-striped my-5">
1721
<thead>
@@ -35,17 +39,17 @@
3539

3640
{% block js %}
3741
<script>
38-
$.getJSON('/job/hot-tags.json', function (data) {
39-
var chart = new G2.Chart({
40-
container: 'chart',
41-
forceFit: true,
42-
height: 500
43-
});
42+
// $.getJSON('/job/hot-tags.json', function (data) {
43+
// var chart = new G2.Chart({
44+
// container: 'chart',
45+
// forceFit: true,
46+
// height: 500
47+
// });
4448

45-
chart.source(data.slice(0, 10));
46-
chart.interval().position('tag*count');
49+
// chart.source(data.slice(0, 10));
50+
// chart.interval().position('tag*count');
4751

48-
chart.render();
49-
});
52+
// chart.render();
53+
// });
5054
</script>
5155
{% endblock %}

0 commit comments

Comments
 (0)