def get_display(my):

        top = my.top

        #top.add_gradient("background", "background", 5, -20)
        top.add_color("background", "background", -5)
        #top.add_style("padding-top: 10px")

        #title = "Sample Chart"
        title = my.kwargs.get("title")
        if title:
            date = "@FORMAT(@STRING($TODAY),'Dec 31, 1999')"
            date = Search.eval(date, single=True)

            title_wdg = DivWdg()
            top.add(title_wdg)
            title_wdg.add(title)
            title_wdg.add(" [%s]" % date)
            title_wdg.add_style("font-size: 1.1em")
            title_wdg.add_color("background", "background3")
            title_wdg.add_color("color", "color3")
            title_wdg.add_style("padding: 10px")
            title_wdg.add_style("font-weight: bold")
            title_wdg.add_style("text-align: center")

        colors = [
            'rgba(255,0,0,0.5)',
            'rgba(0,255,0,0.5)',
            'rgba(0,0,255,0.5)',
            'rgba(128,0,255,0.5)',
            'rgba(0,128,255,0.5)',
            'rgba(255,0,255,0.5)',
        ]

        # draw a legend
        legend = None
        from chart2_wdg import ChartLegend

        #import pdb; pdb.set_trace();

        labels = my.kwargs.get("labels")
        my.user = my.kwargs.get("user")

        if labels:
            legend = ChartLegend()
            labels = labels.split("|")
            legend.set_labels(labels)
            top.add(legend)
            legend.add_style("width: %s" % str(len(labels)*200))
            legend.add_style("margin-left: auto")
            legend.add_style("margin-right: auto")

            #legend.add_style("width: 200px")
            #legend.add_style("position: absolute")
            #legend.add_style("top: 40px")
            #legend.add_style("left: 300px")


        if legend:
            legend.set_colors(colors)


        #############
        # table for start-end date
        search_table = Table()
        search_table.add_style("margin-left: auto")
        search_table.add_style("margin-right: auto")
        search_table.add_style("margin-top: 35px")
        search_table.add_style("margin-bottom: 45px")
        top.add(search_table)
 
        search_table.add_row()

        td = search_table.add_cell()
        start_date = DivWdg("Start Date")
        td.add(start_date)
        start_date.add_style("margin-left: 5px")
        #td = table.add_cell()
        #op = DivWdg(" between   ")
        #op.add_style("margin-left: 5px")
        #td.add(op)

        from tactic.ui.widget import CalendarInputWdg
        td = search_table.add_cell()
        cal1 = CalendarInputWdg("start_date")
        td.add(cal1)

        search_table.add_row()
        end_date = DivWdg("End Date")
        end_date.add_style("margin-left: 5px")
        td = search_table.add_cell()
        td.add(end_date)
        #td.add(spacing)

        td = search_table.add_cell()
        cal2 = CalendarInputWdg("end_date")
        td.add(cal2)

        search_table.add_row()
        td = search_table.add_cell()

        from pyasm.widget import ButtonWdg
        button = ButtonWdg()
        #button.add_style("width: %s" % top_width)
        button.add_class('spt_label')

        icon = my.kwargs.get("icon")
        if icon:
            icon_div = DivWdg()
            icon = IconWdg(title, icon, width=16 )
            icon_div.add(icon)
            button.add(icon_div)
            my.table.add_style("position: relative")
            icon_div.add_style("position: absolute")
            icon_div.add_style("left: 5px")
            icon_div.add_style("top: 6px")
            title = "     %s" % title
            button.add_style("padding: 2px")

        button.set_name("Reload")

        td.add(button)
        #############


        task_data = my.get_task_data()
        
        labels = [label[0] for label in task_data.get(task_data.keys()[0])]

        #labels = ['chr001', 'chr002', 'chr003', 'chr004', 'prop001', 'prop002', 'cow001']
        #labels = ['week 1', 'week 2', 'week 3', 'week 4', 'week 5', 'week 6', 'week 7', 'week 8']
        #values = [1,2,4,5,6,7,8]
        values = [i+1 for i in range(len(labels))]

        width = my.kwargs.get("width")
        if not width:
            width = '1600px'
        height = my.kwargs.get("height")
        if not height:
            height = '1000px'


        chart_div = DivWdg()
        top.add(chart_div)
        chart_div.add_style("text-align: center")

        chart = ChartWdg(
            height=height,
            width=width,
            chart_type='bar',
            labels=labels
        )
        chart_div.add(chart)

        data = ChartData(
            color=colors[0], #"rgba(255, 0, 0, 1.0)",
            #data=[task_data['Pending'], 5.5, 7.5, 14.3, 10.2, 1.1, 3.3],
            data = [data[1] for data in task_data.get('Pending')]
        )
        chart.add(data)

        data = ChartData(
            color=colors[1], #"rgba(0, 255, 0, 1.0)",
            #data=[task_data['Assignment'], 4.3, 8.4, 6.2, 8.4, 2.2],
            data = [data[1] for data in task_data.get('Assignment')]
        )
        chart.add(data)


        data = ChartData(
            color=colors[2], #"rgba(0, 0, 255, 1.0)",
            #data=[task_data['In Progress'], 3.5, 2.2, 6.6, 1.3, 9.4],
            data = [data[1] for data in task_data.get('In Progress')]
        )
        chart.add(data)


        #data = [task_data['Approved'], 17, 15.5, -3, 17, 16.8, 11.4]
        data = [data[1] for data in task_data.get('Approved')]
        data = ChartData(data=data, color=colors[3]) #"rgba(128, 0, 255, 1.0)")
        chart.add(data)


        data = [data[1] for data in task_data.get('Review')] 
        data = ChartData(data=data, color=colors[4]) #"rgba(0, 128, 255, 1.0)")
        chart.add(data)

        table = Table()
        table.add_color("color", "color")
        top.add(table)
        table.add_row()
        table.center()
        table.add_style("width: 1%")


        x_title = my.kwargs.get("x_title")
        y_title = my.kwargs.get("y_title")

        if y_title:
            y_title = y_title.replace(" ", " ")

            y_axis_div = DivWdg()
            td = table.add_cell(y_axis_div)
            td.add_style("vertical-align: middle")
            td.add_style("width: 1%")
            y_axis_div.add(y_title)
            y_axis_div.add_style("-moz-transform: rotate(-90deg)")
            y_axis_div.add_style("-webkit-transform: rotate(-90deg)")
            y_axis_div.add_style("font-size: 1.33em")
            y_axis_div.add_style("height: 100%")
            y_axis_div.add_style("width: 30px")

        table.add_row()

        # add the x-axis title
        if x_title:
            x_title = x_title.replace(" ", " ")

            x_axis_div = DivWdg()
            td = table.add_cell(x_axis_div)
            td.add_style("text-align: center")
            td.add_style("width: 1%")
            x_axis_div.add(x_title)
            x_axis_div.add_style("font-size: 1.33em")
            x_axis_div.add_style("width: 100%")
            x_axis_div.add_style("height: 30px")

        return top
    def get_display(my):

        top = my.top
        top.add_style("position: relative")

        labels = my.kwargs.get("labels")
        if not labels:
            labels = []
        label_values = my.kwargs.get("label_values")

        width = my.kwargs.get("width")
        height = my.kwargs.get("height")

        default_chart_type = my.kwargs.get("chart_type")
        if not default_chart_type:
            default_chart_type = 'bar'

        canvas = Canvas()
        top.add(canvas)
        canvas.set_id("chart1")
        canvas.add_attr("width", width)
        canvas.add_attr("height", height)
        canvas.add("Your web-browser does not support the HTML 5 canvas element.")

        canvas.add_behavior( {
            'type': 'load',
            'cbjs_action': ONLOAD_JS
        } )


        bar_chart_index = 0
        num_bar_charts = 0
        for widget in my.widgets:
            # count the number of bar charts
            chart_type = widget.get_chart_type()
            if not chart_type:
                chart_type = default_chart_type
                widget.set_chart_type(chart_type)


            if chart_type == 'bar':
                num_bar_charts += 1



        # auto figure out the range
        xmax = 1
        ymax = 0

        for widget in my.widgets:

            # count the number of bar charts
            chart_type = widget.get_chart_type()
            if not chart_type:
                chart_type = default_chart_type
                widget.set_chart_type(chart_type)


            widget.set_index(bar_chart_index, num_bar_charts)
            if chart_type == 'bar':
                bar_chart_index += 1


            # remember the largest value
            data = widget.get_data()
            for value in data:
                if value > ymax:
                    ymax = value


            x_data = widget.get_xdata()
            if not x_data:
                if len(data)-1 > xmax:
                    xmax = len(data)-1
            else:
                last = x_data[-1]
                if last > xmax:
                    xmax = last

            top.add(widget)

        # FIXME: doesn't handle small numbers too well
        ymax += 1

        if len(labels) > xmax:
            xmax = len(labels)
        if not xmax:
            xmax = 1



        # initialize the canvas
        canvas.add_behavior( {
            'type': 'load',
            'xmax': xmax,
            'ymax': ymax,
            'cbjs_action': '''
            spt.chart.top = bvr.src_el;
            spt.chart.set_range(bvr.xmax, bvr.ymax);
            '''
        } )


        # draw the grid
        grid = ChartGrid(labels=labels, label_values=label_values)
        top.add(grid)


        # draw a legend
        if my.kwargs.get("legend"):
            legend = ChartLegend(labels=my.kwargs.get('legend'))
            legend.add_style("position: absolute")
            legend.add_style("left: %s" % 50)
            legend.add_style("top: %s" % 10)

            top.add(legend)

        return top
    def get_display(my):

        top = my.top

        top.add_color("background", "background", -5)
        top.add_gradient("color", "color")

        #top.add_style("background", "#000")
        #top.add_style("opacity: 0.95")
        #top.add_style("color: #FFF")

        #top.add_style("padding-top: 10px")
        top.add_style("position: relative")

        title = my.kwargs.get("title")
        if title:
            title_wdg = my.get_title_wdg(title)
            top.add(title_wdg)

        # get the column to use as a date for searching
        my.column = my.kwargs.get("column")
        if not my.column:
            my.column = 'timestamp'


        # elements
        elements = my.kwargs.get("elements")
        if elements:
            if isinstance(elements, basestring):
                elements = elements.split("|")
        elif my.kwargs.get("chart_data"):
            elements = []
        else:
            elements = ['{@COUNT()}']


        # set some start and end dates
        start_date = my.kwargs.get("start_date")
        if not start_date:
            start_date = None
        else:
            if start_date.startswith("{") and start_date.endswith("}"):
                start_date = Search.eval(start_date)

            start_date = parser.parse(start_date)

        end_date = my.kwargs.get("end_date")
        if not end_date:
            end_date = None
        else:
            if end_date.startswith("{") and end_date.endswith("}"):
                end_date = Search.eval(end_date)
            end_date = parser.parse(end_date)




        expression = my.kwargs.get("expression")
        search_type = my.kwargs.get("search_type")
        if expression:
            sobjects = Search.eval(expression)
        elif search_type:
            search = Search(search_type)
            if start_date:
                search.add_filter(my.column, start_date, op=">")
            if end_date:
                search.add_filter(my.column, end_date, op="<")
            sobjects = search.get_sobjects()

        else:
            sobjects = []


        # Is this a plot or a chart
        # A plot puts the X-axis at the right place.
        # A chart has a interval in which data is combined

        my.interval = my.kwargs.get("interval")
        if not my.interval:
            my.interval = 'weekly'
            #my.interval = 'monthly'

        min_date = None
        max_date = None

        if not sobjects:
            if not start_date:
                min_date = datetime.today() - timedelta(days=30)
            else:
                min_date = start_date

            if not end_date:
                max_date = datetime.today() + timedelta(days=30)
            else:
                max_date = end_date

        for sobject in sobjects:
            timestamp = sobject.get_value(my.column)
            timestamp = parser.parse(timestamp)
            if min_date == None or timestamp < min_date:
                min_date = timestamp
            if max_date == None or timestamp > max_date:
                max_date = timestamp


        # defined the buckets based on interval
        dates = [] 
        if my.interval == 'weekly':
            min_date = datetime(min_date.year, min_date.month, min_date.day)
            max_date = datetime(max_date.year, max_date.month, max_date.day)
            min_date = min_date - timedelta(days=8)
            max_date = max_date + timedelta(days=8)

            dates = list(rrule.rrule(rrule.WEEKLY, byweekday=0, dtstart=min_date, until=max_date))

        elif my.interval == 'monthly':
            min_date = datetime(min_date.year, min_date.month, 1)
            if max_date.month == 12:
                year = max_date.year+1
                month = 1
            else:
                year = max_date.year
                month = max_date.month + 1
            
            max_date = datetime(year, month, 1)
            dates = list(rrule.rrule(rrule.MONTHLY, bymonthday=1, dtstart=min_date, until=max_date))


        my.dates_dict = {}
        for date in dates:
            my.dates_dict[str(date)] = []


        for sobject in sobjects:
            timestamp = sobject.get_value(my.column)
            timestamp = parser.parse(timestamp)

            if my.interval == "weekly":
                # put in the week
                timestamp = list(rrule.rrule(rrule.WEEKLY, byweekday=0, dtstart=timestamp-timedelta(days=7), count=1))
                timestamp = timestamp[0]
                timestamp = datetime(timestamp.year,timestamp.month,timestamp.day)
            else:
                timestamp = datetime(timestamp.year,timestamp.month,1)

            if my.dates_dict:
            	week_sobjects = my.dates_dict[str(timestamp)]
            	week_sobjects.append(sobject)



        # get all the chart labels
        chart_labels = []
        for date in dates:
            if my.interval == 'weekly':
                #chart_labels.append("Week %s" % date.strftime("%W"))
                label = (date + timedelta(days=6)).strftime("%d")
                chart_labels.append("%s - %s" % (date.strftime("%b %d"), label))
            else:
                chart_labels.append(date.strftime("%b %Y"))


        my.sobjects = sobjects


        width = my.kwargs.get("width")
        if not width:
            width = "800px"

        height = my.kwargs.get("height")
        if not height:
            height = "500px"


        x_title = my.kwargs.get("x_title")
        #x_title = Search.eval(x_title)
        y_title = my.kwargs.get("y_title")
        #y_title = Search.eval(y_title)




        # draw a legend
        legend = None
        from chart2_wdg import ChartLegend
        labels = my.kwargs.get("labels")
        if labels:
            legend = ChartLegend()
            labels = labels.split("|")
            legend.set_labels(labels)
            top.add(legend)
            legend.add_style("width: %s" % str(len(labels)*200))
            legend.add_style("margin-left: auto")
            legend.add_style("margin-right: auto")
            legend.add_style("margin-top: 5px")

            #legend.add_style("width: 200px")
            #legend.add_style("position: absolute")
            #legend.add_style("top: 40px")
            #legend.add_style("left: 300px")




        table = Table()
        table.add_color("color", "color")
        top.add(table)
        table.add_row()
        table.center()
        table.add_style("width: 1%")


        if y_title:
            y_title = y_title.replace(" ", "&nbsp;")

            y_axis_div = DivWdg()
            td = table.add_cell(y_axis_div)
            td.add_style("vertical-align: middle")
            td.add_style("width: 1%")
            y_axis_div.add(y_title)
            y_axis_div.add_style("-moz-transform: rotate(-90deg)")
            y_axis_div.add_style("-webkit-transform: rotate(-90deg)")
            y_axis_div.add_style("font-size: 1.33em")
            y_axis_div.add_style("height: 100%")
            y_axis_div.add_style("width: 30px")




        chart = ChartWdg(
            width=width,
            height=height,
            chart_type='bar',
            #legend=my.elements,
            labels=chart_labels,
            label_values=[i+0.5 for i,x in enumerate(chart_labels)]
        )
        table.add_cell(chart)


        chart_type = my.kwargs.get("chart_type")
        if not chart_type:
            chart_type = 'bar'

        my.colors = [
            'rgba(0,255,0,0.5)',
            'rgba(0,0,255,0.5)',
            'rgba(255,0,0,0.5)',
            'rgba(255,255,0,0.5)',
            'rgba(0,255,255,0.5)',
            'rgba(255,0,255,0.5)',
        ]


        if legend:
            legend.set_colors(my.colors)


        element_count = 0

        x_data=[i+0.5 for i,x in enumerate(chart_labels)]
        for i, element in enumerate(elements):


            data_values = my.get_data_values(my.dates_dict, dates, element, my.sobjects)

            chart_data = ChartData(
                chart_type=chart_type,
                data=data_values,
                color=my.colors[element_count],
                x_data=x_data
            )
            chart.add(chart_data)
            element_count += 1



        # add in individual charts
        chart_data = my.kwargs.get("chart_data")
        if chart_data and isinstance(chart_data, basestring):
            chart_data = jsonloads(chart_data)

        if not chart_data:
            chart_data = []
        else:
            # draw back to front
            chart_data.reverse()

        for options in chart_data:

            column = options.get("column")
            if not column:
                column = my.column


            expression = options.get("expression")
            if expression:

                # extra filters
                extra = {}
                #extra['sthpw/task'] = []
                #if start_date:
                #    extra['sthpw/task'].append([column, '>', start_date])
                #if end_date:
                #    extra['sthpw/task'].append([column, '<', end_date])


                sobjects = Search.eval(expression, extra_filters=extra)
                dates_dict = my.get_dates_dict(sobjects, dates, column)
            else:
                sobjects = my.sobjects
                dates_dict = my.dates_dict

            data = my.get_data_values(dates_dict, dates, options['element'], sobjects)


            options['data'] = data
            options['x_data'] = x_data
            if not options.get("color"):
                options['color'] = my.colors[element_count]

            if not options.get("chart_type"):
                options['chart_type'] = chart_type
                
            str_options = {}
            for x, y in options.items():
                str_options[str(x)] = y

            chart_data = ChartData(**str_options)

            chart.add(chart_data)

            element_count += 1


        table.add_row()


        # add the x-axis title
        if x_title:
            x_title = x_title.replace(" ", "&nbsp;")

            x_axis_div = DivWdg()
            td = table.add_cell(x_axis_div)
            td.add_style("text-align: center")
            td.add_style("width: 1%")
            x_axis_div.add(x_title)
            x_axis_div.add_style("font-size: 1.33em")
            x_axis_div.add_style("width: 100%")
            x_axis_div.add_style("height: 30px")


        return top
    def get_display(my):
        my.preprocess()

        if not my.x_axis:
            chart_labels = [x.get_code() for x in my.sobjects]
        else:
            try:
                chart_labels = [x.get_value(my.x_axis) for x in my.sobjects]
            except:
                # FIXME ... put in some special logic for users since it
                # is used so often in charting
                if my.search_type == 'sthpw/login':
                    chart_labels = [x.get_value("login") for x in my.sobjects]
                else:
                    chart_labels = [x.get_code() for x in my.sobjects]


        top = DivWdg()
        top.add_class("spt_chart")
        my.set_as_panel(top)
        top.add_style("position: relative")

        title = my.kwargs.get("title")
        if title:
            title_wdg = my.get_title_wdg(title)
            top.add(title_wdg)


        if not my.sobjects:
            top.add("No results found")
            return top



        element_data = {}

        chart_labels = []
        element_values = []

        # get the labels and values for each sobject
        for sobject in my.sobjects:

            chart_labels.append( sobject.get_code() )

            values, labels = my.get_data(sobject)
            for value, label in zip(values, labels):

                data = element_data.get(label)
                if data == None:
                    data = []
                    element_data[label] = data
                data.append(value)



        width = my.kwargs.get("width")
        if not width:
            width = '800px'
        height = my.kwargs.get("height")
        if not height:
            height = '500px'


        chart_div = DivWdg()
        chart_div.add_style("width", width)
        chart_div.add_style("height", height)
        chart_div.center()

        if not my.sobjects:
            msg_div = DivWdg()
            msg_div.add_style("position: absolute")
            chart_div.add(msg_div)
            msg_div.add_style("width: 200px")
            msg_div.add_style("height: 15px")
            msg_div.add_style("padding: 50px")
            msg_div.add_style("margin-top: 200px")
            msg_div.add("<b>No results found</b>")
            msg_div.add_border()
            msg_div.add_color("color", "color3")
            msg_div.add_color("background", "background3")
            msg_div.add_style("top: 0px")
            msg_div.add_style("left: 350")
            msg_div.add_style("z-index: 100")
            msg_div.add_style("text-align: center")


        chart = ChartWdg(
            width=width,
            height=height,
            chart_type='bar',
            #legend=my.elements,
            labels=chart_labels,
            label_values=[i+0.5 for i,x in enumerate(chart_labels)]
        )
        chart_div.add(chart)

        top.add(chart_div)
        top.add_color("background", "background", -5)
        top.add_color("color", "color")



        # draw a legend
        from chart2_wdg import ChartLegend
        legend = ChartLegend(labels=my.elements)
        top.add(legend)
        #legend.add_style("width: 200px")
        legend.add_style("position: absolute")
        legend.add_style("top: 0px")
        legend.add_style("left: 0px")



        for i, key in enumerate(element_data.keys()):

            if my.colors:
                color = my.colors[i]
            else:
                color = 'rgba(128, 0, 0, 1.0)'

            element_values = element_data.get(key)

            chart_data = ChartData(
                chart_type=my.chart_types[i],
                color=color,
                data=element_values,
                x_data=[i+0.5 for i,x in enumerate(chart_labels)]
            )
            chart.add(chart_data)


        return top
