def _plot_pie(self, options, args, fh): """Plot a pie chart""" from pygooglechart import PieChart3D, PieChart2D delimiter = options.delimiter field = options.field - 1 chart = PieChart2D(options.width, options.height) pts = [] for l in imap(lambda x: x.strip(), fh): splitted_line = l.split(delimiter) k = int(splitted_line.pop(field)) pts.append( (k, ' '.join(splitted_line), locale.format('%d', k, True))) if options.get('limit', None): # Only wanna use top N samples by key, sort and truncate pts = sorted(pts, key=itemgetter(0), reverse=True)[:options.limit] if not pts: raise ValueError("No data to plot") data, labels, legend = zip(*pts) chart.add_data(data) chart.set_pie_labels(labels) if options.get('legend', None) is True: chart.set_legend(map(str, legend)) return chart
def graph_participation(q): #question = get_object_or_404(Question, pk=q.pk) question = Question.objects.get(pk=q.pk) # grab ALL entries for this question entries = Entry.objects.filter(question=question) # look up how many people were asked this question # and make a ratio # if None, use 0 if question.sent_to: participation = float(len(entries)) / float(question.sent_to) else: participation = 0.0 # normalize data pending = 100 * (1.0 - participation) participants = 100 - pending for size in GRAPH_SIZES: # configure and save the graph pie = PieChart2D(int(size), golden(int(size))) pie.add_data([pending, participants]) pie.set_legend(['Pending', 'Respondants']) pie.set_colours(['0091C7', '0FBBD0']) filename = GRAPH_DIR + str( question.pk) + '-' + size + '-participation.png' pie.download(filename) print 'saved ' + filename return 'graphed participation ' + question.text
def pie(size, title, labels, legend, data, colors): """ Function for creating a pie chart. Parameters ---------- size : list width and height title : str title of the graph lables : list list of string containing labels of the data legend : list list of string containing legend of the data data : list data to be plotted colors : list list of string containing the colors of each data in PRGB format Returns ------- PieChart2D object """ graph = PieChart2D(size[0], size[1]) graph.set_title(title) graph.set_legend(legend) graph.set_pie_labels(labels) graph.add_data(data) graph.set_colours(colors) return graph
def pie_chart_cve(data, filename, title): chart = PieChart2D(int(settings.width * 1.7), settings.height) chart.set_legend(['Baixo', 'Médio', 'Alto', 'Crítico']) chart.set_title(title) chart.add_data(data) chart.set_colours(['00ff00', '0000ff', 'ffff00', 'ff0000']) #chart.set_pie_labels([ 'Baixo', 'Médio', 'Alto', 'Crítico']) chart.download(filename)
def chart_MF(gruppo, data): f = sum(1 for i in data['data'] if i[0] == 'F' and i[1] == gruppo) m = sum(1 for i in data['data'] if i[0] == 'M' and i[1] == gruppo) chart = PieChart2D(400, 200) chart.add_data([f, m]) chart.set_pie_labels(['F=' + str(f), 'M=' + str(m)]) chart.set_title(gruppo) chart.set_colours(['FF0000', '0000FF']) print "<img src='%s'>" % chart.get_url()
def graph_boolean(q): # this method is not very DRY with respect to # graph_multiple_choice # will probably combine these once we figure # out how they will be used #question = get_object_or_404(Question, pk=q.pk) question = Question.objects.get(pk=q.pk) # collect answers to this question answers = Answer.objects.filter(question=question) # only two choices unless we accept maybies choices = {0: 0, 1: 0} # grab the parsed entries for this question entries = Entry.objects.filter(question=question,\ is_unparseable=False) # i'm assuming here that the Entry.text is the # same thing as the Answer.choice, presumably # 0 for false/no and 1 for true/yes # # iterate entries and tally the choices for e in entries: if int(e.text) in choices: choices[int(e.text)] += 1 # collect the long, textual representation # of the answer choice for labelling the graph # along with the choice counts of each choice # for display on large graphs long_answers_big = [] long_answers_small = [] for a in answers: long_answers_small.append(a.text) long_answers_big.append(a.text + ' (' + str(choices[int(a.choice)]) + ')') for size in GRAPH_SIZES: # configure and save the graph pie = PieChart2D(int(size), golden(int(size))) # TODO normalize values pie.add_data(choices.values()) if (size == GRAPH_SIZES[0]): pie.set_legend(long_answers_small) else: pie.set_legend(long_answers_big) pie.set_colours(['0091C7', '0FBBD0']) filename = GRAPH_DIR + str(question.pk) + '-' + size + '-entries.png' pie.download(filename) print 'saved ' + filename return 'graphed entries ' + question.text
def mk_chart(self): from pygooglechart import PieChart2D chart = PieChart2D(self.width, self.height) data = self.data[:self.max_entries] if len(self.data) > len(data): remainder = sum(d[1] for d in self.data[self.max_entries:]) data.append(('Other', remainder)) chart.add_data([d[1] for d in data]) chart.set_pie_labels([d[0].split()[0] for d in data]) return chart
def create_diagram_requests(count_bad, count_good): chart = PieChart2D(650, 300) chart.set_title('HTTP Requests') chart.add_data([count_good, count_bad]) chart.set_pie_labels(["Bad Requests", "Good Requests"]) chart.set_legend([ f'Count XSS, SQL INJ Requests - {count_good}', f'Good Requests - {count_bad}' ]) chart.set_colours(['8e5ea2', '3e95cd']) url = chart.get_url() return url
def house_explosions(): """ Data from http://indexed.blogspot.com/2007/12/meltdown-indeed.html """ chart = PieChart2D(int(width * 1.7), height) chart.add_data([10, 10, 30, 200]) chart.set_pie_labels([ 'Budding Chemists', 'Propane issues', 'Meth Labs', 'Attempts to escape morgage', ]) chart.download('pie-house-explosions.png')
def piechart(d): #medduppercent = dups[len(dups)/2][0]*100.0/len(valuelist) keys = [] values = [] vcount = sum(e.get("v") for e in d) for i in range(min(5, len(d))): keys.append("%s (%s%%)" % (d[i]["k"],d[i]["v"]*100/vcount )) values.append(d[i]["v"]) if len(d) > 5: val = sum(e.get("v") for e in d[5:]) values.append(val) keys.append("All Other (%s%%)" % (val*100/vcount)) chart = PieChart2D(380, 180) chart.add_data(values) chart.set_legend(keys) return chart.get_url()
def pie_chart(data_dict, title=None): chart = PieChart2D(500, 225) chart_data = [] chart_labels = [] for label, value in data_dict.items(): if value > 0: chart_labels.append(str(label)) chart_data.append(value) if title: chart.title = title chart.add_data(chart_data) chart.set_pie_labels(chart_labels) chart.set_colours(colours[:len(data_dict)]) return chart
def buildPieChart2D(self, data, addValueToLabel=False, sortByKeys=False): chart = PieChart2D(self.defaultWidth, self.defaultHeight) labels = list() dataPoints = list() keys = sorted(data.keys()) if sortByKeys else data.keys() for key in keys: value = data[key] label = key if addValueToLabel: label += ' (' + str(value) + ')' labels.append(label) dataPoints.append(value) chart.add_data(dataPoints) chart.set_pie_labels(labels) return chart
def __get_pie_chart(self, results, width, height): # Create a chart object of 200x100 pixels pchart = PieChart2D(width, height) # Add some data votes = [] labels = [] for prli in results: votes.append(prli.votes) labels.append(prli.answer.answer) print votes print labels # Add some data pchart.add_data(votes) # Assign the labels to the pie data pchart.set_pie_labels(labels) return pchart
def _pie(self, results): width = self.request.GET.get('width') height = self.request.GET.get('height') if width: width = int(width) else: width = 300 if height: height = int(height) else: height = 220 chart = PieChart2D(width, height) data = defaultdict(int) if self.request.GET.get('entity_type') == 'party': to_include = [ 'R', 'D', ] for result in results: if result['party'] in to_include: data[result['party']] = result['count'] else: data['Other'] += result['count'] data = data.items() counts = [x[1] for x in data] labels = [x[0] for x in data] party_colors = {'R': 'bb3110', 'D': '295e72', 'Other': 'efefef'} colors = [party_colors.get(label) for label in labels] chart.add_data(counts) if self.request.GET.get('labels', 'true') != 'false': chart.set_pie_labels(labels) chart.set_colours(colors) chart.fill_solid('bg', '00000000') # Make the background transparent if self.request.GET.get('output') == 'data': return (labels, counts) return chart.get_url()
def month_breakdown(file): #open file f = open(file) #create two lists values = [] labels = [] #populate lists for line in f: l, v = line.split('\t') values.append(float(v)) labels.append(l) # Create a chart object of 200x100 pixels chart = PieChart2D(width, height) # Add some data chart.add_data(values) # Assign the labels to the pie data chart.set_pie_labels(labels) # Download the chart chart.download(file.replace('txt', 'png'))
return render('pollngo/question.html', payload, request) def results(request, slug): try: question = Question.objects.get(slug=slug) except ObjectDoesNotExist, e: raise Http404 total_votes = 0 choice_name = [] choice_val = [] for choice in question.choice_set.all(): total_votes += choice.total_votes choice_name.append(choice.text) choice_val.append(choice.total_votes) chart = PieChart2D(400, 200) chart.add_data(choice_val) choice_name = [choice_obj.encode('utf8') for choice_obj in choice_name] chart.set_pie_labels(choice_name) chart_url = chart.get_url() payload = { 'question': question, 'total_votes': total_votes, 'chart_url': chart_url } return render('pollngo/results.html', payload, request) def create(request): if request.method == 'POST': form = pforms.CreatePoll(request, request.POST)
def generateGraphs(): version() conndio = sqlite3.connect(dbfiledionaea) c = conndio.cursor() connglas = sqlite3.connect(dbfileglastopf) x = connglas.cursor() # # # Dionaea Queries starting here! # # #Most attacked ports - Dionaea querySQL = 'SELECT COUNT(local_port) AS hitcount,local_port AS port FROM connections WHERE connection_type = "accept" GROUP BY local_port HAVING COUNT(local_port) > 10' print querySQL c.execute(querySQL) #Count attacks on port 80 - Glastopf querySQL = "SELECT COUNT(request_url) AS hitcount,'80 - http' AS port FROM events" print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in c: print(row) flist.append(row[0]) seclist.append(str(row[1])) for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) seclist = [port.replace('21','21 - ftp') for port in seclist] seclist = [port.replace('42','42 - wins') for port in seclist] seclist = [port.replace('135','135 - msrpc') for port in seclist] seclist = [port.replace('445','445 - smb') for port in seclist] seclist = [port.replace('1433','1433 - ms-sql') for port in seclist] seclist = [port.replace('3306','3306 - mysql') for port in seclist] seclist = [port.replace('5060','5060 - sip') for port in seclist] chart.add_data(flist) chart.set_legend(seclist) chart.download('attacked_ports.png') #Top10 Malware querySQL = 'SELECT COUNT(download_md5_hash), download_md5_hash FROM downloads GROUP BY download_md5_hash ORDER BY COUNT(download_md5_hash) DESC LIMIT 10' print querySQL c.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in c: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_malware.png') #Connections per Day Dionaea and Glastopf - 7 Days - Dionaea querySQL = "SELECT strftime('%Y', connection_timestamp,'unixepoch') as 'year', strftime('%m', connection_timestamp,'unixepoch') as 'month', strftime('%d', connection_timestamp,'unixepoch') as 'day', count(strftime('%m', connection_timestamp,'unixepoch')) as 'num' FROM connections GROUP BY strftime('%Y', connection_timestamp,'unixepoch'), strftime('%m', connection_timestamp,'unixepoch'), strftime('%d', connection_timestamp,'unixepoch') ORDER BY strftime('%Y', connection_timestamp,'unixepoch') DESC, strftime('%m', connection_timestamp,'unixepoch') DESC, strftime('%d', connection_timestamp,'unixepoch') DESC LIMIT 7" print querySQL c.execute(querySQL) #Connections per Day Dionaea and Glastopf - 7 Days - Glastopf querySQL = "SELECT COUNT(time), SUBSTR(time,-20,12) AS stripped FROM events GROUP BY stripped ORDER BY stripped DESC LIMIT 7" print querySQL x.execute(querySQL) chart = SimpleLineChart(pieChartWidth, pieChartHeight, y_range=[0, 5000]) flist = [] seclist = [] for (row,rowx) in zip(c,x): print(row) print(rowx) l = list(row) l[3]=row[3]+rowx[0] flist.append(l[3]) date = '%s-%s-%s' % (str(row[0]),str(row[1]),str(row[2])) seclist.append(date) flist.reverse() seclist.reverse() chart.add_data(flist) chart.set_axis_labels(Axis.BOTTOM,seclist) chart.set_colours(['0000FF']) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 20, 5, 5) left_axis = range(0, 5001, 1000) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) chart.download('connections_per_day.png') # # # Glastopf Queries starting here! # # #Top15 intext requests querySQL = 'SELECT count, content FROM intext ORDER BY count DESC LIMIT 15' print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_intext.png') #Top15 intitle requests querySQL = 'SELECT count, content FROM intitle ORDER BY count DESC LIMIT 15' print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_intitle.png') #Top10 inurl requests querySQL = 'SELECT count, SUBSTR(content,0,40) FROM inurl ORDER BY count DESC LIMIT 10' print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_inurl.png')
print '<tr class="', "odd" if rank % 2 else "even", '">' print "<td>", rank, "</td>" print "<td>", d["amount"], "</td>" print "<td class='country'>", d["country"], "</td>" print "<td>", d["matchesPlayedSum"], "</td>" print "<td class='matcheswon'>", d["matchesWonSum"], "</td>" print "</tr>" rank += 1 chartData.append(d["amount"]) labels.append(str(d["country"])) print "</table>" print "<br /><br />" chart = PieChart2D(480, 300) chart.add_data(chartData) chart.set_pie_labels(labels) print "<img src='%s' />" % chart.get_url() print "</div><br /><br />" print "<div id='chart_div'></div>" import scraperwiki, string from pygooglechart import PieChart2D scraperwiki.sqlite.attach("cuetrackernet", "src") data = scraperwiki.sqlite.select( 'COUNT(id) AS amount, country, SUM(matchesPlayed) AS matchesPlayedSum, SUM(matchesWon) AS matchesWonSum FROM src.cuetracker GROUP BY country ORDER BY amount DESC' ) gb = scraperwiki.sqlite.select( 'COUNT(id) AS amount, "GB" as country, SUM(matchesPlayed) AS matchesPlayedSum, SUM(matchesWon) AS matchesWonSum FROM src.cuetracker WHERE country IN ("England", "Scotland", "Wales", "Northern Ireland") ORDER BY amount DESC'
def summary(request, formID): """for rendering the index page for any user who has just logged in""" if 'username' not in request.session or request.session['username'] == None: request.session['redirect'] = request.get_full_path() return redirect('%s/ldap_login' % ROOT) """summary of feedback for a form...""" #select a form f = feedbackForm.objects.get(pk=formID) print 'we got %s in summary wala view' % (f) # check if the user is actually allowed to view the form..!! u = user.objects.get(username=request.session['username']) feedbackAbout = u.allowed_viewing_feedback_about.values() allowed = False for a in feedbackAbout: if str(a['title']) == str(f.title): allowed = True if allowed is False: return HttpResponse("You are not allowed to see this submission ") #do we have any submissions for this form ? submissions = feedbackSubmission.objects.filter(feedbackForm=f).count() print "no of submissions are..!!!", submissions #if number of submissions is less than 0, if submissions <= 0: return redirect("%s/manage_feedback/5/error" % ROOT) #print whether deadline is gone or not for submitting... if f.deadline_for_filling < datetime.now(): deadlineGone = True else: deadlineGone = False summary_outer_dict = dict() #for each feedbackQuestion in the form from pygooglechart import PieChart2D for q in f.questions.all(): summary_inner_dict = dict() Options = feedbackQuestionOption.objects.filter(question=q) # to set the dictionalry initial value to 0. and the key to the question options for o in Options: summary_inner_dict[o] = 0 #find all AnswerOptions corresponding to it. sub = feedbackSubmission.objects.filter(feedbackForm=f) for s in sub: # the options to be counted is to be related to a partcular submission made for this partucal form. qwaleOptions = feedbackSubmissionAnswer.objects.filter( question=q).filter(submission=s) #for each AnswerOption: for opt in qwaleOptions: #count the number of total feedbackSubmissionAnswer... # adding the count to the inner dictionry if opt.answer_text is not None: summary_inner_dict[opt.answer_text] = None continue else: summary_inner_dict[opt.answer_option] = summary_inner_dict[ opt.answer_option] + 1 try: chart = PieChart2D(450, 200) def text(x): return x.text print summary_inner_dict.values() print summary_inner_dict.keys() chart.add_data(summary_inner_dict.values()) chart.set_pie_labels(list(map(text, summary_inner_dict.keys()))) try: chart.download('%s/piecharts/%s.png' % (MEDIA_ROOT, q.id)) except: pass except: pass summary_outer_dict[q] = summary_inner_dict t = loader.get_template('manage_feedback/summary.html') c = Context({ 'deadlinegone': deadlineGone, # idk why we had put this condition...!! ?? 'formName': f.title, 'summaryDict': summary_outer_dict, 'ROOT': ROOT, 'MEDIA_URL': MEDIA_URL }) return HttpResponse(t.render(c))