docs: private void writeGnuplotScript(final String basepath,
                                    final String[] datafiles) throws IOException {
      final String script_path = basepath + ".gnuplot";
      final PrintWriter gp = new PrintWriter(script_path);
      try {
        // XXX don't hardcode all those settings.  At least not like that.
        gp.append("set term png small size ")
          // Why the fuck didn't they also add methods for numbers?
          .append(Short.toString(width)).append(",")
          .append(Short.toString(height));
        final String smooth = params.remove("smooth");
        final String fgcolor = params.remove("fgcolor");
        final String style = params.remove("style");
        String bgcolor = params.remove("bgcolor");
        if (fgcolor != null && bgcolor == null) {
          // We can't specify a fgcolor without specifying a bgcolor.
          bgcolor = "xFFFFFF";  // So use a default.
        }
        if (bgcolor != null) {
          if (fgcolor != null && "transparent".equals(bgcolor)) {
            // In case we need to specify a fgcolor but we wanted a transparent
            // background, we also need to pass a bgcolor otherwise the first
            // hex color will be mistakenly taken as a bgcolor by Gnuplot.
            bgcolor = "transparent xFFFFFF";
          }
          gp.append(' ').append(bgcolor);
        }
        if (fgcolor != null) {
          gp.append(' ').append(fgcolor);
        }
  
        gp.append("\n"
                  + "set xdata time\n"
                  + "set timefmt \"%s\"\n"
                  + "if (GPVAL_VERSION < 4.6) set xtics rotate; else set xtics rotate right\n"
                  + "set output \"").append(basepath + ".png").append("\"\n"
                  + "set xrange [\"")
          .append(String.valueOf((start_time & UNSIGNED) + utc_offset))
          .append("\":\"")
          .append(String.valueOf((end_time & UNSIGNED) + utc_offset))
          .append("\"]\n");
        if (!params.containsKey("format x")) {
          gp.append("set format x \"").append(xFormat()).append("\"\n");
        }
        final int nseries = datapoints.size();
        if (nseries > 0) {
          gp.write("set grid\n"
                   + "set style data ");
          gp.append(style != null? style : "linespoint").append("\n");
          if (!params.containsKey("key")) {
            gp.write("set key right box\n");
          }
        } else {
          gp.write("unset key\n");
          if (params == null || !params.containsKey("label")) {
            gp.write("set label \"No data\" at graph 0.5,0.9 center\n");
          }
        }
  
        if (params != null) {
          for (final Map.Entry<String, String> entry : params.entrySet()) {
            final String key = entry.getKey();
            final String value = entry.getValue();
            if (value != null) {
              gp.append("set ").append(key)
                .append(' ').append(value).write('\n');
            } else {
              gp.append("unset ").append(key).write('\n');
            }
          }
        }
        for (final String opts : options) {
          if (opts.contains("x1y2")) {
            // Create a second scale for the y-axis on the right-hand side.
            gp.write("set y2tics border\n");
            break;
          }
        }
        
        // compile annotations to determine if we have any to graph
        final List<Annotation> notes = new ArrayList<Annotation>();
        for (int i = 0; i < nseries; i++) {
          final DataPoints dp = datapoints.get(i);
          final List<Annotation> series_notes = dp.getAnnotations();
          if (series_notes != null && !series_notes.isEmpty()) {
            notes.addAll(series_notes);
          }
        }
        if (globals != null) {
          notes.addAll(globals);
        }
        if (notes.size() > 0) {
          Collections.sort(notes);
          for(Annotation note : notes) {
            String ts = Long.toString(note.getStartTime());
            String value = new String(note.getDescription());
            gp.append("set arrow from \"").append(ts).append("\", graph 0 to \"");
            gp.append(ts).append("\", graph 1 nohead ls 3\n");
            gp.append("set object rectangle at \"").append(ts);
            gp.append("\", graph 0 size char (strlen(\"").append(value);
            gp.append("\")), char 1 front fc rgbcolor \"white\"\n");
            gp.append("set label \"").append(value).append("\" at \"");
            gp.append(ts).append("\", graph 0 front center\n");
          } 
        }
  
        gp.write("plot ");
        for (int i = 0; i < nseries; i++) {
          final DataPoints dp = datapoints.get(i);
          final String title = dp.metricName() + dp.getTags();
          gp.append(" \"").append(datafiles[i]).append("\" using 1:2");
          if (smooth != null) {
            gp.append(" smooth ").append(smooth);
          }
          // TODO(tsuna): Escape double quotes in title.
          gp.append(" title \"").append(title).write('"');
          final String opts = options.get(i);
          if (!opts.isEmpty()) {
            gp.append(' ').write(opts);
          }
          if (i != nseries - 1) {
            gp.print(", \\");
          }
          gp.write('\n');
        }
        if (nseries == 0) {
          gp.write('0');
        }
      } finally {
        gp.close();
        LOG.info("Wrote Gnuplot script to " + script_path);
      }
    }