Exemplo n.º 5
0
    def get_display(my):

        top = my.top

        top.add_color("background", "background", -5)
        top.add_gradient("color", "color")

        #top.add_style("background", "#000")
        #top.add_style("opacity: 0.95")
        #top.add_style("color: #FFF")

        #top.add_style("padding-top: 10px")
        top.add_style("position: relative")

        title = my.kwargs.get("title")
        if title:
            title_wdg = my.get_title_wdg(title)
            top.add(title_wdg)

        # get the column to use as a date for searching
        my.column = my.kwargs.get("column")
        if not my.column:
            my.column = 'timestamp'


        # elements
        elements = my.kwargs.get("elements")
        if elements:
            if isinstance(elements, basestring):
                elements = elements.split("|")
        elif my.kwargs.get("chart_data"):
            elements = []
        else:
            elements = ['{@COUNT()}']


        # set some start and end dates
        start_date = my.kwargs.get("start_date")
        if not start_date:
            start_date = None
        else:
            if start_date.startswith("{") and start_date.endswith("}"):
                start_date = Search.eval(start_date)

            start_date = parser.parse(start_date)

        end_date = my.kwargs.get("end_date")
        if not end_date:
            end_date = None
        else:
            if end_date.startswith("{") and end_date.endswith("}"):
                end_date = Search.eval(end_date)
            end_date = parser.parse(end_date)




        expression = my.kwargs.get("expression")
        search_type = my.kwargs.get("search_type")
        if expression:
            sobjects = Search.eval(expression)
        elif search_type:
            search = Search(search_type)
            if start_date:
                search.add_filter(my.column, start_date, op=">")
            if end_date:
                search.add_filter(my.column, end_date, op="<")
            sobjects = search.get_sobjects()

        else:
            sobjects = []


        # Is this a plot or a chart
        # A plot puts the X-axis at the right place.
        # A chart has a interval in which data is combined

        my.interval = my.kwargs.get("interval")
        if not my.interval:
            my.interval = 'weekly'
            #my.interval = 'monthly'

        min_date = None
        max_date = None

        if not sobjects:
            if not start_date:
                min_date = datetime.today() - timedelta(days=30)
            else:
                min_date = start_date

            if not end_date:
                max_date = datetime.today() + timedelta(days=30)
            else:
                max_date = end_date

        for sobject in sobjects:
            timestamp = sobject.get_value(my.column)
            timestamp = parser.parse(timestamp)
            if min_date == None or timestamp < min_date:
                min_date = timestamp
            if max_date == None or timestamp > max_date:
                max_date = timestamp



        # defined the buckets based on interval
        dates = [] 
        if my.interval == 'weekly':
            min_date = datetime(min_date.year, min_date.month, min_date.day)
            max_date = datetime(max_date.year, max_date.month, max_date.day)
            min_date = min_date - timedelta(days=8)
            max_date = max_date + timedelta(days=8)

            dates = list(rrule.rrule(rrule.WEEKLY, byweekday=0, dtstart=min_date, until=max_date))


        elif my.interval == 'daily':
            min_date = datetime(min_date.year, min_date.month, min_date.day)
            max_date = datetime(max_date.year, max_date.month, max_date.day)
            min_date = min_date - timedelta(days=1)
            #max_date = max_date + timedelta(days=1)

            dates = list(rrule.rrule(rrule.DAILY, dtstart=min_date, until=max_date))


        elif my.interval == 'monthly':
            min_date = datetime(min_date.year, min_date.month, 1)
            if max_date.month == 12:
                year = max_date.year+1
                month = 1
            else:
                year = max_date.year
                month = max_date.month + 1
            
            max_date = datetime(year, month, 1)
            dates = list(rrule.rrule(rrule.MONTHLY, bymonthday=1, dtstart=min_date, until=max_date))


        my.dates_dict = {}
        for date in dates:
            my.dates_dict[str(date)] = []


        # put the appropriate sobjects in each date_dict item
        for sobject in sobjects:
            timestamp = sobject.get_value(my.column)
            timestamp = parser.parse(timestamp)

            if my.interval == "weekly":
                # put in the week
                timestamp = list(rrule.rrule(rrule.WEEKLY, byweekday=0, dtstart=timestamp-timedelta(days=7), count=1))
                timestamp = timestamp[0]
                timestamp = datetime(timestamp.year,timestamp.month,timestamp.day)
            elif my.interval == "daily":
                timestamp = datetime(timestamp.year,timestamp.month,timestamp.day)

            else:
                timestamp = datetime(timestamp.year,timestamp.month,1)

            if my.dates_dict:
            	interval_sobjects = my.dates_dict[str(timestamp)]
            	interval_sobjects.append(sobject)



        # get all the chart labels
        chart_labels = []
        for date in dates:
            if my.interval == 'weekly':
                #chart_labels.append("Week %s" % date.strftime("%W"))
                label = (date + timedelta(days=6)).strftime("%d")
                chart_labels.append("%s - %s" % (date.strftime("%b %d"), label))
            elif my.interval == 'daily':
                chart_labels.append(date.strftime("%b %d"))
            else:
                chart_labels.append(date.strftime("%b %Y"))


        my.sobjects = sobjects


        width = my.kwargs.get("width")
        if not width:
            width = "800px"

        height = my.kwargs.get("height")
        if not height:
            height = "500px"


        x_title = my.kwargs.get("x_title")
        #x_title = Search.eval(x_title)
        y_title = my.kwargs.get("y_title")
        #y_title = Search.eval(y_title)




        # draw a legend
        legend = None
        from chart2_wdg import ChartLegend
        labels = my.kwargs.get("labels")
        if labels:
            legend = ChartLegend()
            labels = labels.split("|")
            legend.set_labels(labels)
            top.add(legend)
            legend.add_style("width: %s" % str(len(labels)*200))
            legend.add_style("margin-left: auto")
            legend.add_style("margin-right: auto")
            legend.add_style("margin-top: 5px")

            #legend.add_style("width: 200px")
            #legend.add_style("position: absolute")
            #legend.add_style("top: 40px")
            #legend.add_style("left: 300px")




        table = Table()
        table.add_color("color", "color")
        top.add(table)
        table.add_row()
        table.center()
        table.add_style("width: 1%")


        if y_title:
            y_title = y_title.replace(" ", "&nbsp;")

            y_axis_div = DivWdg()
            td = table.add_cell(y_axis_div)
            td.add_style("vertical-align: middle")
            td.add_style("width: 1%")
            y_axis_div.add(y_title)
            y_axis_div.add_style("-moz-transform: rotate(-90deg)")
            y_axis_div.add_style("-webkit-transform: rotate(-90deg)")
            y_axis_div.add_style("font-size: 1.33em")
            y_axis_div.add_style("height: 100%")
            y_axis_div.add_style("width: 30px")



        rotate_x_axis = my.kwargs.get("rotate_x_axis")

        chart = ChartWdg(
            width=width,
            height=height,
            chart_type='bar',
            #legend=my.elements,
            labels=chart_labels,
            label_values=[i+0.5 for i,x in enumerate(chart_labels)],
            rotate_x_axis=rotate_x_axis,
        )
        table.add_cell(chart)


        chart_type = my.kwargs.get("chart_type")
        if not chart_type:
            chart_type = 'bar'

        my.colors = [
            'rgba(0,255,0,0.5)',
            'rgba(0,0,255,0.5)',
            'rgba(255,0,0,0.5)',
            'rgba(255,255,0,0.5)',
            'rgba(0,255,255,0.5)',
            'rgba(255,0,255,0.5)',
        ]


        if legend:
            legend.set_colors(my.colors)


        element_count = 0

        x_data=[i+0.5 for i,x in enumerate(chart_labels)]
        for i, element in enumerate(elements):


            data_values = my.get_data_values(my.dates_dict, dates, element, my.sobjects)

            chart_data = ChartData(
                chart_type=chart_type,
                data=data_values,
                color=my.colors[element_count],
                x_data=x_data
            )
            chart.add(chart_data)
            element_count += 1



        # add in individual charts
        chart_data = my.kwargs.get("chart_data")
        if chart_data and isinstance(chart_data, basestring):
            chart_data = jsonloads(chart_data)

        if not chart_data:
            chart_data = []
        else:
            # draw back to front
            chart_data.reverse()

        for options in chart_data:

            column = options.get("column")
            if not column:
                column = my.column


            expression = options.get("expression")
            if expression:

                # extra filters
                extra = {}
                #extra['sthpw/task'] = []
                #if start_date:
                #    extra['sthpw/task'].append([column, '>', start_date])
                #if end_date:
                #    extra['sthpw/task'].append([column, '<', end_date])


                sobjects = Search.eval(expression, extra_filters=extra)
                dates_dict = my.get_dates_dict(sobjects, dates, column)
            else:
                sobjects = my.sobjects
                dates_dict = my.dates_dict

            data = my.get_data_values(dates_dict, dates, options['element'], sobjects)


            options['data'] = data
            options['x_data'] = x_data
            if not options.get("color"):
                options['color'] = my.colors[element_count]

            if not options.get("chart_type"):
                options['chart_type'] = chart_type
                
            str_options = {}
            for x, y in options.items():
                str_options[str(x)] = y

            chart_data = ChartData(**str_options)

            chart.add(chart_data)

            element_count += 1


        table.add_row()


        # add the x-axis title
        if x_title:
            x_title = x_title.replace(" ", "&nbsp;")

            x_axis_div = DivWdg()
            td = table.add_cell(x_axis_div)
            td.add_style("text-align: center")
            td.add_style("width: 1%")
            x_axis_div.add(x_title)
            x_axis_div.add_style("font-size: 1.33em")
            x_axis_div.add_style("width: 100%")
            x_axis_div.add_style("height: 30px")


        return top
Exemplo n.º 6
0
    def get_display(my):
        my.preprocess()

        if not my.x_axis:
            chart_labels = [x.get_code() for x in my.sobjects]
        else:
            try:
                chart_labels = [x.get_value(my.x_axis) for x in my.sobjects]
            except:
                # FIXME ... put in some special logic for users since it
                # is used so often in charting
                if my.search_type == 'sthpw/login':
                    chart_labels = [x.get_value("login") for x in my.sobjects]
                else:
                    chart_labels = [x.get_code() for x in my.sobjects]


        top = DivWdg()
        top.add_class("spt_chart")
        my.set_as_panel(top)
        top.add_style("position: relative")

        title = my.kwargs.get("title")
        if title:
            title_wdg = my.get_title_wdg(title)
            top.add(title_wdg)


        if not my.sobjects:
            top.add("No results found")
            return top



        element_data = {}

        chart_labels = []
        element_values = []

        # get the labels and values for each sobject
        for sobject in my.sobjects:

            chart_labels.append( sobject.get_code() )

            values, labels = my.get_data(sobject)
            for value, label in zip(values, labels):

                data = element_data.get(label)
                if data == None:
                    data = []
                    element_data[label] = data
                data.append(value)



        width = my.kwargs.get("width")
        if not width:
            width = '800px'
        height = my.kwargs.get("height")
        if not height:
            height = '500px'


        chart_div = DivWdg()
        chart_div.add_style("width", width)
        chart_div.add_style("height", height)
        chart_div.center()

        if not my.sobjects:
            msg_div = DivWdg()
            msg_div.add_style("position: absolute")
            chart_div.add(msg_div)
            msg_div.add_style("width: 200px")
            msg_div.add_style("height: 15px")
            msg_div.add_style("padding: 50px")
            msg_div.add_style("margin-top: 200px")
            msg_div.add("<b>No results found</b>")
            msg_div.add_border()
            msg_div.add_color("color", "color3")
            msg_div.add_color("background", "background3")
            msg_div.add_style("top: 0px")
            msg_div.add_style("left: 350")
            msg_div.add_style("z-index: 100")
            msg_div.add_style("text-align: center")


        chart = ChartWdg(
            width=width,
            height=height,
            chart_type='bar',
            #legend=my.elements,
            labels=chart_labels,
            label_values=[i+0.5 for i,x in enumerate(chart_labels)]
        )
        chart_div.add(chart)

        top.add(chart_div)
        top.add_color("background", "background", -5)
        top.add_color("color", "color")



        # draw a legend
        from chart2_wdg import ChartLegend
        legend = ChartLegend(labels=my.elements)
        top.add(legend)
        #legend.add_style("width: 200px")
        legend.add_style("position: absolute")
        legend.add_style("top: 0px")
        legend.add_style("left: 0px")



        for i, key in enumerate(element_data.keys()):

            if my.colors:
                color = my.colors[i]
            else:
                color = 'rgba(128, 0, 0, 1.0)'

            element_values = element_data.get(key)

            chart_data = ChartData(
                chart_type=my.chart_types[i],
                color=color,
                data=element_values,
                x_data=[i+0.5 for i,x in enumerate(chart_labels)]
            )
            chart.add(chart_data)


        return top