更多創客作品,請關注筆者網站園丁鳥,蒐集全球極具創意,且有價值的創客作品
ROS機器人知識請關注,diegorobot
業餘時間完成的一款在線統計過程分析工具SPC,及SPC知識分享網站qdo
#前言
實時SPC也就是SPC的運行控制圖,是實時收集生產過程中的生產數據,如來料檢驗的數據,生產過程中的數據,成品檢驗數據,然後對數據進行SPC的分析形成實時是控制圖,並通過判異規則對判斷存在品質失控的數據點,先上效果圖:
感興趣的同學可以到Qdo.plus體驗
#功能設計
實時圖表採用了Bokeh開源圖表開發包,Bokeh可以說是數據分析圖表的神器,而且本身是python開發的,可以和Flask無縫集成在一起,這裏主要介紹實時圖表的製作,以均值運行圖爲例,首先我們需要爲圖表定義Ajax類型的data source,並定義更新的頻率polling_interval,mode參數可以是replace,也可以append,也就是全部覆蓋原來的數據,還是隻是添加新數據。同時還需要定義數據跟新的視圖函數data_x,後續只要在data_x函數中處理此數據源,Web頁面上的圖表數據就會實時變化。下面是後臺圖表定義的代碼
def bokeh_monitor_x(chart_id,x,y,ucl,cl,lcl):
url_str='/data_x/'+str(chart_id)
source= AjaxDataSource(data_url=url_str,
polling_interval=5000, mode='replace')
source.data=set_source_data(x,y)
p= figure( title=_('X-bar'),tools="pan",sizing_mode="stretch_width",plot_height=300, background_fill_color="#fafafa",y_range=(lcl*0.95,ucl*1.05),x_minor_ticks=5)
p.toolbar.autohide = True
p.line('x', 'y', source=source, line_width=2,line_color="green", line_alpha=0.5)
p.circle('x', 'y', source=source,fill_color="white",size=8)
set_p_rule(p,source)
set_p_limit(p,ucl,cl,lcl)
p.legend.click_policy="hide"
p.x_range.follow = "end"
script, div = components(p)
return script, div
@spc.route('/data_x/<int:id>', methods=['GET', 'OPTIONS', 'POST'])
def data_x(id):
##get from cache
monitor_data=cache.get('spc_monitor_'+str(id))
data_index=8#緩存數據的列索引
x0=monitor_data.get_data_newest(100)[ : ,0]
y0=monitor_data.get_data_newest(100)[ : ,data_index]
t=get_t_dic(x0,y0,monitor_data.cl,monitor_data.lcl,monitor_data.ucl)
return jsonify(t)
下面是web頁面的示例代碼,在需要顯示圖表的位置,標記{{mdiv0|safe}},並在頭部引用scrip
<head>
{{ js_resources|indent(4)|safe }}
{{ css_resources|indent(4)|safe }}
{{mscript0|indent(4)|safe}}
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
{{mdiv0|safe}}
</div>
</div>
</div>
</div>
<body>
最後只需在flask的視圖函數中,按照如下調用
...
script0, div0 = bokeh_monitor_(id,x_value,y_value,ucl,cl,lcl)
return render_template('spc_x_bar.html', js_resources=js_resources,css_resources=css_resources,mscript0=script0,mdiv0=div0)
...