diff --git a/Sample_Chart.png b/Sample_Chart.png new file mode 100644 index 0000000000000000000000000000000000000000..03e05bd1563293430821ae7728851814299036eb Binary files /dev/null and b/Sample_Chart.png differ diff --git a/src/main/java/com/xeiam/xchart/AxisPair.java b/src/main/java/com/xeiam/xchart/AxisPair.java index edd8a8fa0140f609201a14f1c69d36c9ba7e99b4..2c599cbd55acf6580858bd50e6515d86364ea9c7 100644 --- a/src/main/java/com/xeiam/xchart/AxisPair.java +++ b/src/main/java/com/xeiam/xchart/AxisPair.java @@ -17,13 +17,14 @@ package com.xeiam.xchart; import java.awt.Graphics2D; import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import com.xeiam.xchart.interfaces.IChartPart; import com.xeiam.xchart.series.Series; - /** * @author timmolter */ @@ -57,45 +58,42 @@ public class AxisPair implements IChartPart { * @param xData * @param yData */ - public Series addSeries(String seriesName, double[] xData, double[] yData) { + public Series addSeries(String seriesName, Collection<Number> xData, Collection<Number> yData) { // Sanity checks if (seriesName == null) { - throw new RuntimeException("Series Name cannot be null!!!"); + throw new IllegalArgumentException("Series Name cannot be null!!!"); } if (yData == null) { - throw new RuntimeException("Y-Axis data cannot be null!!!"); + throw new IllegalArgumentException("Y-Axis data cannot be null!!!"); } - if (yData.length == 0) { - throw new RuntimeException("Y-Axis data cannot be empty!!!"); + if (yData.size() == 0) { + throw new IllegalArgumentException("Y-Axis data cannot be empty!!!"); } - if (xData != null && xData.length == 0) { - throw new RuntimeException("X-Axis data cannot be empty!!!"); + if (xData != null && xData.size() == 0) { + throw new IllegalArgumentException("X-Axis data cannot be empty!!!"); } - if (xData != null && xData.length == 1 && Double.isNaN(yData[0])) { - throw new RuntimeException("X-Axis data cannot contain a single NaN value!!!"); + if (xData != null && xData.size() == 1 && Double.isNaN(xData.iterator().next().doubleValue())) { + throw new IllegalArgumentException("X-Axis data cannot contain a single NaN value!!!"); } - if (yData.length == 1 && Double.isNaN(yData[0])) { - throw new RuntimeException("Y-Axis data cannot contain a single NaN value!!!"); + if (yData.size() == 1 && Double.isNaN(yData.iterator().next().doubleValue())) { + throw new IllegalArgumentException("Y-Axis data cannot contain a single NaN value!!!"); } Series series; if (xData != null) { - verifyValues(xData); - verifyValues(yData); series = new Series(seriesName, xData, yData); } else { // generate xData - double[] generatedXData = new double[yData.length]; - verifyValues(yData); - for (int i = 1; i < yData.length; i++) { - generatedXData[i] = i; + Collection<Number> generatedXData = new ArrayList<Number>(); + for (int i = 1; i < yData.size(); i++) { + generatedXData.add(i); } series = new Series(seriesName, generatedXData, yData); } // Sanity check - if (xData != null && xData.length != yData.length) { - throw new RuntimeException("X and Y-Axis lengths are not the same!!! "); + if (xData != null && xData.size() != yData.size()) { + throw new IllegalArgumentException("X and Y-Axis lengths are not the same!!! "); } seriesMap.put(seriesCount++, series); @@ -107,22 +105,6 @@ public class AxisPair implements IChartPart { return series; } - /** - * Checks for invalid values in data array - * - * @param data - */ - private void verifyValues(double[] data) { - - for (int i = 0; i < data.length; i++) { - if (data[i] == Double.POSITIVE_INFINITY) { - throw new RuntimeException("Axis data cannot contain Double.POSITIVE_INFINITY!!!"); - } else if (data[i] == Double.NEGATIVE_INFINITY) { - throw new RuntimeException("Axis data cannot contain Double.NEGATIVE_INFINITY!!!"); - } - } - } - protected Axis getXAxis() { return xAxis; diff --git a/src/main/java/com/xeiam/xchart/Chart.java b/src/main/java/com/xeiam/xchart/Chart.java index 3b6ee801275f3ef657898cbb2468c349b5a818ef..0350c7f89a17988dddad7330bdd9e35955d6850f 100644 --- a/src/main/java/com/xeiam/xchart/Chart.java +++ b/src/main/java/com/xeiam/xchart/Chart.java @@ -17,6 +17,8 @@ package com.xeiam.xchart; import java.awt.Graphics2D; import java.awt.RenderingHints; +import java.util.ArrayList; +import java.util.Collection; import com.xeiam.xchart.series.Series; import com.xeiam.xchart.series.SeriesColor; @@ -107,13 +109,31 @@ public class Chart { return plot; } - // EXTERNAL GETTERS & SETTERS + // PUBLIC SETTERS - public Series addSeries(String seriesName, double[] xData, double[] yData) { + public Series addSeries(String seriesName, Collection<Number> xData, Collection<Number> yData) { return axisPair.addSeries(seriesName, xData, yData); } + @Deprecated + public Series addSeries(String seriesName, double[] xData, double[] yData) { + + Collection<Number> xDataNumber = null; + if (xData != null) { + xDataNumber = new ArrayList<Number>(); + for (double d : xData) { + xDataNumber.add(new Double(d)); + } + } + Collection<Number> yDataNumber = new ArrayList<Number>(); + for (double d : yData) { + yDataNumber.add(new Double(d)); + } + + return axisPair.addSeries(seriesName, xDataNumber, yDataNumber); + } + public void setChartTitle(String title) { this.chartTitle.setText(title); diff --git a/src/main/java/com/xeiam/xchart/PlotContent.java b/src/main/java/com/xeiam/xchart/PlotContent.java index 7c4f8870a6061a7a502699b521c28490a49076e7..8e83f5c897e28ffee8c9b16804413a09ab41b216 100644 --- a/src/main/java/com/xeiam/xchart/PlotContent.java +++ b/src/main/java/com/xeiam/xchart/PlotContent.java @@ -17,12 +17,13 @@ package com.xeiam.xchart; import java.awt.Graphics2D; import java.awt.Rectangle; +import java.util.Collection; +import java.util.Iterator; import java.util.Map; import com.xeiam.xchart.interfaces.IChartPart; import com.xeiam.xchart.series.Series; - /** * @author timmolter */ @@ -63,30 +64,27 @@ public class PlotContent implements IChartPart { int yTopMargin = AxisPair.getMargin((int) bounds.getHeight(), yTickSpace); // data points - double[] xData = series.getxData(); + Collection<Number> xData = series.getxData(); double xMin = chart.getAxisPair().getXAxis().getMin(); double xMax = chart.getAxisPair().getXAxis().getMax(); - double[] yData = series.getyData(); + Collection<Number> yData = series.getyData(); double yMin = chart.getAxisPair().getYAxis().getMin(); double yMax = chart.getAxisPair().getYAxis().getMax(); int previousX = Integer.MIN_VALUE; int previousY = Integer.MIN_VALUE; - for (int i = 0; i < xData.length; i++) { - - if (Double.isInfinite(xData[i])) { - throw new RuntimeException("Infinite values in xAxis Data not allowed!!!"); - } + Iterator<Number> xItr = xData.iterator(); + Iterator<Number> yItr = yData.iterator(); + while (xItr.hasNext()) { - if (Double.isInfinite(yData[i])) { - throw new RuntimeException("Infinite values in yAxis Data not allowed!!!"); - } + double x = xItr.next().doubleValue(); + double y = yItr.next().doubleValue(); - if (!Double.isNaN(xData[i]) && !Double.isNaN(yData[i])) { + if (!Double.isNaN(x) && !Double.isNaN(y)) { - int xTransform = (int) (xLeftMargin + ((xData[i] - xMin) / (xMax - xMin) * xTickSpace)); - int yTransform = (int) (bounds.getHeight() - (yTopMargin + (yData[i] - yMin) / (yMax - yMin) * yTickSpace)); + int xTransform = (int) (xLeftMargin + ((x - xMin) / (xMax - xMin) * xTickSpace)); + int yTransform = (int) (bounds.getHeight() - (yTopMargin + (y - yMin) / (yMax - yMin) * yTickSpace)); // a check if all y data are the exact same values if (Math.abs(xMax - xMin) / 5 == 0.0) { diff --git a/src/main/java/com/xeiam/xchart/series/Series.java b/src/main/java/com/xeiam/xchart/series/Series.java index 70f1229610dc680001738ccf22e1d95f340b0b36..585f156133265946be8e853071d2c554d707c1d1 100644 --- a/src/main/java/com/xeiam/xchart/series/Series.java +++ b/src/main/java/com/xeiam/xchart/series/Series.java @@ -17,10 +17,10 @@ package com.xeiam.xchart.series; import java.awt.BasicStroke; import java.awt.Color; +import java.util.Collection; import com.xeiam.xchart.series.markers.Marker; - /** * @author timmolter */ @@ -28,9 +28,9 @@ public class Series { private String name = ""; - protected double[] xData; + protected Collection<Number> xData; - protected double[] yData; + protected Collection<Number> yData; /** the minimum value of axis range */ private double xMin; @@ -63,7 +63,7 @@ public class Series { * @param xData * @param yData */ - public Series(String name, double[] xData, double[] yData) { + public Series(String name, Collection<Number> xData, Collection<Number> yData) { this.name = name; this.xData = xData; @@ -90,36 +90,57 @@ public class Series { } - private double[] findMinMax(double[] data) { + /** + * Finds the min and max of a dataset + * + * @param data + * @return + */ + private double[] findMinMax(Collection<Number> data) { Double min = null; Double max = null; - for (int i = 0; i < data.length; i++) { - if (min == null || data[i] < min) { - if (!Double.isNaN(data[i])) { - min = data[i]; + for (Number number : data) { + verify(number.doubleValue()); + if (min == null || number.doubleValue() < min) { + if (!Double.isNaN(number.doubleValue())) { + min = number.doubleValue(); } } - if (max == null || data[i] > max) { - if (!Double.isNaN(data[i])) { - max = data[i]; + if (max == null || number.doubleValue() > max) { + if (!Double.isNaN(number.doubleValue())) { + max = number.doubleValue(); } } } return new double[] { min, max }; } + /** + * Checks for invalid values in data array + * + * @param data + */ + private void verify(double value) { + + if (value == Double.POSITIVE_INFINITY) { + throw new RuntimeException("Axis data cannot contain Double.POSITIVE_INFINITY!!!"); + } else if (value == Double.NEGATIVE_INFINITY) { + throw new RuntimeException("Axis data cannot contain Double.NEGATIVE_INFINITY!!!"); + } + } + public String getName() { return name; } - public double[] getxData() { + public Collection<Number> getxData() { return xData; } - public double[] getyData() { + public Collection<Number> getyData() { return yData; } diff --git a/src/test/java/com/xeiam/xchart/example/ChartServletExample.java b/src/test/java/com/xeiam/xchart/example/ChartServletExample.java new file mode 100644 index 0000000000000000000000000000000000000000..2efb359ee8e4e4b7904a6d3c83a8313fddb9570e --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/ChartServletExample.java @@ -0,0 +1,78 @@ +/** + * Copyright 2011-2012 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.example; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.xeiam.xchart.BitmapEncoder; +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.QuickChart; + +/** + * Generates, stores, and serves charts + * + * @author timmolter + */ +@javax.servlet.annotation.WebServlet(name = "ChartServlet", urlPatterns = { "/chart" }, asyncSupported = true) +public class ChartServletExample extends HttpServlet { + + private static Map<String, Chart> chartMap = new HashMap<String, Chart>(); + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { + + String id = request.getParameter("id"); + + Chart chart = chartMap.get(id); + + response.setContentType("image/png"); + ServletOutputStream out = response.getOutputStream(); + + try { + BitmapEncoder.streamPNG(out, chart); + } catch (Exception e) { + e.printStackTrace(); + } + + out.close(); + + chart = null; + + chartMap.remove(id); + } + + /** + * Generates a chart and puts it in chartMap + * + * @return UUID uniquely identifying chart in the chartMap + */ + public static String generateDummyChart() { + + Chart chart = QuickChart.getChart("Sample Chart", "X", "Y", null, null, new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + String uuid = UUID.randomUUID().toString(); + chartMap.put(uuid, chart); + + return uuid; + } +} diff --git a/src/test/java/com/xeiam/xchart/example/Example1.java b/src/test/java/com/xeiam/xchart/example/Example1.java new file mode 100644 index 0000000000000000000000000000000000000000..7eff23811285383b79a59f54c796d2c1e5fe4e1c --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example1.java @@ -0,0 +1,46 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.BitmapEncoder; +import com.xeiam.xchart.Chart; + +/** + * Creates a simple charts and saves it as aPNG image file. + * + * @author timmolter + */ +public class Example1 { + + public static void main(String[] args) { + + double[] xData = { 0.0, 1.0, 2.0 }; + double[] yData = { 0.0, 1.0, 2.0 }; + + // Create Chart + Chart chart = new Chart(500, 400); + chart.setChartTitle("Sample Chart"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + chart.addSeries("y(x)", xData, yData); + + try { + BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/com/xeiam/xchart/example/Example2.java b/src/test/java/com/xeiam/xchart/example/Example2.java new file mode 100644 index 0000000000000000000000000000000000000000..02a1f1155ba3a321586dd5fc2f8ac7143e7d5222 --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example2.java @@ -0,0 +1,62 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.series.Series; +import com.xeiam.xchart.series.SeriesColor; +import com.xeiam.xchart.series.SeriesLineStyle; +import com.xeiam.xchart.series.SeriesMarker; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Embed a Chart in a simple Swing application + * + * @author timmolter + */ +public class Example2 { + + public static void main(String[] args) { + + // generates sine data + int size = 30; + double[] xData1 = new double[size + 1]; + double[] yData1 = new double[size + 1]; + for (int i = 0; i <= size; i++) { + double radians = (Math.PI / (size / 2) * i); + xData1[i] = i - size / 2; + yData1[i] = size * Math.sin(radians); + } + + // Create Chart + Chart chart = new Chart(440, 300); + + // Customize Chart + chart.setChartTitleVisible(false); + chart.setChartLegendVisible(false); + chart.setAxisTitlesVisible(false); + + // Series 1 + Series series1 = chart.addSeries("y=sin(x)", xData1, yData1); + series1.setLineColor(SeriesColor.PURPLE); + series1.setLineStyle(SeriesLineStyle.DASH_DASH); + series1.setMarkerColor(SeriesColor.GREEN); + series1.setMarker(SeriesMarker.SQUARE); + + new SwingWrapper(chart).displayChart(); + } + +} diff --git a/src/test/java/com/xeiam/xchart/example/Example3.java b/src/test/java/com/xeiam/xchart/example/Example3.java new file mode 100644 index 0000000000000000000000000000000000000000..bd5bf769c1e1ce47a5d0f89fd95f49bf46c35f18 --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example3.java @@ -0,0 +1,57 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Create multiple curves on one chart + * + * @author timmolter + */ +public class Example3 { + + public static void main(String[] args) { + + // Create Chart + Chart chart = new Chart(700, 500); + + for (int i = 1; i <= 14; i++) { + + // generates linear data + int b = 20; + double[] xData = new double[b + 1]; + double[] yData = new double[b + 1]; + for (int x = 0; x <= b; x++) { + xData[x] = 2 * x - b; + yData[x] = 2 * i * x - i * b; + } + + // Customize Chart + chart.setChartTitle("Sample Chart"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + + String seriesName = "y=" + 2 * i + "x-" + i * b + "b"; + chart.addSeries(seriesName, xData, yData); + + } + + new SwingWrapper(chart).displayChart(); + } + +} diff --git a/src/test/java/com/xeiam/xchart/example/Example4.java b/src/test/java/com/xeiam/xchart/example/Example4.java new file mode 100644 index 0000000000000000000000000000000000000000..56fbeee651bb7381c99dea1c105641b42581e1af --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example4.java @@ -0,0 +1,60 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.QuickChart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Create a chart matrix + * + * @author timmolter + */ +public class Example4 { + + public static void main(String[] args) { + + int numRows = 2; + int numCols = 2; + Chart[] charts = new Chart[numRows * numCols]; + + int chartCount = 0; + for (int i = 0; i < numRows; i++) { + for (int j = 0; j < numCols; j++) { + + charts[chartCount++] = QuickChart.getChart(i + "," + j, "X", "Y", null, null, getRandomWalk(1000)); + } + } + new SwingWrapper(charts, numRows, numCols).displayChartMatrix(); + } + + /** + * Generates a set of random walk data + * + * @param numPoints + * @return + */ + private static double[] getRandomWalk(int numPoints) { + + double[] y = new double[numPoints]; + y[0] = 0; + for (int i = 1; i < y.length; i++) { + y[i] = y[i - 1] + Math.random() - .5; + } + return y; + } +} diff --git a/src/test/java/com/xeiam/xchart/example/Example5.java b/src/test/java/com/xeiam/xchart/example/Example5.java new file mode 100644 index 0000000000000000000000000000000000000000..bb765cd95e6df305e95965317f59af983b806d25 --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example5.java @@ -0,0 +1,44 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Plot vertical and horizontal lines + * + * @author timmolter + */ +public class Example5 { + + public static void main(String[] args) { + + // Create Chart + Chart chart = new Chart(700, 500); + + // Customize Chart + chart.setChartTitle("Sample Chart"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + + chart.addSeries("vertical", new double[] { 1, 1 }, new double[] { -10, 10 }); + chart.addSeries("horizontal", new double[] { -10, 10 }, new double[] { 0, 0 }); + + new SwingWrapper(chart).displayChart(); + } + +} diff --git a/src/test/java/com/xeiam/xchart/example/Example6.java b/src/test/java/com/xeiam/xchart/example/Example6.java new file mode 100644 index 0000000000000000000000000000000000000000..a283dc6491a2159d8d59ee318a24d91889540292 --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example6.java @@ -0,0 +1,43 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Create chart with single point + * + * @author timmolter + */ +public class Example6 { + + public static void main(String[] args) { + + // Create Chart + Chart chart = new Chart(700, 500); + + // Customize Chart + chart.setChartTitle("Sample Chart"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + + chart.addSeries("single point (1,1)", new double[] { 1 }, new double[] { 1 }); + + new SwingWrapper(chart).displayChart(); + } + +} diff --git a/src/test/java/com/xeiam/xchart/example/Example7.java b/src/test/java/com/xeiam/xchart/example/Example7.java new file mode 100644 index 0000000000000000000000000000000000000000..8a4949e629070eaca1134e77d05d05ab2f3c796d --- /dev/null +++ b/src/test/java/com/xeiam/xchart/example/Example7.java @@ -0,0 +1,43 @@ +/** + * Copyright 2011-2012 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.example; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.swing.SwingWrapper; + +/** + * Create chart with NaN values + * + * @author timmolter + */ +public class Example7 { + + public static void main(String[] args) { + + // Create Chart + Chart chart = new Chart(700, 500); + + // Customize Chart + chart.setChartTitle("Sample Chart"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + + chart.addSeries("NaN Value at (3,2)", null, new double[] { 0, 1, Double.NaN, 3, 4 }); + + new SwingWrapper(chart).displayChart(); + } + +} diff --git a/src/test/java/com/xeiam/xchart/unit/TestNumberCollection.java b/src/test/java/com/xeiam/xchart/unit/TestNumberCollection.java new file mode 100644 index 0000000000000000000000000000000000000000..eaf5b86cc17b6c32fc818159c20fe76297160ded --- /dev/null +++ b/src/test/java/com/xeiam/xchart/unit/TestNumberCollection.java @@ -0,0 +1,46 @@ +package com.xeiam.xchart.unit; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; + +/** + * @author timmolter + * @create Aug 14, 2012 + */ +public class TestNumberCollection { + + @Test + public void testNumber2double() { + + Number myInteger = new Integer(688); + assertThat(myInteger.doubleValue(), is(equalTo(new Double(688).doubleValue()))); + + Number myLong = new Long(976320011); + assertThat(myLong.doubleValue(), is(equalTo(new Long(976320011).doubleValue()))); + + Number myFloat = new Float(95.4765217889); + assertThat(myFloat.doubleValue(), is(equalTo(new Float(95.4765217889).doubleValue()))); + + Number myDouble = new Double(33.2); + assertThat(myDouble.doubleValue(), is(equalTo(33.2))); + + } + + @Test + public void testDoubleInfinity() { + + Number myDouble = new Double(33.2); + assertThat(myDouble.doubleValue(), is(not(equalTo(Double.POSITIVE_INFINITY)))); + + myDouble = new Double(Double.POSITIVE_INFINITY); + assertThat(myDouble.doubleValue(), is(equalTo(Double.POSITIVE_INFINITY))); + + Number myInteger = new Integer(688); + assertThat(myInteger.doubleValue(), is(not(equalTo(Double.POSITIVE_INFINITY)))); + + } +}