SimpleDao
plotly使用渲染图标chart
2019-05-06, 访问数: 1107

使用bottle

  1. import json
  2. from bottle import route, run, template, Bottle, response
  3. import argparse
  4. import pandas as pd
  5. from plotly import utils
  6. import plotly.graph_objs as go
  7. from requests.compat import json as _json
  8. from influxdb import DataFrameClient
  9. app = Bottle()
  10. @app.hook('after_request')
  11. def enable_cors():
  12. response.headers['Access-Control-Allow-Origin'] = '*'
  13. response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
  14. response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
  15. @app.route('/data')
  16. def index():
  17. client = DataFrameClient('127.0.0.1', '8086', database='dc')
  18. res = client.query('select sum(succ), sum(fail) from p group by host limit 10')
  19. data = []
  20. for metric, df in res.items():
  21. print df
  22. trace = go.Bar(
  23. x=df[df.columns[0]],
  24. y=df[df.columns[1]],
  25. name=str(metric),
  26. )
  27. data.append(trace)
  28. layout = go.Layout()
  29. data_json = _json.dumps(data, cls=utils.PlotlyJSONEncoder)
  30. layout_json = _json.dumps(layout, cls=utils.PlotlyJSONEncoder)
  31. data_dict = json.loads(data_json)
  32. layout_dict = json.loads(layout_json)
  33. return {'records': data_dict, 'layout': layout_dict}
  34. @app.route('/pie')
  35. def pie():
  36. client = DataFrameClient('127.0.0.1', '8086', database='dc')
  37. res = client.query('select sum(succ), sum(fail) from p group by host limit 10')
  38. data = []
  39. labels = []
  40. pds = []
  41. for metric, df in res.items():
  42. print metric, df
  43. print '---------', df.columns, df.columns.values
  44. labels.append(str(metric) + df.columns[0])
  45. labels.append(str(metric) + df.columns[1])
  46. pds.append(df[df.columns[0]])
  47. pds.append(df[df.columns[1]])
  48. trace = go.Pie(
  49. labels=labels,
  50. values=pd.concat(pds),
  51. name=str(metric),
  52. )
  53. data.append(trace)
  54. layout = go.Layout()
  55. data_json = _json.dumps(data, cls=utils.PlotlyJSONEncoder)
  56. layout_json = _json.dumps(layout, cls=utils.PlotlyJSONEncoder)
  57. data_dict = json.loads(data_json)
  58. layout_dict = json.loads(layout_json)
  59. return {'records': data_dict, 'layout': layout_dict}
  60. @app.route('/line')
  61. def line():
  62. client = DataFrameClient('127.0.0.1', '8086', database='dc')
  63. res = client.query('select sum(succ), sum(fail) from p where time > now() - 1d and succ > 0 group by time(1h) limit 10')
  64. data = []
  65. for metric, df in res.items():
  66. print metric, df
  67. print '---------', df.index
  68. for col in df.columns.values:
  69. trace = go.Scatter(
  70. x=df.index,
  71. y=df[col],
  72. name=str(metric),
  73. )
  74. data.append(trace)
  75. layout = go.Layout()
  76. data_json = _json.dumps(data, cls=utils.PlotlyJSONEncoder)
  77. layout_json = _json.dumps(layout, cls=utils.PlotlyJSONEncoder)
  78. data_dict = json.loads(data_json)
  79. layout_dict = json.loads(layout_json)
  80. return {'records': data_dict, 'layout': layout_dict}
  81. run(app, host='localhost', port=8080, debug=True)

html

  1. <html>
  2. <head>
  3. <script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
  4. <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
  5. </head>
  6. <div id="plot" style="width:90%;height:600px;"></div>
  7. <div id="plot_query"></div>
  8. <script>
  9. function makeplot() {
  10. $.ajax({
  11. url: 'http://127.0.0.1:8080/pie',
  12. data: {
  13. },
  14. dataType: 'json',
  15. success: function (result) {
  16. console.log(result);
  17. //result = JSON.parse(result);
  18. console.log(result.records);
  19. console.log(result.layout);
  20. makePlotly(result.query, result.records, result.layout);
  21. document.getElementById("plot_query").innerHTML = result.query;
  22. }
  23. });
  24. };
  25. function makePlotly(title, traces, layout){
  26. var plotDiv = document.getElementById("plot");
  27. Plotly.newPlot('plot', traces, layout);
  28. };
  29. makeplot();
  30. </script>
  31. </html>