Exemple #1
0
  /**
   * Format just the value of a {@link VType} into a buffer (not timestamp, ..)
   *
   * @param value Value
   * @param buf {@link StringBuilder}
   * @return {@link StringBuilder}
   */
  public StringBuilder format(final VType value, final StringBuilder buf) {
    // After the time this is implemented, VEnum may change into a class
    // that also implements VNumber and/or VString.
    // Handle it first to assert that VEnum is always identified as such
    // and not handled as Number.
    if (value instanceof VEnum) {
      final VEnum enumerated = (VEnum) value;
      try {
        buf.append(enumerated.getValue());
        buf.append(" (").append(enumerated.getIndex()).append(")");
      } catch (ArrayIndexOutOfBoundsException ex) { // Error getting label for invalid index?
        buf.append("<enum ").append(enumerated.getIndex()).append(">");
      }
    } else if (value instanceof VNumber) {
      final VNumber number = (VNumber) value;
      final Display display = (Display) number;
      format(number.getValue().doubleValue(), display, buf);
    } else if (value instanceof VNumberArray) {
      final VNumberArray array = (VNumberArray) value;
      final Display display = (Display) array;
      final ListNumber list = array.getData();
      final int N = list.size();
      if (N <= MAX_ARRAY_ELEMENTS) {
        if (N > 0) format(list.getDouble(0), display, buf);
        for (int i = 1; i < N; ++i) {
          buf.append(", ");
          format(list.getDouble(i), display, buf);
        }
      } else {
        format(list.getDouble(0), display, buf);
        for (int i = 1; i < MAX_ARRAY_ELEMENTS / 2; ++i) {
          buf.append(", ");
          format(list.getDouble(i), display, buf);
        }
        buf.append(", ... (total ").append(N).append(" elements) ...");
        for (int i = N - MAX_ARRAY_ELEMENTS / 2; i < N; ++i) {
          buf.append(", ");
          format(list.getDouble(i), display, buf);
        }
      }
    } else if (value instanceof VStatistics) {
      final VStatistics stats = (VStatistics) value;
      final Display display = (Display) stats;
      format(stats.getAverage(), display, buf);
      buf.append(" [").append(stats.getMin()).append(" ... ").append(stats.getMax());
      buf.append(", ").append(stats.getNSamples()).append(" samples");
      final Double dev = stats.getStdDev();
      if (dev > 0) buf.append(", dev ").append(dev);
      buf.append("]");
    } else if (value instanceof VString) buf.append(((VString) value).getValue());
    else if (value == null) buf.append("null");
    else // TODO: VEnumArray, other types?
    buf.append(value.toString());

    return buf;
  }
  /**
   * Formats a numeric array. This method can be overridden to change the way numeric arrays are
   * formatted.
   *
   * @param array the array to format
   * @param toAppendTo the buffer to append to
   * @param pos the position of the field
   * @return the string buffer
   */
  protected StringBuffer format(VNumberArray array, StringBuffer toAppendTo, FieldPosition pos) {
    NumberFormat f = nf(array);

    toAppendTo.append("[");
    boolean hasMore = false;

    ListNumber data = array.getData();
    if (data.size() > maxElements) {
      hasMore = true;
    }

    for (int i = 0; i < Math.min(data.size(), maxElements); i++) {
      if (i != 0) {
        toAppendTo.append(", ");
      }
      if (data instanceof ListByte
          || data instanceof ListShort
          || data instanceof ListInt
          || data instanceof ListLong) {
        toAppendTo.append(f.format(data.getLong(i)));
      } else {
        toAppendTo.append(f.format(data.getDouble(i)));
      }
    }

    if (hasMore) {
      toAppendTo.append(", ...");
    }
    toAppendTo.append("]");
    return toAppendTo;
  }
  public void draw(Graphics2D g, Point2DDataset data) {
    int dataCount = data.getCount();

    // Retain the integrated min/max
    integratedMinX =
        java.lang.Double.isNaN(data.getXMinValue())
            ? integratedMinX
            : Math.min(integratedMinX, data.getXMinValue());
    integratedMinY =
        java.lang.Double.isNaN(data.getYMinValue())
            ? integratedMinY
            : Math.min(integratedMinY, data.getYMinValue());
    integratedMaxX =
        java.lang.Double.isNaN(data.getXMaxValue())
            ? integratedMaxX
            : Math.max(integratedMaxX, data.getXMaxValue());
    integratedMaxY =
        java.lang.Double.isNaN(data.getYMaxValue())
            ? integratedMaxY
            : Math.max(integratedMaxY, data.getYMaxValue());

    // Determine range of the plot.
    // If no range is set, use the one from the dataset
    double startXPlot;
    double startYPlot;
    double endXPlot;
    double endYPlot;
    if (rangeFromDataset) {
      startXPlot = integratedMinX;
      startYPlot = integratedMinY;
      endXPlot = integratedMaxX;
      endYPlot = integratedMaxY;
    } else {
      startXPlot = startPlotX;
      startYPlot = startPlotY;
      endXPlot = endPlotX;
      endYPlot = endPlotY;
    }
    int margin = 3;

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    g.setColor(Color.BLACK);

    // Compute axis
    ValueAxis xAxis = ValueAxis.createAutoAxis(startXPlot, endXPlot, Math.max(2, width / 60));
    ValueAxis yAxis = ValueAxis.createAutoAxis(startYPlot, endYPlot, Math.max(2, height / 60));
    HorizontalAxisRenderer xAxisRenderer = new HorizontalAxisRenderer(xAxis, margin, g);
    VerticalAxisRenderer yAxisRenderer = new VerticalAxisRenderer(yAxis, margin, g);

    // Compute graph area
    int xStartGraph = yAxisRenderer.getAxisWidth();
    int xEndGraph = width - margin;
    int yStartGraph = margin;
    int yEndGraph = height - xAxisRenderer.getAxisHeight();
    int plotWidth = xEndGraph - xStartGraph;
    int plotHeight = yEndGraph - yStartGraph;

    // Draw axis
    xAxisRenderer.draw(g, 0, xStartGraph, xEndGraph, width, yEndGraph);
    yAxisRenderer.draw(g, 0, yStartGraph, yEndGraph, height, xStartGraph);

    double rangeX = endXPlot - startXPlot;
    double rangeY = endYPlot - startYPlot;

    // Scale data
    int[] scaledX = new int[dataCount];
    int[] scaledY = new int[dataCount];
    ListNumber xValues = data.getXValues();
    ListNumber yValues = data.getYValues();
    for (int i = 0; i < scaledY.length; i++) {
      scaledX[i] =
          (int)
              (xStartGraph
                  + NumberUtil.scale(xValues.getDouble(i), startXPlot, endXPlot, plotWidth));
      scaledY[i] =
          (int)
              (height
                  - xAxisRenderer.getAxisHeight()
                  - NumberUtil.scale(yValues.getDouble(i), startYPlot, endYPlot, plotHeight));
    }

    // Draw reference lines
    g.setColor(new Color(240, 240, 240));
    int[] xTicks = xAxisRenderer.horizontalTickPositions();
    for (int xTick : xTicks) {
      g.drawLine(xTick, yStartGraph, xTick, yEndGraph);
    }
    int[] yTicks = yAxisRenderer.verticalTickPositions();
    for (int yTick : yTicks) {
      g.drawLine(xStartGraph, height - yTick, xEndGraph, height - yTick);
    }

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    g.setColor(Color.BLACK);
    // Make sure that the line does not go ouside the chart
    g.setClip(xStartGraph - 1, yStartGraph - 1, plotWidth + 2, plotHeight + 2);
    for (int i = 0; i < scaledY.length; i++) {
      g.draw(createShape(scaledX[i], scaledY[i]));
    }
  }