예제 #1
0
def test_color_mode():
    fig = Figure()

    assert fig.color_mode == 'names'

    fig.color_mode = 'byte'
    assert fig.color_mode == 'byte'

    with pytest.raises(ValueError):
        fig.color_mode = 'rgba'

    with pytest.raises(ValueError):
        fig.color_mode = 15

    fig.plot([0.5], [0.5])

    with pytest.raises(RuntimeError):
        fig.color_mode = 'names'
def create_chart(scriptpath, args):
    timerange = str(args.dayrange)
    interval = ""
    filename = scriptpath + timerange + "range_" + interval + "interval.json"
    urllib3.disable_warnings()
    if not path.isfile(filename) or (
            time() - path.getmtime(filename)) / 60 > 15 or args.force:
        url = "https://bitcoincharts.com/charts/chart.json?m=bitstampUSD&SubmitButton=Draw&r={0}&i={1}"\
            .format(timerange, interval)
        with open(filename, "w") as file:
            dump(requests.get(url, verify=False).json(), file)
    with open(filename, "r") as file:
        response = load(file)

    zipped = list(zip(*response))
    timestamps = zipped[0]
    weighted = zipped[7]
    openprice = weighted[0]
    closeprice = weighted[-1]
    change = (closeprice - openprice) / openprice
    if change > 0:
        color = custom_text_color((38, 200, 0))
    else:
        color = custom_text_color((255, 43, 0))
    print(f'Current price: ${closeprice:.2f}, {color}{change:.2%}{ENDC}')
    if args.log:
        weighted = [log(i) for i in weighted]

    fig = Figure()
    fig.width = int(args.width)
    fig.height = int(args.height)
    fig.x_axis_round = 2
    fig.set_x_limits(min_=min(timestamps), max_=max(timestamps))
    fig.set_y_limits(min_=min(weighted), max_=max(weighted))
    fig.y_label = "USD/BTC"
    fig.color_mode = 'byte'
    fig.y_axis_transform = lambda x: "${:,.2f}".format(x)
    if args.log:
        fig.y_axis_transform = lambda x: "${:,.2f}".format(exp(x))
    fig.x_axis_transform = lambda x: '{:%m-%d-%y}'.format(
        datetime.fromtimestamp(x))
    fig.plot(timestamps, weighted, lc=2, label="Bitcoin price")

    print(fig.show(legend=True))
    print('Data provided by bitcoincharts [{0}]'.format(
        "http://bitcoincharts.com/"))
    print('Last Updated {:.2} minutes ago'.format(
        (time() - path.getmtime(filename)) / 60))