From 4b70ad19213e88a35cf68c3547f18dc3bb6b27e4 Mon Sep 17 00:00:00 2001
From: Tim Molter <tim.molter@gmail.com>
Date: Sat, 18 Jun 2011 10:42:26 +0200
Subject: [PATCH] fixed horizontal and vertical plotting bug
---
src/com/xeiam/xcharts/AxisTick.java | 12 ++-
src/com/xeiam/xcharts/AxisTickLabels.java | 2 +-
src/com/xeiam/xcharts/PlotContent.java | 10 +++
src/com/xeiam/xcharts/example/SwingChart.java | 22 ++---
.../xeiam/xcharts/example/SwingChart3.java | 85 +++++++++++++++++++
5 files changed, 118 insertions(+), 13 deletions(-)
create mode 100644 src/com/xeiam/xcharts/example/SwingChart3.java
diff --git a/src/com/xeiam/xcharts/AxisTick.java b/src/com/xeiam/xcharts/AxisTick.java
index 613468f8..61c8c502 100644
--- a/src/com/xeiam/xcharts/AxisTick.java
+++ b/src/com/xeiam/xcharts/AxisTick.java
@@ -149,6 +149,12 @@ public class AxisTick implements IChartPart {
// System.out.println("b= " + b);
tickLabels.add(format(b.doubleValue()));
int tickLabelPosition = (int) (leftMargin + ((b.doubleValue() - axis.getMin()) / (axis.getMax() - axis.getMin()) * tickSpace));
+
+ // a check if all axis data are the exact same values
+ if (Math.abs(axis.getMax() - axis.getMin()) / 5 == 0.0) {
+ tickLabelPosition = (int) (leftMargin + tickSpace / 2.0);
+ }
+
tickLocations.add(tickLabelPosition);
}
}
@@ -162,7 +168,11 @@ public class AxisTick implements IChartPart {
// e.g. 724.1 --> 7.241 * 10 ** 2
double mantissa = gridStepHint;
int exponent = 0;
- if (mantissa < 1) {
+ if (mantissa == 0) {
+ // mantissa = 0.0;
+ exponent = 1;
+
+ } else if (mantissa < 1) {
while (mantissa < 1) {
mantissa *= 10.0;
exponent--;
diff --git a/src/com/xeiam/xcharts/AxisTickLabels.java b/src/com/xeiam/xcharts/AxisTickLabels.java
index 5bb63620..c502526c 100644
--- a/src/com/xeiam/xcharts/AxisTickLabels.java
+++ b/src/com/xeiam/xcharts/AxisTickLabels.java
@@ -60,7 +60,7 @@ public class AxisTickLabels implements IChartPart {
g.setColor(foreground);
- if (axis.getDirection() == Axis.Direction.Y) {
+ if (axis.getDirection() == Axis.Direction.Y) { // Y-Axis
int xOffset = (int) (axis.getAxisTitle().getBounds().getX() + axis.getAxisTitle().getBounds().getWidth());
int yOffset = (int) (axis.getPaintZone().getY());
diff --git a/src/com/xeiam/xcharts/PlotContent.java b/src/com/xeiam/xcharts/PlotContent.java
index 47c15ed4..e4c96288 100644
--- a/src/com/xeiam/xcharts/PlotContent.java
+++ b/src/com/xeiam/xcharts/PlotContent.java
@@ -75,6 +75,16 @@ public class PlotContent implements IChartPart {
int xTransform = (int) (xLeftMargin + ((xData[i] - xMin) / (xMax - xMin) * xTickSpace));
int yTransform = (int) (bounds.getHeight() - (yLeftMargin + (yData[i] - yMin) / (yMax - yMin) * yTickSpace));
+ // a check if all y data are the exact same values
+ if (Math.abs(xMax - xMin) / 5 == 0.0) {
+ xTransform = (int) (bounds.getWidth() / 2.0);
+ }
+
+ // a check if all y data are the exact same values
+ if (Math.abs(yMax - yMin) / 5 == 0.0) {
+ yTransform = (int) (bounds.getHeight() / 2.0);
+ }
+
int xOffset = (int) (bounds.getX() + xTransform - 1);
int yOffset = (int) (bounds.getY() + yTransform);
diff --git a/src/com/xeiam/xcharts/example/SwingChart.java b/src/com/xeiam/xcharts/example/SwingChart.java
index 7770c044..d6a7487d 100644
--- a/src/com/xeiam/xcharts/example/SwingChart.java
+++ b/src/com/xeiam/xcharts/example/SwingChart.java
@@ -32,18 +32,18 @@ public class SwingChart {
private static void createAndShowGUI() {
- // generates sine data
- int size = 100;
- 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);
- }
+ // // generates sine data
+ // int size = 100;
+ // 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);
+ // }
// generates linear data
- int size2 = 40;
+ int size2 = 100;
double[] xData2 = new double[size2 + 1];
double[] yData2 = new double[size2 + 1];
for (int i = 0; i <= size2; i++) {
@@ -68,7 +68,7 @@ public class SwingChart {
// chart.setAxisTitlesVisible(false);
// Series 1
- Series series1 = chart.addSeries("y=sin(x)", xData1, yData1);
+ // Series series1 = chart.addSeries("y=sin(x)", xData1, yData1);
// series1.setLineColor(SeriesColor.PURPLE);
// series1.setLineStyle(SeriesLineStyle.NONE);
// series1.setMarkerColor(SeriesColor.GREEN);
diff --git a/src/com/xeiam/xcharts/example/SwingChart3.java b/src/com/xeiam/xcharts/example/SwingChart3.java
new file mode 100644
index 00000000..c0e74bd2
--- /dev/null
+++ b/src/com/xeiam/xcharts/example/SwingChart3.java
@@ -0,0 +1,85 @@
+/**
+ * Copyright 2011 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.xcharts.example;
+
+import java.awt.BorderLayout;
+
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
+import com.xeiam.xcharts.Chart;
+import com.xeiam.xcharts.JChartPanel;
+import com.xeiam.xcharts.series.Series;
+
+/**
+ * Demonstrated/Tests plotting horizontal and vertical lines
+ *
+ * @author timmolter
+ */
+public class SwingChart3 {
+
+ private static void createAndShowGUI() {
+
+ // generates linear data
+ double[] xData1 = new double[] { 0.0, 1.0, 2.0 };
+ double[] yData1 = new double[] { 0.0, 0.0, 0.0 };
+ double[] xData2 = new double[] { 0.0, 0.0, 0.0 };
+ double[] yData2 = new double[] { 0.0, 1.0, 2.0 };
+
+ // Create and set up the window.
+ JFrame frame = new JFrame("XChart");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setUndecorated(true);
+
+ // Create Chart
+ Chart chart = new Chart(800, 600);
+
+ // Customize Chart
+ chart.setChartTitle("Sample Chart");
+ chart.setXAxisTitle("X");
+ chart.setYAxisTitle("Y");
+ // chart.setChartTitleVisible(false);
+ // chart.setChartLegendVisible(false);
+ // chart.setAxisTitlesVisible(false);
+
+ // Series
+ Series series1 = chart.addSeries("y=0", xData1, yData1);
+ Series series2 = chart.addSeries("x=0", xData2, yData2);
+
+ // Swing
+ JPanel chartPanel = new JChartPanel(chart);
+
+ // add the panel to the content pane
+ frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
+
+ // Display the window
+ frame.pack();
+ frame.setLocationRelativeTo(null); // centers on screen
+ frame.setVisible(true);
+ }
+
+ public static void main(String[] args) {
+
+ // Schedule a job for the event-dispatching thread:
+ // creating and showing this application's GUI.
+ javax.swing.SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ createAndShowGUI();
+ }
+ });
+ }
+}
--
GitLab