Raph Levien | dcecdd8 | 2012-03-23 11:21:16 -0700 | [diff] [blame^] | 1 | // Copyright 2011 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package com.google.typography.font.compression; |
| 6 | |
| 7 | import com.google.common.collect.Lists; |
| 8 | |
| 9 | import java.io.PrintWriter; |
| 10 | import java.util.Collections; |
| 11 | import java.util.List; |
| 12 | |
| 13 | /** |
| 14 | * Class for gathering up stats, for summarizing and graphing. |
| 15 | * |
| 16 | * @author raph@google.com (Raph Levien) |
| 17 | */ |
| 18 | public class StatsCollector { |
| 19 | private final List<Double> values; |
| 20 | |
| 21 | public StatsCollector() { |
| 22 | values = Lists.newArrayList(); |
| 23 | } |
| 24 | |
| 25 | public void addStat(double value) { |
| 26 | values.add(value); |
| 27 | } |
| 28 | |
| 29 | public double mean() { |
| 30 | double sum = 0; |
| 31 | for (Double value : values) { |
| 32 | sum += value; |
| 33 | } |
| 34 | return sum / values.size(); |
| 35 | } |
| 36 | |
| 37 | public double median() { |
| 38 | Collections.sort(values); |
| 39 | int length = values.size(); |
| 40 | if (length % 2 == 1) { |
| 41 | return values.get((length - 1) / 2); |
| 42 | } else { |
| 43 | return 0.5 * (values.get(length / 2 - 1) + values.get(length / 2)); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Need to print <html> before calling this method |
| 48 | public void chartHeader(PrintWriter o, int n) { |
| 49 | o.println("<head>"); |
| 50 | o.println("<script type='text/javascript' src='https://www.google.com/jsapi'></script>"); |
| 51 | o.println("<script type='text/javascript'>"); |
| 52 | o.println("google.load('visualization', '1', {packages:['corechart']});"); |
| 53 | o.println("google.setOnLoadCallback(drawChart);"); |
| 54 | o.println("function drawChart() {"); |
| 55 | o.println(" var data = new google.visualization.DataTable()"); |
| 56 | o.println(" data.addColumn('string', 'Font');"); |
| 57 | if (n == 1) { |
| 58 | o.println(" data.addColumn('number', 'Ratio');"); |
| 59 | } else { |
| 60 | for (int i = 0; i < n; i++) { |
| 61 | o.printf(" data.addColumn('number', 'Ratio %c');\n", 'A' + i); |
| 62 | } |
| 63 | } |
| 64 | o.printf(" data.addRows(%d);\n", values.size()); |
| 65 | } |
| 66 | |
| 67 | public void chartData(PrintWriter o, int ix) { |
| 68 | Collections.sort(values); |
| 69 | int length = values.size(); |
| 70 | for (int i = 0; i < length; i++) { |
| 71 | o.printf(" data.setValue(%d, %d, %f);\n", i, ix, values.get(i)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public void chartEnd(PrintWriter o) { |
| 76 | o.println(" var chart = new google.visualization.LineChart(document.getElementById(" |
| 77 | + "'chart_div'));"); |
| 78 | o.println(" chart.draw(data, {width:700, height:400, title: 'Compression ratio'});"); |
| 79 | o.println("}"); |
| 80 | o.println("</script>"); |
| 81 | o.println("</head>"); |
| 82 | |
| 83 | o.println(); |
| 84 | o.println("<body>"); |
| 85 | o.println("<div id='chart_div'></div>"); |
| 86 | // TODO: split so we can get content into the HTML |
| 87 | } |
| 88 | public void chartFooter(PrintWriter o) { |
| 89 | o.println("</body>"); |
| 90 | o.println("</html>"); |
| 91 | } |
| 92 | } |