Skip to content
Snippets Groups Projects
Commit 12cfdead authored by Tim Molter's avatar Tim Molter
Browse files

Tweaked Histogram code and made unit test

parent 507e65ee
No related branches found
No related tags found
No related merge requests found
......@@ -91,6 +91,13 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- FEST for fluent test assertions -->
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<version>2.0M10</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
......
......@@ -22,7 +22,7 @@ import java.util.List;
/**
* This class can be used to create histogram data for histogram bar charts
*
*
* @author timmolter
*/
public class Histogram {
......@@ -36,7 +36,7 @@ public class Histogram {
/**
* Constructor
*
*
* @param data
* @param numBins
*/
......@@ -64,7 +64,7 @@ public class Histogram {
/**
* Constructor
*
*
* @param data
* @param numBins
* @param min
......@@ -89,16 +89,15 @@ public class Histogram {
Iterator<? extends Number> itr = originalData.iterator();
while (itr.hasNext()) {
int bin = (int) ((((Number) itr.next()).doubleValue() - min) / binSize); // changed this from numBins
double doubleValue = ((Number) itr.next()).doubleValue();
int bin = (int) ((doubleValue - min) / binSize); // changed this from numBins
if (bin < 0) { /* this data is smaller than min */
// System.out.println("less than");
}
else if (bin > numBins) { /* this data point is bigger than max */
} else if (doubleValue == max) { // the value falls exactly on the max value
tempYAxisData[bin - 1] += 1;
} else if (bin > numBins || bin == numBins) { /* this data point is bigger than max */
// System.out.println("greater than");
}
else if (bin == numBins) { // this falls on the next bin of the max bin
}
else {
} else {
tempYAxisData[bin] += 1;
}
}
......
/**
* Copyright 2013 Xeiam LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart;
import static org.fest.assertions.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
/**
* @author timmolter
*/
public class HistogramTest {
@Test
public void test1() {
Histogram histogram = new Histogram(Arrays.asList(1, 2, 3, 4, 5, 6), 2, 0, 4);
assertThat(histogram.getMax()).isEqualTo(4.0);
assertThat(histogram.getMin()).isEqualTo(0.0);
assertThat(histogram.getNumBins()).isEqualTo(2);
assertThat(histogram.getyAxisData().get(0) + histogram.getyAxisData().get(1)).isEqualTo(4);
// Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).build();
// chart.addSeries("histogram 1", histogram.getxAxisData(), histogram.getyAxisData());
// new SwingWrapper(chart).displayChart();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment