From 12cfdeadd43ec68a67f2ca6162fba2c07075ecf7 Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Wed, 4 Mar 2015 10:40:42 +0100 Subject: [PATCH] Tweaked Histogram code and made unit test --- pom.xml | 7 +++ .../main/java/com/xeiam/xchart/Histogram.java | 19 ++++---- .../java/com/xeiam/xchart/HistogramTest.java | 44 +++++++++++++++++++ 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 xchart/src/test/java/com/xeiam/xchart/HistogramTest.java diff --git a/pom.xml b/pom.xml index d13bd84a..d9cbb308 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/xchart/src/main/java/com/xeiam/xchart/Histogram.java b/xchart/src/main/java/com/xeiam/xchart/Histogram.java index ffd99ce7..fa307af3 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Histogram.java +++ b/xchart/src/main/java/com/xeiam/xchart/Histogram.java @@ -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; } } diff --git a/xchart/src/test/java/com/xeiam/xchart/HistogramTest.java b/xchart/src/test/java/com/xeiam/xchart/HistogramTest.java new file mode 100644 index 00000000..ebe3a080 --- /dev/null +++ b/xchart/src/test/java/com/xeiam/xchart/HistogramTest.java @@ -0,0 +1,44 @@ +/** + * 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(); + + } +} -- GitLab