Esempio n. 1
0
  private static JFreeChart createDistributionViewChart() {
    XYPlot plot = new XYPlot();

    XYLineAndShapeRenderer dRenderer = new XYSplineRenderer();
    dRenderer.setBaseShapesVisible(false);
    dRenderer.setAutoPopulateSeriesPaint(false);
    dRenderer.setAutoPopulateSeriesStroke(false);
    dRenderer.setBaseStroke(TsCharts.getStrongStroke(LinesThickness.Thin));
    dRenderer.setDrawSeriesLineAsPath(true); // not sure if useful
    plot.setDataset(DISTRIBUTION_INDEX, Charts.emptyXYDataset());
    plot.setRenderer(DISTRIBUTION_INDEX, dRenderer);

    XYBarRenderer hRenderer = new XYBarRenderer();
    hRenderer.setShadowVisible(false);
    hRenderer.setDrawBarOutline(true);
    hRenderer.setAutoPopulateSeriesPaint(false);
    hRenderer.setAutoPopulateSeriesOutlinePaint(false);
    hRenderer.setBaseSeriesVisibleInLegend(false);
    plot.setDataset(HISTOGRAM_INDEX, Charts.emptyXYDataset());
    plot.setRenderer(HISTOGRAM_INDEX, hRenderer);

    NumberAxis domainAxis = new NumberAxis();
    domainAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR);
    plot.setDomainAxis(domainAxis);
    plot.setDomainGridlinesVisible(false);

    NumberAxis rangeAxis = new NumberAxis();
    rangeAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR);
    rangeAxis.setTickUnit(new NumberTickUnit(0.05));
    rangeAxis.setNumberFormatOverride(new DecimalFormat("0.###"));
    plot.setRangeAxis(rangeAxis);

    plot.mapDatasetToDomainAxis(0, 0);
    plot.mapDatasetToRangeAxis(0, 0);
    plot.mapDatasetToDomainAxis(1, 0);
    plot.mapDatasetToRangeAxis(1, 0);

    JFreeChart result = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    result.setPadding(TsCharts.CHART_PADDING);
    result.getTitle().setFont(TsCharts.CHART_TITLE_FONT);
    result.getLegend().setFrame(BlockBorder.NONE);
    result.getLegend().setBackgroundPaint(null);
    return result;
  }
Esempio n. 2
0
  public DistributionView() {
    this.lBound = DEFAULT_L_BOUND;
    this.rBound = DEFAULT_R_BOUND;
    this.distribution = DEFAULT_DISTRIBUTION;
    this.adjustDistribution = DEFAULT_ADJUST_DISTRIBUTION;
    this.hCount = DEFAULT_H_COUNT;
    this.data = DEFAULT_DATA;

    this.chartPanel = new ChartPanel(createDistributionViewChart());
    Charts.avoidScaling(chartPanel);

    onDataFormatChange();
    onColorSchemeChange();
    onComponentPopupMenuChange();
    enableProperties();

    setLayout(new BorderLayout());
    add(chartPanel, BorderLayout.CENTER);
  }
Esempio n. 3
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();
  }