예제 #1
0
    def get_stock_chart(self):
        # Reset the date index.
        stock_data = self.__reset_date_index()

        # Only keep the number of days requested in chart_params
        stock_data = stock_data.tail(self.days)

        # Make a Bokeh figure
        # Bokeh comes with a list of tools that include xpan and crosshair.
        TOOLS = "xpan,crosshair"
        p = figure(x_axis_type='datetime', tools=TOOLS, plot_width=self.width, plot_height= self.height, title = self.title)
        p.xaxis.major_label_orientation = pi/4
        p.grid.grid_line_alpha=0.3

        mids = (stock_data.Open + stock_data.Close)/2
        spans = abs(stock_data.Close-stock_data.Open)
        inc = stock_data.Close > stock_data.Open
        dec = stock_data.Open >= stock_data.Close
        half_day_in_ms_width = 12*60*60*1000 # half day in 

        # Bokeh glyphs allows you to draw different types of glyphs on your charts....
        # Each candle consists of a rectangle and a segment.  
        p.segment(stock_data.Date, stock_data.High, stock_data.Date, stock_data.Low, color="black")
        # Add the rectangles of the candles going up in price
        p.rect(stock_data.Date[inc], mids[inc], half_day_in_ms_width, spans[inc], fill_color=self.color_up, line_color="black")
        # Add the rectangles of the candles going down in price
        p.rect(stock_data.Date[dec], mids[dec], half_day_in_ms_width, spans[dec], fill_color=self.color_down, line_color="black")

        ############# ADDING INDICATORS ############################
        #p = add_indicators(chart_params["indicators"], stock_data, p)
        p = self.__add_indicators(stock_data, p)

        ############# ADDING HOVER CALLBACK ############################
        # Create a dictionary that I can pass to the javascript callback
        stock_data_dictio = self.__get_stock_data_dict()

        callback_jscode = """
        var stock_dic = %s;         //The dictionary will be replaced here
        var day_im_ms = 24*60*60*1000;

        function formatDate(date) {
            var d = new Date(date),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();
            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;
            return [ month, day, year.toString().substring(2)].join('/');
        }

         var d = cb_data.geometry.x;
         try {
          d = Math.floor( d + day_im_ms);
          d = new Date(d);
        } catch(err) {
           d= err; 
        }

        sel_date = formatDate(d);

        date_lbl = sel_date;
        date_lbl = date_lbl + " open:" + stock_dic[sel_date].Open
        date_lbl = date_lbl + " close:" + stock_dic[sel_date].Close
        date_lbl = date_lbl + " high:" + stock_dic[sel_date].High
        date_lbl = date_lbl + " low:" + stock_dic[sel_date].Low
        date_label.text = date_lbl
        """  % stock_data_dictio   # <--- Observe tha dictionary that is to be replaced into the stock_dic variable

        # This label will display the date and price information:
        date_label = Label(x=30, y=self.height-50, x_units='screen', y_units='screen',
                         text='', render_mode='css',
                         border_line_color='white', border_line_alpha=1.0,
                         background_fill_color='white', background_fill_alpha=1.0)

        date_label.text = ""
        p.add_layout(date_label)

        # When we create the hover callback, we pass the label and the callback code.
        callback = CustomJS(args={'date_label':date_label}, code=callback_jscode)
        p.add_tools(HoverTool(tooltips=None, callback=callback))
        ###################################################################   

        return p
예제 #2
0
        )
p.add_tools(hover)

p.xaxis.ticker = SingleIntervalTicker(interval=10)
p.xaxis.axis_label = "Percentage of temporary workers"
p.yaxis.ticker = SingleIntervalTicker(interval=20)
p.yaxis.axis_label = "Percentage change since 2006"

# show the year as a label on the chart
label = Label(x=30, y=-50, text=str(years[0]), text_font_size='70pt', text_color='#cccccc')
p.add_layout(label)

# set initial data to first year
initial = 2006
cir.data_source.data = sources[initial].data
label.text = str(initial)

# update callback when slider value changes
# label and source for cir updated
def slider_update (attrname, old, new):
    year = slider.value
    label.text = str(year)
    cir.data_source.data = sources[year].data

# add slider
slider = Slider(start=years[0], end=years[-1], value=years[0], step=1, title="Year")
slider.on_change('value', slider_update)

layout = layout([
    [p],
    [slider],