def GetBarChartView(c, state, ccount):
    c.execute(
        '''select count(*) as count, 
                   (case WHEN annualpension<5000 THEN 'a 0<5k' 
                        WHEN annualpension<10000 THEN 'b 5k-10k' 
                        WHEN annualpension<20000 THEN 'c 10k-20k' 
                        WHEN annualpension<30000 THEN 'd 20k-30k' 
                        WHEN annualpension<40000 THEN 'e 30k-40k' 
                        WHEN annualpension<50000 THEN 'f 40k-50k' 
                        WHEN annualpension<60000 THEN 'g 50k-60k' 
                        ELSE 'h +60k' END) as pensionband, 
                 avg(annualpension), avg(finalsalary) 
                 from njpension
                 where state=?
                 group by pensionband
                 order by pensionband
              ''', (state, ))

    rows = list(c)

    yrange = max(60, int(ccount * 0.5))
    chart = pygooglechart.StackedVerticalBarChart(150,
                                                  50,
                                                  y_range=(0, yrange),
                                                  colours=["556600"])
    chart.set_bar_width(13)
    axisbottom = [r[1][-3:-1] for r in rows]
    axisbottom[-1] = ">60"
    chart.set_axis_labels(pygooglechart.Axis.BOTTOM, axisbottom)
    chart.add_data([r[0] for r in rows])
    return chart.get_url()
예제 #2
0
    def get_incorrect_tries_chart(problem):

        solvers = Solver.objects.filter(problem=problem, solved=True)
        FAIL_DISPLAY_LIMIT = 50

        dist = {}
        for entry in solvers.values('incorrect_tries').annotate(
                Count('incorrect_tries')):
            incorrect_tries = min(FAIL_DISPLAY_LIMIT, entry['incorrect_tries'])
            dist[incorrect_tries] = entry['incorrect_tries__count']

        max_fails = max(dist.keys()) if dist else 0
        steps = max(1, max_fails / 10)
        chart = pgc.StackedVerticalBarChart(400, 120)
        chart.add_data([dist.get(i, 0) for i in xrange(max_fails + 1)])
        chart.set_colours(['C02942'])

        def get_label(fails):
            if fails == FAIL_DISPLAY_LIMIT:
                return str(FAIL_DISPLAY_LIMIT) + '+'
            if fails % steps == 0:
                return str(fails)
            return ''

        chart.set_axis_labels(pgc.Axis.BOTTOM,
                              map(get_label, range(max_fails + 1)))
        chart.fill_solid("bg", "65432100")
        return chart.get_url() + '&chbh=r,3'
예제 #3
0
    def get_incorrect_tries_chart(problem):
        solvers = Solver.objects.filter(problem=problem, solved=True)
        dist = {}
        for entry in solvers.values('incorrect_tries').annotate(Count('incorrect_tries')):
            dist[entry['incorrect_tries']] = entry['incorrect_tries__count']

        max_fails = max(dist.keys()) if dist else 0
        steps = max(1, max_fails / 20)
        chart = pgc.StackedVerticalBarChart(400, 120)
        chart.add_data([dist.get(i, 0) for i in xrange(max_fails + 2) ])
        chart.set_colours(['C02942'])
        chart.set_axis_labels(pgc.Axis.BOTTOM,
                              [str(i) if i % steps == 0 else ''
                               for i in xrange(max_fails + 1)])
        chart.fill_solid("bg", "65432100")
        return chart.get_url() + '&chbh=r,3'
예제 #4
0
def get_category_chart(user):
    solved_problems = set()
    for s in Solver.objects.filter(user=user, solved=True):
        solved_problems.add(s.problem)
    problem_count = defaultdict(int)
    solved_count = defaultdict(int)
    # 문제/태그 쌍을 모두 순회하자.
    problem_id = ContentType.objects.get_for_model(Problem).id
    for item in TaggedItem.objects.filter(content_type=problem_id).all():
        problem, tag = item.object, item.tag
        if problem.state != Problem.PUBLISHED: continue
        problem_count[tag] += 1
        if problem in solved_problems:
            solved_count[tag] += 1
    # 문제 수가 많은 순서대로 태그들을 정렬한다
    tags_ordered = sorted([(-value, key)
                           for key, value in problem_count.items()])
    # 문제 수가 가장 많은 n개의 태그를 고른다
    tags_display = [t for _, t in tags_ordered[:8]]
    # 나머지를 "나머지" 카테고리로 묶는다
    others_problems = others_solved = 0
    for tag in problem_count.keys():
        if tag not in tags_display:
            others_problems += problem_count[tag]
            others_solved += solved_count[tag]

    progress = [
        solved_count[tag] * 100 / problem_count[tag] for tag in tags_display
    ]
    labels = [tag.name.encode('utf-8') for tag in tags_display]
    if others_problems > 0:
        progress.append(others_solved * 100 / others_problems)
        labels.append(u'기타'.encode('utf-8'))

    # 구글 차트
    chart = pgc.StackedVerticalBarChart(400, 120, y_range=(0, 100))
    chart.add_data(progress)
    chart.set_grid(0, 25, 5, 5)
    chart.set_colours(['C02942'])
    chart.set_axis_labels(pgc.Axis.LEFT, ["", "25", "50", "75", "100"])
    chart.set_axis_labels(pgc.Axis.BOTTOM, labels)
    chart.fill_solid("bg", "65432100")
    return chart.get_url() + "&chbh=r,3"
예제 #5
0
    def google_chart(klass, data):
        """
        Creates a google chart called 'chart.png'
        """
        colors = []
        names = []
        lines = []
        for calendar in sorted(data['calendars']):
            colors.append(calendar.color)
            names.append(calendar.name)
            line = []
            for tp, events in calendar.ordered_time_periods():
                sum = 0
                for event in events:
                    sum += event.duration
                line.append(sum / 3600)
            lines.append(line)
        #names.reverse()
        #colors.reverse()

        max_y = 0
        y_labels = []
        for (start, tp) in TimePeriod.get_ordered_periods():
            y_labels.append(
                start.strftime("%a %d"))  #"%s/%s" % (start.month, start.day))
            tp_sum = 0
            for event in tp.events:
                tp_sum += event.duration
            if tp_sum / 3600 > max_y:
                max_y = tp_sum / 3600
        max_y = ceil(max_y)

        chart = pygooglechart.StackedVerticalBarChart(1000,
                                                      300,
                                                      y_range=[0, max_y],
                                                      legend=names,
                                                      colours=colors)
        #colours=['000000']*len(data['calendars']),
        #colours_within_series=colors)

        for line in lines:
            chart.add_data(line)

        # Last value is the lowest in the Y axis.
        #chart.add_data([0] * 2)

        def factor(n):
            """
            from: http://blog.dhananjaynene.com/2009/01/2009-is-not-a-prime-number-a-python-program-to-compute-factors/ 
            Get the factors for a number 
            Not optimised for tail recursion 
            """
            if n == 1: return [1]
            i = 2
            limit = n**0.5
            while i <= limit:
                if n % i == 0:
                    ret = factor(n / i)
                    ret.append(i)
                    return ret
                i += 1
            return [n]

        # Some axis data

        t = ['']
        factors = factor(max_y)
        if factors:
            tt = factors[0]
        else:
            tt = max_y
        print tt
        for i in range(int(tt)):
            t.append(max_y / tt * (i + 1))
        chart.set_axis_labels(pygooglechart.Axis.LEFT, t)
        chart.set_axis_labels(pygooglechart.Axis.BOTTOM, y_labels)

        chart.set_bar_width(50)

        chart.download('chart.png')