diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java index f906f95a52c0df7fa06e2a5415bee8a816ffa99b..ce69b6b9e4a4f9c4b23bac17c3fd9d4c5c5911d9 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java @@ -30,6 +30,13 @@ import javax.swing.tree.TreeSelectionModel; import com.xeiam.xchart.XChartPanel; import com.xeiam.xchart.demo.charts.area.AreaChart01; +import com.xeiam.xchart.demo.charts.date.DateChart01; +import com.xeiam.xchart.demo.charts.date.DateChart02; +import com.xeiam.xchart.demo.charts.date.DateChart03; +import com.xeiam.xchart.demo.charts.date.DateChart04; +import com.xeiam.xchart.demo.charts.date.DateChart05; +import com.xeiam.xchart.demo.charts.date.DateChart06; +import com.xeiam.xchart.demo.charts.date.DateChart07; import com.xeiam.xchart.demo.charts.line.LineChart01; import com.xeiam.xchart.demo.charts.line.LineChart02; import com.xeiam.xchart.demo.charts.line.LineChart03; @@ -160,20 +167,45 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { chart = new DefaultMutableTreeNode(new ChartInfo("LineChart11 - Using ChartBuilder to Make a Chart", new LineChart11().getChart())); category.add(chart); - // Second category + // Scatter category category = new DefaultMutableTreeNode("Scatter Charts"); top.add(category); chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob", new ScatterChart01().getChart())); category.add(chart); - // Second category + // Area category category = new DefaultMutableTreeNode("Area Charts"); top.add(category); chart = new DefaultMutableTreeNode(new ChartInfo("AreaChart01 - 3-Series", new AreaChart01().getChart())); category.add(chart); + // Date category + category = new DefaultMutableTreeNode("Date Charts"); + top.add(category); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart01 - Millisecond Scale", new DateChart01().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart02 - Second Scale", new DateChart02().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart03 - Minute Scale", new DateChart03().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart04 - Hour Scale", new DateChart04().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart05 - Day Scale", new DateChart05().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart06 - Month Scale", new DateChart06().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("DateChart07 - Year Scale", new DateChart07().getChart())); + category.add(chart); + } /** diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java index c66e9fd4db05f84cee9625b1c3e1be376997cc9f..d0194a968fb80a6f6a5372a748e1fd501e10133b 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java @@ -19,7 +19,6 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Random; @@ -29,6 +28,11 @@ import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.line.ExampleChart; import com.xeiam.xchart.style.Series; +/** + * Millisecond scale + * + * @author timmolter + */ public class DateChart01 implements ExampleChart { public static void main(String[] args) { @@ -67,7 +71,6 @@ public class DateChart01 implements ExampleChart { chart.setChartTitle("DateChart01"); chart.getStyleManager().setLegendVisible(false); Series series = chart.addDateSeries("value", xData, yData); - System.out.println(Arrays.toString(xData.toArray())); return chart; diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java index 5c05e629b81354cc7ed8b06313fc3e9d51b28ee1..fd2db2f19d1cfc7b07ecb0cbada50b9aa4de8e7d 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java @@ -19,7 +19,6 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Random; @@ -29,6 +28,11 @@ import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.line.ExampleChart; import com.xeiam.xchart.style.Series; +/** + * Second scale + * + * @author timmolter + */ public class DateChart02 implements ExampleChart { public static void main(String[] args) { @@ -66,7 +70,6 @@ public class DateChart02 implements ExampleChart { chart.setChartTitle("DateChart02"); chart.getStyleManager().setLegendVisible(false); Series series = chart.addDateSeries("value", xData, yData); - System.out.println(Arrays.toString(xData.toArray())); return chart; diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java index 739c4f132812fd6bb7cc2c5a332fca31be2a6a1c..157d8215756dacdb7ab78ba421be69c540e4340c 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java @@ -19,7 +19,6 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Random; @@ -29,6 +28,11 @@ import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.line.ExampleChart; import com.xeiam.xchart.style.Series; +/** + * Minute scale + * + * @author timmolter + */ public class DateChart03 implements ExampleChart { public static void main(String[] args) { @@ -66,7 +70,6 @@ public class DateChart03 implements ExampleChart { chart.setChartTitle("DateChart03"); chart.getStyleManager().setLegendVisible(false); Series series = chart.addDateSeries("value", xData, yData); - System.out.println(Arrays.toString(xData.toArray())); return chart; diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart04.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart04.java new file mode 100644 index 0000000000000000000000000000000000000000..cf0ae7701bbad1b416fc92fb36280e9042156497 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart04.java @@ -0,0 +1,77 @@ +/** + * Copyright 2011-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.demo.charts.date; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Random; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.SwingWrapper; +import com.xeiam.xchart.demo.charts.line.ExampleChart; +import com.xeiam.xchart.style.Series; + +/** + * Hour scale + * + * @author timmolter + */ +public class DateChart04 implements ExampleChart { + + public static void main(String[] args) { + + ExampleChart exampleChart = new DateChart04(); + Chart chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart getChart() { + + // Create Chart + Chart chart = new Chart(800, 600); + + // generates linear data + Collection<Date> xData = new ArrayList<Date>(); + Collection<Number> yData = new ArrayList<Number>(); + + Random random = new Random(); + + DateFormat sdf = new SimpleDateFormat("dd-HH"); + Date date = null; + for (int i = 1; i <= 14; i++) { + try { + date = sdf.parse("25-" + (2 * i + random.nextInt(2))); + } catch (ParseException e) { + e.printStackTrace(); + } + xData.add(date); + yData.add(Math.random() * i); + } + + // Customize Chart + chart.setChartTitle("DateChart04"); + chart.getStyleManager().setLegendVisible(false); + Series series = chart.addDateSeries("value", xData, yData); + + return chart; + + } +} diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart05.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart05.java new file mode 100644 index 0000000000000000000000000000000000000000..b71be652c0d5cfc8b93b122d7ef5fb8e924d80e2 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart05.java @@ -0,0 +1,77 @@ +/** + * Copyright 2011-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.demo.charts.date; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Random; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.SwingWrapper; +import com.xeiam.xchart.demo.charts.line.ExampleChart; +import com.xeiam.xchart.style.Series; + +/** + * Day scale + * + * @author timmolter + */ +public class DateChart05 implements ExampleChart { + + public static void main(String[] args) { + + ExampleChart exampleChart = new DateChart05(); + Chart chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart getChart() { + + // Create Chart + Chart chart = new Chart(800, 600); + + // generates linear data + Collection<Date> xData = new ArrayList<Date>(); + Collection<Number> yData = new ArrayList<Number>(); + + Random random = new Random(); + + DateFormat sdf = new SimpleDateFormat("MM-dd"); + Date date = null; + for (int i = 1; i <= 14; i++) { + try { + date = sdf.parse("02-" + (6 * i + random.nextInt(2))); + } catch (ParseException e) { + e.printStackTrace(); + } + xData.add(date); + yData.add(Math.random() * i); + } + + // Customize Chart + chart.setChartTitle("DateChart05"); + chart.getStyleManager().setLegendVisible(false); + Series series = chart.addDateSeries("value", xData, yData); + + return chart; + + } +} diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart06.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart06.java new file mode 100644 index 0000000000000000000000000000000000000000..09d66139a9ddd84b915d859de284a75b5a60a8bf --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart06.java @@ -0,0 +1,77 @@ +/** + * Copyright 2011-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.demo.charts.date; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Random; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.SwingWrapper; +import com.xeiam.xchart.demo.charts.line.ExampleChart; +import com.xeiam.xchart.style.Series; + +/** + * Month scale + * + * @author timmolter + */ +public class DateChart06 implements ExampleChart { + + public static void main(String[] args) { + + ExampleChart exampleChart = new DateChart06(); + Chart chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart getChart() { + + // Create Chart + Chart chart = new Chart(800, 600); + + // generates linear data + Collection<Date> xData = new ArrayList<Date>(); + Collection<Number> yData = new ArrayList<Number>(); + + Random random = new Random(); + + DateFormat sdf = new SimpleDateFormat("yyyy-MM"); + Date date = null; + for (int i = 1; i <= 14; i++) { + try { + date = sdf.parse("2013-" + (2 * i + random.nextInt(1))); + } catch (ParseException e) { + e.printStackTrace(); + } + xData.add(date); + yData.add(Math.random() * i); + } + + // Customize Chart + chart.setChartTitle("DateChart06"); + chart.getStyleManager().setLegendVisible(false); + Series series = chart.addDateSeries("value", xData, yData); + + return chart; + + } +} diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart07.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart07.java new file mode 100644 index 0000000000000000000000000000000000000000..0a7b999b2efd9219d01ab066fc5b19a097c87a0b --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart07.java @@ -0,0 +1,77 @@ +/** + * Copyright 2011-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.demo.charts.date; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Random; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.SwingWrapper; +import com.xeiam.xchart.demo.charts.line.ExampleChart; +import com.xeiam.xchart.style.Series; + +/** + * Year scale + * + * @author timmolter + */ +public class DateChart07 implements ExampleChart { + + public static void main(String[] args) { + + ExampleChart exampleChart = new DateChart07(); + Chart chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart getChart() { + + // Create Chart + Chart chart = new Chart(800, 600); + + // generates linear data + Collection<Date> xData = new ArrayList<Date>(); + Collection<Number> yData = new ArrayList<Number>(); + + Random random = new Random(); + + DateFormat sdf = new SimpleDateFormat("yyyy-MM"); + Date date = null; + for (int i = 1; i <= 14; i++) { + try { + date = sdf.parse("" + (2001 + i) + "-" + random.nextInt(12)); + } catch (ParseException e) { + e.printStackTrace(); + } + xData.add(date); + yData.add(Math.random() * i); + } + + // Customize Chart + chart.setChartTitle("DateChart06"); + chart.getStyleManager().setLegendVisible(false); + Series series = chart.addDateSeries("value", xData, yData); + + return chart; + + } +} diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/axistickcalculator/DateAxisTickCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/axistickcalculator/DateAxisTickCalculator.java index cbb1c5900c30d2bea846a666b25e23ce36f52548..eb51da94b5fd65282fdd7108ede94d5b100bab81 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/axistickcalculator/DateAxisTickCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/axistickcalculator/DateAxisTickCalculator.java @@ -23,6 +23,9 @@ package com.xeiam.xchart.internal.chartpart.axistickcalculator; import java.math.BigDecimal; import java.text.SimpleDateFormat; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; import java.util.concurrent.TimeUnit; import com.xeiam.xchart.internal.chartpart.Axis.AxisType; @@ -37,13 +40,16 @@ import com.xeiam.xchart.style.StyleManager; */ public class DateAxisTickCalculator extends AxisTickCalculator { - public static final long SEC_SCALE = TimeUnit.SECONDS.toMillis(2L); - public static final long MIN_SCALE = TimeUnit.MINUTES.toMillis(2L); - public static final long HOUR_SCALE = TimeUnit.HOURS.toMillis(2L); - public static final long DAY_SCALE = TimeUnit.DAYS.toMillis(2L); - public static final long WEEK_SCALE = TimeUnit.DAYS.toMillis(2L) * 7; - public static final long MONTH_SCALE = TimeUnit.DAYS.toMillis(2L) * 31; - public static final long YEAR_SCALE = TimeUnit.DAYS.toMillis(2L) * 365; + public static final long MILLIS_SCALE = TimeUnit.MILLISECONDS.toMillis(1L); + public static final long SEC_SCALE = TimeUnit.SECONDS.toMillis(1L); + public static final long MIN_SCALE = TimeUnit.MINUTES.toMillis(1L); + public static final long HOUR_SCALE = TimeUnit.HOURS.toMillis(1L); + public static final long DAY_SCALE = TimeUnit.DAYS.toMillis(1L); + public static final long MONTH_SCALE = TimeUnit.DAYS.toMillis(1L) * 31; + public static final long YEAR_SCALE = TimeUnit.DAYS.toMillis(1L) * 365; + + private Map<Long, int[]> validTickStepsMap; + private long timeUnit; /** * Constructor @@ -57,6 +63,15 @@ public class DateAxisTickCalculator extends AxisTickCalculator { public DateAxisTickCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) { super(axisDirection, workingSpace, minValue, maxValue, styleManager); + + validTickStepsMap = new TreeMap<Long, int[]>(); + validTickStepsMap.put(MILLIS_SCALE, new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 }); + validTickStepsMap.put(SEC_SCALE, new int[] { 1, 2, 5, 10, 15, 20, 30, 60 }); + validTickStepsMap.put(MIN_SCALE, new int[] { 1, 2, 3, 5, 10, 15, 20, 30, 60 }); + validTickStepsMap.put(HOUR_SCALE, new int[] { 1, 2, 4, 6, 12, 24 }); + validTickStepsMap.put(DAY_SCALE, new int[] { 1, 2, 3, 5, 10, 15, 31 }); + validTickStepsMap.put(MONTH_SCALE, new int[] { 1, 2, 3, 4, 6, 12 }); + validTickStepsMap.put(YEAR_SCALE, new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 }); calculate(); } @@ -64,7 +79,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator { // a check if all axis data are the exact same values if (minValue == maxValue) { - tickLabels.add(formatDateValue(maxValue, maxValue, maxValue)); + tickLabels.add(formatDateValue(maxValue)); tickLocations.add((int) (workingSpace / 2.0)); return; } @@ -81,7 +96,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator { // generate all tickLabels and tickLocations from the first to last position for (BigDecimal tickPosition = firstPosition; tickPosition.compareTo(maxValue) <= 0; tickPosition = tickPosition.add(gridStep)) { - tickLabels.add(formatDateValue(tickPosition, minValue, maxValue)); + tickLabels.add(formatDateValue(tickPosition)); // here we convert tickPosition finally to plot space, i.e. pixels int tickLabelPosition = (int) (margin + ((tickPosition.subtract(minValue)).doubleValue() / (maxValue.subtract(minValue)).doubleValue() * tickSpace)); tickLocations.add(tickLabelPosition); @@ -98,20 +113,18 @@ public class DateAxisTickCalculator extends AxisTickCalculator { public BigDecimal getGridStep(int tickSpace) { // the span of the data - double span = Math.abs(maxValue.subtract(minValue).doubleValue()); // in data space - - double gridStepHint = span / tickSpace * DEFAULT_TICK_MARK_STEP_HINT_X; - - BigDecimal gridStep; - if (span < SEC_SCALE) { - // decimal - gridStep = getGridStepDecimal(gridStepHint); - - } else { - - // date - gridStep = getGridStepDecimal(gridStepHint); - + long span = Math.abs(maxValue.subtract(minValue).longValue()); // in data space + + long gridStepHint = (long) (span / (double) tickSpace * DEFAULT_TICK_MARK_STEP_HINT_X); + + timeUnit = getTimeUnit(gridStepHint); + BigDecimal gridStep = null; + int[] steps = validTickStepsMap.get(timeUnit); + for (int i = 0; i < steps.length - 1; i++) { + if (gridStepHint < (timeUnit * steps[i] + timeUnit * steps[i + 1]) / 2.0) { + gridStep = new BigDecimal(timeUnit * steps[i]); + break; + } } return gridStep; @@ -129,6 +142,21 @@ public class DateAxisTickCalculator extends AxisTickCalculator { return firstPosition; } + private long getTimeUnit(long gridStepHint) { + + for (Entry<Long, int[]> entry : validTickStepsMap.entrySet()) { + + long groupMagnitude = entry.getKey(); + int[] steps = entry.getValue(); + long validTickStepMagnitude = (long) ((groupMagnitude * steps[steps.length - 2] + groupMagnitude * steps[steps.length - 1]) / 2.0); + if (gridStepHint < validTickStepMagnitude) { + return groupMagnitude; + } + } + + return YEAR_SCALE; + } + /** * Format a date value * @@ -137,28 +165,23 @@ public class DateAxisTickCalculator extends AxisTickCalculator { * @param max * @return */ - public String formatDateValue(BigDecimal value, BigDecimal min, BigDecimal max) { + public String formatDateValue(BigDecimal value) { String datePattern; - // TODO check if min and max are the same, then calculate this differently // intelligently set date pattern if none is given - long diff = max.subtract(min).longValue(); - - if (diff < SEC_SCALE) { + if (timeUnit == MILLIS_SCALE) { datePattern = "ss.SSS"; - } else if (diff < MIN_SCALE) { + } else if (timeUnit == SEC_SCALE) { datePattern = "mm:ss"; - } else if (diff < HOUR_SCALE) { + } else if (timeUnit == MIN_SCALE) { datePattern = "HH:mm"; - } else if (diff < DAY_SCALE) { - datePattern = "EEE HH:mm"; - } else if (diff < WEEK_SCALE) { - datePattern = "EEE"; - } else if (diff < MONTH_SCALE) { - datePattern = "MMM-dd"; - } else if (diff < YEAR_SCALE) { - datePattern = "yyyy:MMM"; + } else if (timeUnit == HOUR_SCALE) { + datePattern = "dd-HH"; + } else if (timeUnit == DAY_SCALE) { + datePattern = "MM-dd"; + } else if (timeUnit == MONTH_SCALE) { + datePattern = "yyyy-MM"; } else { datePattern = "yyyy"; } diff --git a/xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java b/xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java index 8a1594481ee9149a11544c9082263d0ebdd2d176..26aff9a68f397fed56006dac069dd5c35803400f 100644 --- a/xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/unit/ValueFormatterTest.java @@ -26,7 +26,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.math.BigDecimal; import java.util.Locale; -import java.util.TimeZone; import org.junit.Test; @@ -135,72 +134,4 @@ public class ValueFormatterTest { } - @Test - public void testDateFormatting() { - - StyleManager styleManager = new StyleManager(); - DateAxisTickCalculator dateAxisTickCalculator = new DateAxisTickCalculator(Direction.X, 1000, new BigDecimal(10), new BigDecimal(90), 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); - String stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("05.531")); - - // sec - value = new BigDecimal(1358108105000L); - min = new BigDecimal(1358108101000L); - max = new BigDecimal(1358108109000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("15:05")); - - // min - value = new BigDecimal(1358111750000L); - min = new BigDecimal(1358111690000L); - max = new BigDecimal(1358111870000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("21:15")); - - // hour - value = new BigDecimal(1358111870000L); - min = new BigDecimal(1358101070000L); - max = new BigDecimal(1358115470000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("Sun 21:17")); - - // day - value = new BigDecimal(1358112317000L); - min = new BigDecimal(1357939517000L); - max = new BigDecimal(1358285117000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("Sun")); - - // week - value = new BigDecimal(1358112317000L); - min = new BigDecimal(1357075517000L); - max = new BigDecimal(1359149117000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("Jan-13")); - - // month - value = new BigDecimal(1358112838000L); - min = new BigDecimal(1354397638000L); - max = new BigDecimal(1361223238000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("2013:Jan")); - - // year - value = new BigDecimal(1358113402000L); - min = new BigDecimal(1263419002000L); - max = new BigDecimal(1421185402000L); - stringValue = dateAxisTickCalculator.formatDateValue(value, min, max); - assertThat(stringValue, equalTo("2013")); - - } }