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

clean up and added unit test

parent 0ce95ecf
Branches
No related tags found
No related merge requests found
......@@ -64,7 +64,7 @@ public class DateFormatter {
validTickStepsMap.put(YEAR_SCALE, new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 });
}
long getTimeUnit(long gridStepHint) {
public long getTimeUnit(long gridStepHint) {
for (Entry<Long, int[]> entry : validTickStepsMap.entrySet()) {
......@@ -86,7 +86,7 @@ public class DateFormatter {
* @param max
* @return
*/
String formatDate(BigDecimal value, long timeUnit) {
public String formatDate(BigDecimal value, long timeUnit) {
String datePattern;
......
/**
* Copyright (C) 2013 Xeiam LLC http://xeiam.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.xeiam.xchart.chart;
import java.util.ArrayList;
import java.util.List;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.Series;
import com.xeiam.xchart.SwingWrapper;
/**
* @author timmolter
*/
public class NoBug {
static void plot(Chart chart, int n) {
chart.setChartTitle("title");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
List<Number> x = new ArrayList<Number>();
List<Number> y = new ArrayList<Number>();
String seriesName = addOneSeries(chart, n, x, y);
}
private static String addOneSeries(Chart chart, int n, List<Number> x, List<Number> y) {
for (int i = 0; i <= 10; i++) {
x.add(i / 10.);
y.add(i * n / 10.);
}
String seriesName = "series " + n;
Series series = chart.addSeries(seriesName, x, y);
chart.getStyleManager().setxAxisMin(0);
chart.getStyleManager().setxAxisMax(1);
chart.getStyleManager().setyAxisMin(-5);
chart.getStyleManager().setyAxisMax(5);
return seriesName;
}
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
Chart chart = new Chart(700, 500);
plot(chart, i);
new SwingWrapper(chart).displayChart();
}
}
}
......@@ -21,6 +21,9 @@
*/
package com.xeiam.xchart.unit;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
......@@ -28,8 +31,8 @@ import java.util.List;
import org.junit.Test;
import com.xeiam.xchart.StyleManager;
import com.xeiam.xchart.internal.chartpart.AxisTickDateCalculator;
import com.xeiam.xchart.internal.chartpart.Axis.Direction;
import com.xeiam.xchart.internal.chartpart.AxisTickDateCalculator;
/**
* @author timmolter
......@@ -43,12 +46,12 @@ public class DateAxisTickCalculatorTest {
List<String> tickLabels = decimalAxisTickCalculator.getTickLabels();
System.out.println(Arrays.toString(tickLabels.toArray()));
// assertThat(tickLabels.size(), equalTo(7));
// assertThat(tickLabels.get(0), equalTo("-15"));
assertThat(tickLabels.size(), equalTo(6));
assertThat(tickLabels.get(0), equalTo("17:50"));
// List<Integer> tickLocations = decimalAxisTickCalculator.getTickLocations();
// System.out.println(Arrays.toString(tickLocations.toArray()));
// assertThat(tickLocations.size(), equalTo(7));
// assertThat(tickLocations.get(0), equalTo(15));
List<Integer> tickLocations = decimalAxisTickCalculator.getTickLocations();
System.out.println(Arrays.toString(tickLocations.toArray()));
assertThat(tickLocations.size(), equalTo(6));
assertThat(tickLocations.get(0), equalTo(100));
}
}
/**
* Copyright (C) 2013 Xeiam LLC http://xeiam.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.xeiam.xchart.unit;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.Test;
import com.xeiam.xchart.StyleManager;
import com.xeiam.xchart.internal.chartpart.DateFormatter;
/**
* @author timmolter
*/
public class DateFormatterTest {
private final Locale locale = Locale.US;
@Test
public void testDateFormatting() {
StyleManager styleManager = new StyleManager();
DateFormatter dateFormatter = new DateFormatter(styleManager);
TimeZone timeZone = TimeZone.getTimeZone("UTC");
styleManager.setLocale(locale);
styleManager.setTimezone(timeZone);
// ms
BigDecimal value = new BigDecimal(1358108105531L);
BigDecimal min = new BigDecimal(1358108105100L);
BigDecimal max = new BigDecimal(1358108105900L);
long span = Math.abs(max.subtract(min).longValue()); // in data space
long gridStepHint = (long) (span / (double) 1000 * 74);
long timeUnit = dateFormatter.getTimeUnit(gridStepHint);
String stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("05.531"));
// sec
value = new BigDecimal(1358108105000L);
min = new BigDecimal(1358108101000L);
max = new BigDecimal(1358108109000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("05.000"));
// min
value = new BigDecimal(1358111750000L);
min = new BigDecimal(1358111690000L);
max = new BigDecimal(1358111870000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("15:50"));
// hour
value = new BigDecimal(1358111870000L);
min = new BigDecimal(1358101070000L);
max = new BigDecimal(1358115470000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("21:17"));
// day
value = new BigDecimal(1358112317000L);
min = new BigDecimal(1357939517000L);
max = new BigDecimal(1358285117000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("13-21"));
// week
value = new BigDecimal(1358112317000L);
min = new BigDecimal(1357075517000L);
max = new BigDecimal(1359149117000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("01-13"));
// month
value = new BigDecimal(1358112838000L);
min = new BigDecimal(1354397638000L);
max = new BigDecimal(1361223238000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("01-13"));
// year
value = new BigDecimal(1358113402000L);
min = new BigDecimal(1263419002000L);
max = new BigDecimal(1421185402000L);
span = Math.abs(max.subtract(min).longValue()); // in data space
gridStepHint = (long) (span / (double) 1000 * 74);
timeUnit = dateFormatter.getTimeUnit(gridStepHint);
stringValue = dateFormatter.formatDate(value, timeUnit);
assertThat(stringValue, equalTo("2013-01"));
}
}
......@@ -35,7 +35,7 @@ import com.xeiam.xchart.internal.chartpart.NumberFormatter;
/**
* @author timmolter
*/
public class ValueFormatterTest {
public class NumberFormatterTest {
private final Locale locale = Locale.US;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment