示例#1
0
  /** Get the FrequencyDistribution object */
  protected FrequencyDistribution getFrequencyDistribution(Variate data, int minimumBinCount) {
    double min = (Double) data.getMin();
    double max = (Double) data.getMax();

    if (min == max) {
      if (min == 0) {
        min = -1.0;
      } else {
        min -= Math.abs(min / 10.0);
      }
      if (max == 0) {
        max = 1.0;
      } else {
        max += Math.abs(max / 10.0);
      }
    }

    Axis axis = new LinearAxis(Axis.AT_MAJOR_TICK, Axis.AT_MAJOR_TICK);
    axis.setRange(min, max);

    int majorTickCount = axis.getMajorTickCount();
    axis.setPrefNumTicks(majorTickCount, 4);

    double binSize = axis.getMinorTickSpacing();
    int binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2;

    if (minimumBinCount > 0) {
      while (binCount < minimumBinCount) {
        majorTickCount++;
        axis.setPrefNumTicks(majorTickCount, 4);

        binSize = axis.getMinorTickSpacing();
        binCount =
            (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize)
                + 2; // should +2, otherwise the last bar will lose
      }
    }

    FrequencyDistribution frequency =
        new FrequencyDistribution(axis.getMinAxis(), binCount, binSize);

    for (int i = 0; i < raw.getCount(); i++) {
      frequency.addValue((Double) raw.get(i));
    }

    return frequency;
  }
  /** Set data */
  public void setData(Variate data, int minimumBinCount) {
    raw = data;
    FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount);

    Variate.D xData = new Variate.D();
    Variate.D yData = new Variate.D();

    double x = frequency.getLowerBound();

    for (int i = 0; i < frequency.getBinCount(); i++) {

      xData.add(x);
      yData.add(0.0);

      x += frequency.getBinSize();

      xData.add(x);
      yData.add(frequency.getProb(i));
    }
    setData(xData, yData);
  }