예제 #1
0
  protected void onDataChange() {
    XYPlot plot = chartPanel.getChart().getXYPlot();

    if (data != DEFAULT_DATA) {
      DescriptiveStatistics stats = new DescriptiveStatistics(new DataBlock(data));

      double m = 0, M = 0, dv = 1;
      if (adjustDistribution && distribution != DEFAULT_DISTRIBUTION) {
        m = stats.getAverage();
        M = distribution.getExpectation();
        double v = stats.getVar();
        double V = distribution.getVariance();
        dv = Math.sqrt(v / V);
      }

      final double xmin = stats.getMin() < lBound ? stats.getMin() : lBound;
      final double xmax = stats.getMax() > rBound ? stats.getMax() : rBound;
      final int n = hCount != 0 ? hCount : (int) Math.ceil(Math.sqrt(stats.getObservationsCount()));
      final double xstep = (xmax - xmin) / n;

      // distribution >
      if (distribution != DEFAULT_DISTRIBUTION) {
        Function2D density =
            new Function2D() {
              @Override
              public double getValue(double x) {
                return distribution.getDensity(x);
              }
            };

        final double zmin =
            distribution.hasLeftBound() != BoundaryType.None
                ? distribution.getLeftBound()
                : ((xmin - xstep - m) / dv + M);
        final double zmax =
            distribution.hasRightBound() != BoundaryType.None
                ? distribution.getRightBound()
                : ((xmax + xstep - m) / dv + M);

        // TODO: create IDistribution#getName() method
        String name = distribution.getClass().getSimpleName();

        ((XYLineAndShapeRenderer) plot.getRenderer(DISTRIBUTION_INDEX))
            .setLegendItemToolTipGenerator(getTooltipGenerator(distribution));
        plot.setDataset(
            DISTRIBUTION_INDEX, DatasetUtilities.sampleFunction2D(density, zmin, zmax, n, name));
      } else {
        plot.setDataset(DISTRIBUTION_INDEX, Charts.emptyXYDataset());
      }
      // < distribution

      // histogram >
      XYSeries hSeries = new XYSeries("");
      double nobs = stats.getObservationsCount();
      for (int i = 0; i <= n; ++i) {
        double x0 = xmin + i * xstep;
        double x1 = x0 + xstep;
        double y = stats.countBetween(x0, x1) / (nobs * xstep / dv);
        hSeries.add(((x0 + x1) / 2 - m) / dv + M, y);
      }

      plot.setDataset(
          HISTOGRAM_INDEX, new XYBarDataset(new XYSeriesCollection(hSeries), xstep / dv + M));
      // < histogram
    } else {
      plot.setDataset(HISTOGRAM_INDEX, Charts.emptyXYDataset());
      plot.setDataset(DISTRIBUTION_INDEX, Charts.emptyXYDataset());
    }

    onColorSchemeChange();
  }