Example usage for org.jfree.data.statistics SimpleHistogramDataset addBin

List of usage examples for org.jfree.data.statistics SimpleHistogramDataset addBin

Introduction

In this page you can find the example usage for org.jfree.data.statistics SimpleHistogramDataset addBin.

Prototype

public void addBin(SimpleHistogramBin bin) 

Source Link

Document

Adds a bin to the dataset.

Usage

From source file:org.jfree.expdemo.SelectionDemo4.java

/**
 * Creates a sample {@link HistogramDataset}.
 * /* w  w  w .  j a v  a2s .c om*/
 * @return the dataset.
 */
private static IntervalXYDataset createDataset() {
    SimpleHistogramDataset dataset = new SimpleHistogramDataset("H1");
    double lower = 0.0;
    for (int i = 0; i < 100; i++) {
        double upper = (i + 1) / 10.0;
        SimpleHistogramBin bin = new SimpleHistogramBin(lower, upper, true, false);
        dataset.addBin(bin);
        lower = upper;
    }
    double[] values = new double[1000];
    Random generator = new Random(12345678L);
    for (int i = 0; i < 1000; i++) {
        values[i] = generator.nextGaussian() + 5;
    }
    dataset.addObservations(values);
    return dataset;
}

From source file:org.jfree.data.statistics.SimpleHistogramDatasetTest.java

/**
 * Some checks for the removeAllBins() method.
 *//*  w w  w.  j  a  va  2  s .com*/
@Test
public void testRemoveAllBins() {
    SimpleHistogramDataset d1 = new SimpleHistogramDataset("D1");
    d1.addBin(new SimpleHistogramBin(0.0, 1.0));
    d1.addObservation(0.5);
    d1.addBin(new SimpleHistogramBin(2.0, 3.0));
    assertEquals(2, d1.getItemCount(0));
    d1.removeAllBins();
    assertEquals(0, d1.getItemCount(0));
}

From source file:org.jfree.data.statistics.SimpleHistogramDatasetTest.java

/**
 * Some checks for the clearObservations() method.
 *//*from  w  w w  .j  a va 2 s.co m*/
@Test
public void testClearObservations() {
    SimpleHistogramDataset d1 = new SimpleHistogramDataset("D1");
    d1.clearObservations();
    assertEquals(0, d1.getItemCount(0));
    d1.addBin(new SimpleHistogramBin(0.0, 1.0));
    d1.addObservation(0.5);
    assertEquals(1.0, d1.getYValue(0, 0), EPSILON);
}

From source file:org.jfree.data.statistics.SimpleHistogramDatasetTest.java

/**
 * Ensure that the equals() method can distinguish all fields.
 *///from  www  . j av  a  2  s.c om
@Test
public void testEquals() {
    SimpleHistogramDataset d1 = new SimpleHistogramDataset("Dataset 1");
    SimpleHistogramDataset d2 = new SimpleHistogramDataset("Dataset 1");
    assertTrue(d1.equals(d2));

    d1.addBin(new SimpleHistogramBin(1.0, 2.0));
    assertFalse(d1.equals(d2));
    d2.addBin(new SimpleHistogramBin(1.0, 2.0));
    assertTrue(d1.equals(d2));
}

From source file:org.jfree.data.statistics.SimpleHistogramDatasetTest.java

/**
 * Some checks for the clone() method.//from www .  j ava  2 s  . c  o  m
 */
@Test
public void testCloning() throws CloneNotSupportedException {
    SimpleHistogramDataset d1 = new SimpleHistogramDataset("Dataset 1");
    SimpleHistogramDataset d2 = (SimpleHistogramDataset) d1.clone();
    assertTrue(d1 != d2);
    assertTrue(d1.getClass() == d2.getClass());
    assertTrue(d1.equals(d2));

    // check that clone is independent of the original
    d2.addBin(new SimpleHistogramBin(2.0, 3.0));
    d2.addObservation(2.3);
    assertFalse(d1.equals(d2));
}

From source file:ch.zhaw.ias.dito.ui.AnalysisPanel.java

private JFreeChart createHistogramChart(String title, Matrix distanceMatrix) {
    int NUM_OF_BINS = 50;
    SimpleHistogramDataset dataset = new SimpleHistogramDataset("");
    dataset.setAdjustForBinSize(false);//w ww  .j  av a  2s .c  om
    double min = distanceMatrix.extremum(false);
    double max = distanceMatrix.extremum(true);
    double spacing = (max - min) / NUM_OF_BINS;
    double currentBound = min;

    for (int i = 0; i < NUM_OF_BINS - 1; i++) {
        dataset.addBin(new SimpleHistogramBin(currentBound, (currentBound + spacing), true, false));
        currentBound += spacing;
    }
    //ensure that the maximum is included and not lost because of numerical problems
    dataset.addBin(new SimpleHistogramBin(currentBound, max, true, true));
    for (int i = 0; i < distanceMatrix.getColCount(); i++) {
        DVector v = distanceMatrix.col(i);
        dataset.addObservations(v.getValues());
    }
    return ChartFactory.createHistogram(title, Translation.INSTANCE.get("misc.graphic.distance"),
            Translation.INSTANCE.get("misc.graphic.frequency"), dataset, PlotOrientation.VERTICAL, false, true,
            false);
}

From source file:imageviewer.tools.HistogramTool.java

public void plot(MouseEvent e) {

    if (e.getSource() instanceof ImagePanel) {
        Image image = ((ImagePanel) e.getSource()).getSource();
        Histogram h = image.getHistogram();
        SimpleHistogramDataset shd = new SimpleHistogramDataset("Voxel distribution");
        for (int i = 0, numBands = h.getNumBands(); i < numBands; i++) {
            int[] binData = h.getBins(i);
            for (int j = 0; j < binData.length; j++) {
                SimpleHistogramBin shb = new SimpleHistogramBin(j - 0.5, j + 0.5, true, false);
                shb.setItemCount(binData[j]);
                shd.addBin(shb);
            }/*from w ww  . java  2s .  c o m*/
        }
        double[] domainBounds = null, rangeBounds = null;
        if (chart != null) {
            ValueAxis va = chart.getXYPlot().getDomainAxis();
            domainBounds = new double[] { va.getLowerBound(), va.getUpperBound() };
            va = chart.getXYPlot().getRangeAxis();
            rangeBounds = new double[] { va.getLowerBound(), va.getUpperBound() };
        }
        chart = ChartFactory.createHistogram(null, "Voxel value", "Count", shd, PlotOrientation.VERTICAL, false,
                true, false);
        chart.setBackgroundPaint(new Color(20, 30, 45));
        chart.setAntiAlias(true);
        ValueAxis domainAxis = chart.getXYPlot().getDomainAxis();
        domainAxis.setLabelFont(axisFont);
        domainAxis.setTickLabelFont(tickFont);
        domainAxis.setAxisLinePaint(Color.white);
        domainAxis.setTickLabelPaint(Color.white);
        domainAxis.setLabelPaint(Color.white);
        if (domainBounds != null) {
            domainAxis.setAutoRange(false);
            domainAxis.setLowerBound(domainBounds[0]);
            domainAxis.setUpperBound(domainBounds[1]);
        } else {
            domainAxis.setLowerBound(0);
        }
        ValueAxis rangeAxis = chart.getXYPlot().getRangeAxis();
        rangeAxis.setLabelFont(axisFont);
        rangeAxis.setTickLabelFont(tickFont);
        rangeAxis.setAxisLinePaint(Color.white);
        rangeAxis.setTickLabelPaint(Color.white);
        rangeAxis.setLabelPaint(Color.white);
        if (rangeBounds != null) {
            rangeAxis.setAutoRange(false);
            rangeAxis.setLowerBound(rangeBounds[0]);
            rangeAxis.setUpperBound(rangeBounds[1]);
        }
        chart.getXYPlot().getRenderer().setSeriesPaint(0, new Color(0, 51, 113));
        ((XYBarRenderer) chart.getXYPlot().getRenderer()).setDrawBarOutline(false);
        chart.getXYPlot().setBackgroundAlpha(0.05f);

        double[] mean = h.getMean();
        double[] sd = h.getStandardDeviation();
        IntervalMarker im = new IntervalMarker(mean[0] - sd[0] / 2, mean[0] + sd[0] / 2);
        im.setPaint(new Color(200, 200, 200, 128));
        chart.getXYPlot().addDomainMarker(0, im, Layer.BACKGROUND);
        cp.setChart(chart);
    }
}

