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

more bar chart work

parent d9dcb5cd
Branches
No related tags found
No related merge requests found
Showing
with 265 additions and 150 deletions
...@@ -46,7 +46,6 @@ public class AreaChart01 implements ExampleChart { ...@@ -46,7 +46,6 @@ public class AreaChart01 implements ExampleChart {
chart.addSeries("c", new double[] { 0, 1, 3, 8, 9 }, new double[] { -2, -1, 1, 0, 1 }); chart.addSeries("c", new double[] { 0, 1, 3, 8, 9 }, new double[] { -2, -1, 1, 0, 1 });
// Customize Chart // Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW); chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
return chart; return chart;
......
...@@ -21,6 +21,14 @@ ...@@ -21,6 +21,14 @@
*/ */
package com.xeiam.xchart.demo.charts.bar; package com.xeiam.xchart.demo.charts.bar;
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.Chart;
import com.xeiam.xchart.ChartBuilder; import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.SwingWrapper;
...@@ -32,7 +40,7 @@ import com.xeiam.xchart.style.StyleManager.ChartType; ...@@ -32,7 +40,7 @@ import com.xeiam.xchart.style.StyleManager.ChartType;
* <p> * <p>
* Demonstrates the following: * Demonstrates the following:
* <ul> * <ul>
* <li>Number categories * <li>Date categories
* <li>All negative values * <li>All negative values
* <li>Single series * <li>Single series
*/ */
...@@ -49,11 +57,24 @@ public class BarChart02 implements ExampleChart { ...@@ -49,11 +57,24 @@ public class BarChart02 implements ExampleChart {
public Chart getChart() { public Chart getChart() {
// Create Chart // Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart01").xAxisTitle("X").yAxisTitle("Y").build(); Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Units Sold Per Year").xAxisTitle("Year").yAxisTitle("Units Sold").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { -40, -30, -20, -60 });
Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>();
// Customize Chart Random random = new Random();
chart.getStyleManager().setChartTitleVisible(false); DateFormat sdf = new SimpleDateFormat("yyyy");
Date date = null;
for (int i = 1; i <= 8; i++) {
try {
date = sdf.parse("" + (2000 + i));
} catch (ParseException e) {
e.printStackTrace();
}
xData.add(date);
yData.add(random.nextInt(i) + 1);
}
chart.addDateSeries("Model 77", xData, yData);
return chart; return chart;
} }
......
...@@ -49,7 +49,7 @@ public class BarChart03 implements ExampleChart { ...@@ -49,7 +49,7 @@ public class BarChart03 implements ExampleChart {
public Chart getChart() { public Chart getChart() {
// Create Chart // Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart01").xAxisTitle("X").yAxisTitle("Y").build(); Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart03").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { 40, -30, -20, -60 }); chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { 40, -30, -20, -60 });
// Customize Chart // Customize Chart
......
...@@ -49,7 +49,7 @@ public class BarChart04 implements ExampleChart { ...@@ -49,7 +49,7 @@ public class BarChart04 implements ExampleChart {
public Chart getChart() { public Chart getChart() {
// Create Chart // Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart01").xAxisTitle("X").yAxisTitle("Y").build(); Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart04").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { 40, 30, 20, 60 }); chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { 40, 30, 20, 60 });
chart.addSeries("b", new double[] { 10, 20, 30, 40 }, new double[] { 50, 10, 20, 40 }); chart.addSeries("b", new double[] { 10, 20, 30, 40 }, new double[] { 50, 10, 20, 40 });
......
/**
* 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.demo.charts.bar;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.ChartBuilder;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart;
import com.xeiam.xchart.style.StyleManager.ChartType;
/**
* Basic Bar Chart
* <p>
* Demonstrates the following:
* <ul>
* <li>Number categories
* <li>Positive and negative values
* <li>Multiple series
*/
public class BarChart05 implements ExampleChart {
public static void main(String[] args) {
ExampleChart exampleChart = new BarChart05();
Chart chart = exampleChart.getChart();
new SwingWrapper(chart).displayChart();
}
@Override
public Chart getChart() {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("BarChart05").xAxisTitle("X").yAxisTitle("Y").build();
chart.addSeries("a", new double[] { 10, 20, 30, 40 }, new double[] { -40, 30, 20, 60 });
chart.addSeries("b", new double[] { 10, 20, 30, 40 }, new double[] { 50, 10, -20, 40 });
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
return chart;
}
}
...@@ -50,7 +50,7 @@ public class DateChart01 implements ExampleChart { ...@@ -50,7 +50,7 @@ public class DateChart01 implements ExampleChart {
Random random = new Random(); Random random = new Random();
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
......
...@@ -48,7 +48,7 @@ public class DateChart02 implements ExampleChart { ...@@ -48,7 +48,7 @@ public class DateChart02 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
......
...@@ -48,7 +48,7 @@ public class DateChart03 implements ExampleChart { ...@@ -48,7 +48,7 @@ public class DateChart03 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
......
...@@ -48,7 +48,7 @@ public class DateChart04 implements ExampleChart { ...@@ -48,7 +48,7 @@ public class DateChart04 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
......
...@@ -48,7 +48,7 @@ public class DateChart05 implements ExampleChart { ...@@ -48,7 +48,7 @@ public class DateChart05 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
......
...@@ -48,7 +48,7 @@ public class DateChart06 implements ExampleChart { ...@@ -48,7 +48,7 @@ public class DateChart06 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
......
...@@ -24,7 +24,6 @@ import java.util.Date; ...@@ -24,7 +24,6 @@ import java.util.Date;
import java.util.Random; import java.util.Random;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.Series;
import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.demo.charts.ExampleChart; import com.xeiam.xchart.demo.charts.ExampleChart;
...@@ -48,7 +47,7 @@ public class DateChart07 implements ExampleChart { ...@@ -48,7 +47,7 @@ public class DateChart07 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new Chart(800, 600); Chart chart = new Chart(800, 600);
// generates linear data // generate data
Collection<Date> xData = new ArrayList<Date>(); Collection<Date> xData = new ArrayList<Date>();
Collection<Number> yData = new ArrayList<Number>(); Collection<Number> yData = new ArrayList<Number>();
...@@ -69,7 +68,7 @@ public class DateChart07 implements ExampleChart { ...@@ -69,7 +68,7 @@ public class DateChart07 implements ExampleChart {
// Customize Chart // Customize Chart
chart.setChartTitle("DateChart06"); chart.setChartTitle("DateChart06");
chart.getStyleManager().setLegendVisible(false); chart.getStyleManager().setLegendVisible(false);
Series series = chart.addDateSeries("value", xData, yData); chart.addDateSeries("value", xData, yData);
return chart; return chart;
......
...@@ -38,13 +38,13 @@ public class Chart { ...@@ -38,13 +38,13 @@ public class Chart {
private int width; private int width;
private int height; private int height;
private StyleManager styleManager = new StyleManager(); private final StyleManager styleManager;
// Chart Parts // Chart Parts
private Legend chartLegend = new Legend(this); private Legend chartLegend;
private AxisPair axisPair = new AxisPair(this); private AxisPair axisPair;
private Plot plot = new Plot(this); private Plot plot;
private ChartTitle chartTitle = new ChartTitle(this); private ChartTitle chartTitle;
/** /**
* Constructor * Constructor
...@@ -54,6 +54,11 @@ public class Chart { ...@@ -54,6 +54,11 @@ public class Chart {
*/ */
public Chart(int width, int height) { public Chart(int width, int height) {
styleManager = new StyleManager();
chartLegend = new Legend(this);
axisPair = new AxisPair(this);
plot = new Plot(this);
chartTitle = new ChartTitle(this);
this.width = width; this.width = width;
this.height = height; this.height = height;
...@@ -67,10 +72,10 @@ public class Chart { ...@@ -67,10 +72,10 @@ public class Chart {
public Chart(ChartBuilder chartBuilder) { public Chart(ChartBuilder chartBuilder) {
this(chartBuilder.width, chartBuilder.height); this(chartBuilder.width, chartBuilder.height);
setTheme(chartBuilder.theme);
setChartTitle(chartBuilder.title); setChartTitle(chartBuilder.title);
setXAxisTitle(chartBuilder.xAxisTitle); setXAxisTitle(chartBuilder.xAxisTitle);
setYAxisTitle(chartBuilder.yAxisTitle); setYAxisTitle(chartBuilder.yAxisTitle);
setTheme(chartBuilder.theme);
getStyleManager().setChartType(chartBuilder.chartType); getStyleManager().setChartType(chartBuilder.chartType);
} }
...@@ -228,11 +233,6 @@ public class Chart { ...@@ -228,11 +233,6 @@ public class Chart {
*/ */
public void setXAxisTitle(String title) { public void setXAxisTitle(String title) {
if (title == null || title.trim().equalsIgnoreCase("")) {
styleManager.setxAxisTitleVisible(false);
} else {
styleManager.setxAxisTitleVisible(true);
}
this.axisPair.getxAxis().getAxisTitle().setText(title); this.axisPair.getxAxis().getAxisTitle().setText(title);
} }
...@@ -269,7 +269,6 @@ public class Chart { ...@@ -269,7 +269,6 @@ public class Chart {
public void setTheme(Theme theme) { public void setTheme(Theme theme) {
styleManager.setTheme(theme); styleManager.setTheme(theme);
} }
/** /**
......
...@@ -30,13 +30,13 @@ import com.xeiam.xchart.style.theme.XChartTheme; ...@@ -30,13 +30,13 @@ import com.xeiam.xchart.style.theme.XChartTheme;
*/ */
public class ChartBuilder { public class ChartBuilder {
protected ChartType chartType = ChartType.Line; ChartType chartType = ChartType.Line;
protected int width = 800; int width = 800;
protected int height = 600; int height = 600;
protected String title = ""; String title = "";
protected String xAxisTitle = ""; String xAxisTitle = "";
protected String yAxisTitle = ""; String yAxisTitle = "";
protected Theme theme = new XChartTheme(); Theme theme = new XChartTheme();
public ChartBuilder chartType(ChartType chartType) { public ChartBuilder chartType(ChartType chartType) {
......
...@@ -144,6 +144,11 @@ public class AxisTitle implements ChartPart { ...@@ -144,6 +144,11 @@ public class AxisTitle implements ChartPart {
public void setText(String text) { public void setText(String text) {
if (text == null || text.trim().equalsIgnoreCase("")) {
getChart().getStyleManager().setxAxisTitleVisible(false);
} else {
getChart().getStyleManager().setxAxisTitleVisible(true);
}
this.text = text; this.text = text;
} }
} }
...@@ -30,8 +30,8 @@ public class ChartTitle implements ChartPart { ...@@ -30,8 +30,8 @@ public class ChartTitle implements ChartPart {
/** parent */ /** parent */
private final Chart chart; private final Chart chart;
/** the bounds */ // /** the bounds */
private Rectangle bounds; // private Rectangle bounds;
/** the title text */ /** the title text */
private String text = ""; // default to "" private String text = ""; // default to ""
...@@ -82,7 +82,7 @@ public class ChartTitle implements ChartPart { ...@@ -82,7 +82,7 @@ public class ChartTitle implements ChartPart {
@Override @Override
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
bounds = new Rectangle(); // bounds = new Rectangle();
g.setFont(chart.getStyleManager().getChartTitleFont()); g.setFont(chart.getStyleManager().getChartTitleFont());
if (chart.getStyleManager().isChartTitleVisible()) { if (chart.getStyleManager().isChartTitleVisible()) {
...@@ -107,7 +107,7 @@ public class ChartTitle implements ChartPart { ...@@ -107,7 +107,7 @@ public class ChartTitle implements ChartPart {
xOffset = (int) (chart.getPlot().getBounds().getX() + (chart.getPlot().getBounds().getWidth() - rectangle.getWidth()) / 2.0); xOffset = (int) (chart.getPlot().getBounds().getX() + (chart.getPlot().getBounds().getWidth() - rectangle.getWidth()) / 2.0);
yOffset = (int) (chart.getStyleManager().getChartPadding() - rectangle.getY() + chart.getStyleManager().getChartTitlePadding()); yOffset = (int) (chart.getStyleManager().getChartPadding() - rectangle.getY() + chart.getStyleManager().getChartTitlePadding());
bounds = new Rectangle(xOffset, yOffset + ((int) rectangle.getY()), (int) rectangle.getWidth(), (int) (rectangle.getHeight())); // bounds = new Rectangle(xOffset, yOffset + ((int) rectangle.getY()), (int) rectangle.getWidth(), (int) (rectangle.getHeight()));
// g.setColor(Color.green); // g.setColor(Color.green);
// g.draw(bounds); // g.draw(bounds);
......
...@@ -22,8 +22,6 @@ ...@@ -22,8 +22,6 @@
package com.xeiam.xchart.internal.chartpart.axistickcalculator; package com.xeiam.xchart.internal.chartpart.axistickcalculator;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -97,34 +95,6 @@ public abstract class AxisTickCalculator { ...@@ -97,34 +95,6 @@ public abstract class AxisTickCalculator {
} }
} }
/**
* Format a number value, if the override patterns are null, it uses defaults
*
* @param value
* @return
*/
public String formatNumber(BigDecimal value) {
NumberFormat numberFormat = NumberFormat.getNumberInstance(styleManager.getLocale());
BigDecimal absoluteValue = value.abs();
if (absoluteValue.compareTo(new BigDecimal("10000.000001")) == -1 && absoluteValue.compareTo(new BigDecimal(".0009999999")) == 1 || BigDecimal.ZERO.compareTo(value) == 0) {
DecimalFormat normalFormat = (DecimalFormat) numberFormat;
normalFormat.applyPattern(styleManager.getNormalDecimalPattern());
return normalFormat.format(value);
} else {
DecimalFormat scientificFormat = (DecimalFormat) numberFormat;
scientificFormat.applyPattern(styleManager.getScientificDecimalPattern());
return scientificFormat.format(value);
}
}
BigDecimal getFirstPosition(BigDecimal gridStep) { BigDecimal getFirstPosition(BigDecimal gridStep) {
BigDecimal firstPosition; BigDecimal firstPosition;
......
...@@ -90,9 +90,21 @@ public class BarChartAxisTickCalculator extends AxisTickCalculator { ...@@ -90,9 +90,21 @@ public class BarChartAxisTickCalculator extends AxisTickCalculator {
int firstPosition = (int) (gridStep / 2.0); int firstPosition = (int) (gridStep / 2.0);
// generate all tickLabels and tickLocations from the first to last position // generate all tickLabels and tickLocations from the first to last position
NumberFormatter numberFormatter = null;
DateFormatter dateFormatter = null;
if (chart.getAxisPair().getxAxis().getAxisType() == AxisType.Number) {
numberFormatter = new NumberFormatter(styleManager);
} else if (chart.getAxisPair().getxAxis().getAxisType() == AxisType.Date) {
dateFormatter = new DateFormatter(chart.getStyleManager());
}
int counter = 0; int counter = 0;
for (BigDecimal category : categories) { for (BigDecimal category : categories) {
tickLabels.add(formatNumber(category)); if (chart.getAxisPair().getxAxis().getAxisType() == AxisType.Number) {
tickLabels.add(numberFormatter.formatNumber(category));
} else if (chart.getAxisPair().getxAxis().getAxisType() == AxisType.Date) {
tickLabels.add(dateFormatter.formatDate(category));
}
int tickLabelPosition = margin + firstPosition + gridStep * counter++; int tickLabelPosition = margin + firstPosition + gridStep * counter++;
tickLocations.add(tickLabelPosition); tickLocations.add(tickLabelPosition);
} }
......
...@@ -22,11 +22,6 @@ ...@@ -22,11 +22,6 @@
package com.xeiam.xchart.internal.chartpart.axistickcalculator; package com.xeiam.xchart.internal.chartpart.axistickcalculator;
import java.math.BigDecimal; 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.Direction; import com.xeiam.xchart.internal.chartpart.Axis.Direction;
import com.xeiam.xchart.internal.chartpart.AxisPair; import com.xeiam.xchart.internal.chartpart.AxisPair;
...@@ -39,16 +34,7 @@ import com.xeiam.xchart.style.StyleManager; ...@@ -39,16 +34,7 @@ import com.xeiam.xchart.style.StyleManager;
*/ */
public class DateAxisTickCalculator extends AxisTickCalculator { public class DateAxisTickCalculator extends AxisTickCalculator {
public static final long MILLIS_SCALE = TimeUnit.MILLISECONDS.toMillis(1L); DateFormatter dateFormatter;
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 * Constructor
...@@ -62,15 +48,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator { ...@@ -62,15 +48,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator {
public DateAxisTickCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) { public DateAxisTickCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) {
super(axisDirection, workingSpace, minValue, maxValue, styleManager); super(axisDirection, workingSpace, minValue, maxValue, styleManager);
dateFormatter = new DateFormatter(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(); calculate();
} }
...@@ -78,7 +56,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator { ...@@ -78,7 +56,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator {
// a check if all axis data are the exact same values // a check if all axis data are the exact same values
if (minValue == maxValue) { if (minValue == maxValue) {
tickLabels.add(formatDateValue(maxValue)); tickLabels.add(dateFormatter.formatDate(maxValue));
tickLocations.add((int) (workingSpace / 2.0)); tickLocations.add((int) (workingSpace / 2.0));
return; return;
} }
...@@ -95,7 +73,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator { ...@@ -95,7 +73,7 @@ public class DateAxisTickCalculator extends AxisTickCalculator {
// generate all tickLabels and tickLocations from the first to last position // generate all tickLabels and tickLocations from the first to last position
for (BigDecimal tickPosition = firstPosition; tickPosition.compareTo(maxValue) <= 0; tickPosition = tickPosition.add(gridStep)) { for (BigDecimal tickPosition = firstPosition; tickPosition.compareTo(maxValue) <= 0; tickPosition = tickPosition.add(gridStep)) {
tickLabels.add(formatDateValue(tickPosition)); tickLabels.add(dateFormatter.formatDate(tickPosition));
// here we convert tickPosition finally to plot space, i.e. pixels // here we convert tickPosition finally to plot space, i.e. pixels
int tickLabelPosition = (int) (margin + ((tickPosition.subtract(minValue)).doubleValue() / (maxValue.subtract(minValue)).doubleValue() * tickSpace)); int tickLabelPosition = (int) (margin + ((tickPosition.subtract(minValue)).doubleValue() / (maxValue.subtract(minValue)).doubleValue() * tickSpace));
tickLocations.add(tickLabelPosition); tickLocations.add(tickLabelPosition);
...@@ -115,9 +93,9 @@ public class DateAxisTickCalculator extends AxisTickCalculator { ...@@ -115,9 +93,9 @@ public class DateAxisTickCalculator extends AxisTickCalculator {
long gridStepHint = (long) (span / (double) tickSpace * DEFAULT_TICK_MARK_STEP_HINT_X); long gridStepHint = (long) (span / (double) tickSpace * DEFAULT_TICK_MARK_STEP_HINT_X);
timeUnit = getTimeUnit(gridStepHint); long timeUnit = dateFormatter.getTimeUnit(gridStepHint);
BigDecimal gridStep = null; BigDecimal gridStep = null;
int[] steps = validTickStepsMap.get(timeUnit); int[] steps = dateFormatter.getValidTickStepsMap().get(timeUnit);
for (int i = 0; i < steps.length - 1; i++) { for (int i = 0; i < steps.length - 1; i++) {
if (gridStepHint < (timeUnit * steps[i] + timeUnit * steps[i + 1]) / 2.0) { if (gridStepHint < (timeUnit * steps[i] + timeUnit * steps[i + 1]) / 2.0) {
gridStep = new BigDecimal(timeUnit * steps[i]); gridStep = new BigDecimal(timeUnit * steps[i]);
...@@ -128,55 +106,4 @@ public class DateAxisTickCalculator extends AxisTickCalculator { ...@@ -128,55 +106,4 @@ public class DateAxisTickCalculator extends AxisTickCalculator {
return gridStep; return gridStep;
} }
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
*
* @param value
* @param min
* @param max
* @return
*/
public String formatDateValue(BigDecimal value) {
String datePattern;
// intelligently set date pattern if none is given
if (timeUnit == MILLIS_SCALE) {
datePattern = "ss.SSS";
} else if (timeUnit == SEC_SCALE) {
datePattern = "mm:ss";
} else if (timeUnit == MIN_SCALE) {
datePattern = "HH:mm";
} 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";
}
SimpleDateFormat simpleDateformat = new SimpleDateFormat(datePattern, styleManager.getLocale());
simpleDateformat.setTimeZone(styleManager.getTimezone());
simpleDateformat.applyPattern(datePattern);
return simpleDateformat.format(value.longValueExact());
}
} }
/**
* 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.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.style.StyleManager;
/**
* @author timmolter
*/
public class DateFormatter {
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;
private final StyleManager styleManager;
/**
* Constructor
*/
public DateFormatter(StyleManager styleManager) {
this.styleManager = 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 });
}
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
*
* @param value
* @param min
* @param max
* @return
*/
String formatDate(BigDecimal value) {
String datePattern;
// intelligently set date pattern if none is given
if (timeUnit == MILLIS_SCALE) {
datePattern = "ss.SSS";
} else if (timeUnit == SEC_SCALE) {
datePattern = "mm:ss";
} else if (timeUnit == MIN_SCALE) {
datePattern = "HH:mm";
} 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";
}
SimpleDateFormat simpleDateformat = new SimpleDateFormat(datePattern, styleManager.getLocale());
simpleDateformat.setTimeZone(styleManager.getTimezone());
simpleDateformat.applyPattern(datePattern);
return simpleDateformat.format(value.longValueExact());
}
Map<Long, int[]> getValidTickStepsMap() {
return validTickStepsMap;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment