Example #1
0
def plot_3D_db(name):
    # connect db
    conn = connect_to_db()
    cursor = conn.cursor()
    # time handling
    currenttime = getorigintime()
    threemonths = (currenttime -
                   datetime.timedelta(days=90)).strftime('%Y-%m-%d')
    st_new = datetime.datetime.strptime(threemonths, "%Y-%m-%d")
    origin = datetime.datetime.strptime('1900-01-01', "%Y-%m-%d")
    delta = (st_new - origin).days + 2
    # open_p = exe_sql(cursor, 'open', delta)
    # close_p = exe_sql(cursor, 'close', delta)
    # high_p = exe_sql(cursor, 'high', delta)
    # low_p = exe_sql(cursor, 'low', delta)
    settle_p = exe_sql(cursor, 'settle', delta)
    volume = exe_sql(cursor, 'volume', delta)
    date = exe_sql(cursor, 'date', delta)
    conn.commit()
    cursor.close()
    conn.close()

    # data
    x = [i for i in range(1, len(date) + 1)]
    y = settle_p
    z = volume

    fig = plt.figure()
    ax = fig.gca(projection='3d')

    ax.plot(x, y, zs=0, zdir='z', label='curve in (x,y)', color='Gold')
    ax.scatter(xs=x, zs=z, ys=y, zdir='z', label='points in (x,y,z)', c='Gold')
    ax.legend()
    ax.title.set_color('Navy')
    ax.w_xaxis.set_pane_color((0.2, 0.2, 0.2, 1.0))
    ax.w_yaxis.set_pane_color((0.2, 0.2, 0.2, 1.0))
    ax.w_zaxis.set_pane_color((0.25, 0.25, 0.25, 1.0))
    ax.set_xlabel('Days')
    ax.set_ylabel('Settle Price')
    ax.set_zlabel('Volume')
    ax.view_init(elev=35, azim=-45)
    plt.xticks(color='Navy')
    plt.yticks(color='Navy')
    ax.tick_params(axis='z', colors='Navy')
    ax.xaxis.label.set_color('Navy')
    ax.yaxis.label.set_color('Navy')
    ax.zaxis.label.set_color('Navy')

    # pwd = os.path.dirname(os.path.dirname(__file__))
    # saveplace = pwd + '/static/pfas/img/' + name + '.png'
    # plt.savefig(saveplace, transparent=True)
    buf = BytesIO()
    plt.savefig(buf, transparent=True, format='png')
    data = base64.b64encode(buf.getbuffer()).decode("ascii")
    return data, name
Example #2
0
def plot_3D_db_xau(name):
    # connect db
    conn = connect_to_db_xau()
    cursor = conn.cursor()
    # time handling
    currenttime = getorigintime()
    threemonths = (currenttime -
                   datetime.timedelta(days=90)).strftime('%Y-%m-%d')
    ymdhms = threemonths + ' 00:00:00'

    usd_am = exe_sql_xau(cursor, 'USD (AM)', ymdhms)
    usd_pm = exe_sql_xau(cursor, 'USD (PM)', ymdhms)

    date = exe_sql_xau(cursor, 'date', ymdhms)
    conn.commit()
    cursor.close()
    conn.close()

    # data
    x = [i for i in range(1, len(date) + 1)]
    y = [i if i else 0 for i in usd_am]
    z = [i if i else 0 for i in usd_pm]

    fig = plt.figure()
    ax = fig.gca(projection='3d')

    ax.plot(x, y, zs=0, zdir='z', label='curve in (x,y)', color='Gold')
    ax.scatter(xs=x, zs=z, ys=y, zdir='z', label='points in (x,y,z)', c='Gold')
    ax.legend()
    ax.title.set_color('Navy')
    ax.w_xaxis.set_pane_color((0.2, 0.2, 0.2, 1.0))
    ax.w_yaxis.set_pane_color((0.2, 0.2, 0.2, 1.0))
    ax.w_zaxis.set_pane_color((0.25, 0.25, 0.25, 1.0))
    ax.set_xlabel('Days')
    ax.set_ylabel('USD (AM)')
    ax.set_zlabel('USD (PM)')
    ax.view_init(elev=35, azim=-45)
    plt.xticks(color='Navy')
    plt.yticks(color='Navy')
    ax.tick_params(axis='z', colors='Navy')
    ax.xaxis.label.set_color('Navy')
    ax.yaxis.label.set_color('Navy')
    ax.zaxis.label.set_color('Navy')

    # pwd = os.path.dirname(os.path.dirname(__file__))
    # saveplace = pwd + '/static/pfas/img/' + name + '.png'
    # plt.savefig(saveplace, transparent=True)
    buf = BytesIO()
    plt.savefig(buf, transparent=True, format='png')
    data = base64.b64encode(buf.getbuffer()).decode("ascii")
    return data, name
Example #3
0
def plot_animation_db_xau(name):
    # connect db
    conn = connect_to_db_xau()
    cursor = conn.cursor()
    # time handling
    currenttime = getorigintime()
    threemonths = (currenttime -
                   datetime.timedelta(days=90)).strftime('%Y-%m-%d')
    ymdhms = threemonths + ' 00:00:00'

    usd_am = exe_sql_xau(cursor, 'USD (AM)', ymdhms)
    date = exe_sql_xau(cursor, 'date', ymdhms)

    conn.commit()
    cursor.close()
    conn.close()

    # data
    y = [i if i else 0 for i in usd_am]
    x = [i for i in range(1, len(date) + 1)]
    # moving average for 3 days
    y_new = [(y[i] + y[i + 1] + y[i + 2]) / 3 if 0 < i < len(y) - 3 else np.NaN
             for i in range(len(y) - 2)]
    x_new = [i for i in range(1, len(y) + 1)]
    x_new.pop(0)
    x_new.pop(-1)

    # plot
    fig, ax = plt.subplots()
    ax.grid(True)
    ax.set_xlabel('Days')
    ax.set_ylabel('USD (AM) Price')
    ax.xaxis.label.set_color('#0028FF')
    ax.yaxis.label.set_color('#0028FF')
    line, = ax.plot(x, y, color='#0028FF', label='USD (AM) Price')
    line2, = ax.plot(x_new,
                     y_new,
                     color='#9B6A12',
                     label='Moving Average(3days)')
    ax.legend()
    text_pt = plt.text(4, 0.8, '', fontsize=10, color='#0028FF')
    point_ani, = plt.plot(x[0], y[0], "ro", color='#0028FF')

    ax.spines['top'].set_color('none')
    ax.spines['bottom'].set_color('#0028FF')
    ax.spines['left'].set_color('#0028FF')
    ax.spines['right'].set_color('none')
    plt.xticks(color='#0028FF')
    plt.yticks(color='#0028FF')
    ax1 = plt.gca()
    # ax1.patch.set_facecolor("black")
    xdata = []
    ydata = []

    def init():  # only required for blitting to give a clean slate.
        line.set_ydata(y[0])
        line.set_xdata(x[0])
        return line,

    def animate(i):
        xdata.append(x[i])
        ydata.append(y[i])
        line.set_data(xdata, ydata)
        text_pt.set_position((x[i], y[i]))
        text_pt.set_text("x=%.3f,\n y=%.3f" % (x[i], y[i]))
        point_ani.set_data(x[i], y[i])
        point_ani.set_marker("o")
        point_ani.set_markersize(5)
        return line, point_ani, text_pt

    ani = animation.FuncAnimation(fig,
                                  animate,
                                  init_func=init,
                                  interval=120,
                                  blit=True,
                                  save_count=len(y))

    # pwd = os.path.dirname(os.path.dirname(__file__))
    # saveplace = pwd + '/static/pfas/img/' + name + '.gif'
    # ani.save(saveplace, savefig_kwargs={'transparent': True}, writer='imagemagick')
    # plt.savefig(saveplace, transparent=True)
    # return ani.to_html5_video()
    return ani.to_jshtml(), name
Example #4
0
    def get(self):
        currenttime = getorigintime()
        # recordtime = datetime.datetime.strptime('2019-12-16', "%Y-%m-%d")
        name = ''
        try:
            action = self.get_query_argument('time', '')
            action2 = self.get_query_argument('type', '')
        except Exception:
            action, action2 = '', ''

        if action == 'days' or action == 'tables':
            # 1 week table
            aweek_table = (currenttime -
                           datetime.timedelta(days=6)).strftime('%Y-%m-%d')
            time, name = plot_price_table(aweek_table, gettime())
        elif action == '1week':
            # 1 week
            aweek = (currenttime -
                     datetime.timedelta(days=7)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(aweek, '1week')
        elif action == '2weeks':
            # 2 weeks
            twoweeks = (currenttime -
                        datetime.timedelta(days=14)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(twoweeks, '2weeks')
        elif action == '3weeks':
            # 3 weeks
            threeweeks = (currenttime -
                          datetime.timedelta(days=21)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(threeweeks, '3weeks')
        elif action == '1month':
            # 1 month
            onemonth = (currenttime -
                        datetime.timedelta(days=30)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(onemonth, '1month')
        elif action == '2months':
            # 2 months
            twomonths = (currenttime -
                         datetime.timedelta(days=60)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(twomonths, '2months')
        elif action == '3months':
            # 3 months
            threemonths = (currenttime -
                           datetime.timedelta(days=90)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(threemonths, '3months')
        elif action == '6months':
            # 6 months
            sixmonths = (currenttime -
                         datetime.timedelta(days=180)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(sixmonths, '6months')
        elif action == '1year':
            # 1 year
            oneyear = (currenttime -
                       datetime.timedelta(days=360)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(oneyear, '1year')
        elif action == '2years':
            # 2 years
            twoyears = (currenttime -
                        datetime.timedelta(days=720)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(twoyears, '2years')
        elif action == '3years':
            # 3 years
            threeyears = (currenttime -
                          datetime.timedelta(days=1080)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(threeyears, '3years')
        elif action == '5years':
            # 5 years
            fiveyears = (currenttime -
                         datetime.timedelta(days=1800)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(fiveyears, '5years')
        elif action == '10years':
            # 10 years
            tenyears = (currenttime -
                        datetime.timedelta(days=3600)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(tenyears, '10years')
        elif action == '12years':
            # 12 years
            twelveyears = (currenttime -
                           datetime.timedelta(days=4320)).strftime('%Y-%m-%d')
            time, name = plot_price_trend_db(twelveyears, '12years')
        elif action == '':
            time = ''

        if action2 == '1line-animation':
            type, name = plot_animation_db('All time animation')
        elif action2 == '3d':
            time, name = plot_3D_db('All time 3D')
            type = ''
        elif action2 == 'diy':
            type = ''
            name = 'Please select Date/Data which you need to analyze'
        else:
            type = ''

        # Transfer parameters
        self.render('dashboard_SHFE.html',
                    time=time,
                    type=type,
                    name=name,
                    diychart='')