From source file:info.financialecology.finance.utilities.datastruct.VersatileChart.java

public JFreeChart drawSimpleHistogram(String name, ArrayList<VersatileTimeSeries> atsArray) {
    JFreeChart chart;//from   w  w w.ja v  a 2  s. c  o  m
    int count = 0;
    double min = 0, max = 0, tmpMin, tmpMax;

    for (VersatileTimeSeries ats : atsArray) {
        if (count == 0) {
            min = ats.getMinY();
            max = ats.getMaxY();
        } else {
            tmpMin = ats.getMinY();
            tmpMax = ats.getMaxY();
            min = tmpMin < min ? tmpMin : min;
            max = tmpMax > max ? tmpMax : max;
        }
    }

    max = max > 0 ? max * 1.05 : max / 1.05;
    min = min > 0 ? min / 1.05 : min * 1.05;

    SimpleHistogramDataset dataSet = new SimpleHistogramDataset(name);

    for (int i = 0; i < params.numBins; i++) {
        double start = min + i * ((max - min) / params.numBins);
        double end = start + ((max - min) / params.numBins) * 0.999999999999;

        SimpleHistogramBin histBin = new SimpleHistogramBin(start, end, true, true);
        dataSet.addBin(histBin);
    }

    for (VersatileTimeSeries ats : atsArray) {
        for (int i = 0; i < ats.getItemCount(); i++) {
            double value = ats.getValue(i).doubleValue();

            try {
                dataSet.addObservation(ats.getValue(i).doubleValue());
            } catch (Throwable t) {
                // sometimes this throws an exception when the value falls within the narrow gaps of the bins
            }
        }
    }
    chart = ChartFactory.createHistogram(params.title, params.xLabel, params.yLabel, dataSet,
            PlotOrientation.VERTICAL, params.legend, params.toolTips, false);

    return chart;
}

From source file:jchrest.gui.Shell.java

private JPanel getHistogramPane(Map<Integer, Integer> contentSizes, String label, String title, String xAxis) {
    int largest = 0;
    for (Integer key : contentSizes.keySet()) {
        if (key > largest)
            largest = key;//from w  ww  .ja  va 2s  .c om
    }
    SimpleHistogramDataset dataset = new SimpleHistogramDataset(label);
    for (int i = 0; i <= largest; ++i) {
        SimpleHistogramBin bin = new SimpleHistogramBin((double) i, (double) (i + 1), true, false);
        int count = 0;
        if (contentSizes.containsKey(i)) {
            count = contentSizes.get(i);
        }
        bin.setItemCount(count);
        dataset.addBin(bin);
    }
    PlotOrientation orientation = PlotOrientation.VERTICAL;
    boolean show = false;
    boolean toolTips = true;
    boolean urls = false;
    JFreeChart chart = ChartFactory.createHistogram(title, xAxis, "frequency", dataset, orientation, show,
            toolTips, urls);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(new ChartPanel(chart, 400, 300, 200, 100, 600, 600, true, true, true, true, true, true));
    JButton saveButton = new JButton("Save Data");
    saveButton.setToolTipText("Save the histogram data to a CSV file");
    saveButton.addActionListener(new SaveHistogramActionListener(this, contentSizes));
    panel.add(saveButton, BorderLayout.SOUTH);
    return panel;
}