Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow Point value types other then Number #15

Open
thombergs opened this issue Apr 21, 2014 · 3 comments
Open

allow Point value types other then Number #15

thombergs opened this issue Apr 21, 2014 · 3 comments

Comments

@thombergs
Copy link
Member

Reported by pepijn@dvelop.nl, Aug 27, 2013
The current implementation of Point does not allow value types other then Number. This makes it impossible to properly use the Axis type 'DATETIME'.
This patch makes the X and Y fields of Point generic, so other types can be used. It also adds a JSON serializer for java.util.Date instances.

Index: wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/jackson/JsonRenderer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/jackson/JsonRenderer.java   (revision 292)
+++ wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/jackson/JsonRenderer.java   (revision )
@@ -14,12 +14,14 @@
  */
 package com.googlecode.wickedcharts.highcharts.jackson;

+import java.util.Date;
 import java.util.Locale;

 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.databind.JsonSerializer;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.googlecode.wickedcharts.highcharts.json.LowercaseEnum;
 import com.googlecode.wickedcharts.highcharts.options.Center;
 import com.googlecode.wickedcharts.highcharts.options.Crosshair;
@@ -97,6 +99,7 @@
        this.jacksonModule.addSerializer(Crosshair.class, new CrosshairSerializer());
        this.jacksonModule.addSerializer(RangeCoordinate.class, new RangeCoordinateSerializer());
        this.jacksonModule.addSerializer(Bubble.class, new BubbleSerializer());
+        this.jacksonModule.addSerializer(Date.class, new DateSerializer());

        ObjectMapper mapper = createDefaultObjectMapper();
        mapper.setLocale(Locale.ENGLISH);
Index: wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/jackson/DateSerializer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/jackson/DateSerializer.java (revision )
+++ wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/jackson/DateSerializer.java (revision )
@@ -0,0 +1,45 @@
+/**
+ *   Copyright 2012-2013 Wicked Charts (http://wicked-charts.googlecode.com)
+ *
+ *   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.googlecode.wickedcharts.highcharts.jackson;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+
+/**
+ * JsonSerializer for {@link Date} objects.
+ * @author pdegeus
+ */
+public class DateSerializer extends JsonSerializer<Date> {
+
+   @Override
+   public void serialize(final Date value, final JsonGenerator jgen, final SerializerProvider provider)
+       throws IOException, JsonProcessingException {
+
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(value);
+
+       jgen.writeRawValue(String.format(
+            "new Date(%d,%d,%d,%d,%d,%d,0)",
+            cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
+            cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)
+        ));
+   }
+}
Index: wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/options/series/Point.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/options/series/Point.java   (revision 292)
+++ wicked-charts/wicked-charts-highcharts/src/main/java/com/googlecode/wickedcharts/highcharts/options/series/Point.java   (revision )
@@ -14,8 +14,9 @@
  */
 package com.googlecode.wickedcharts.highcharts.options.series;

-import java.awt.Color;
+import java.awt.*;
 import java.io.Serializable;
+import java.util.Date;

 import com.googlecode.wickedcharts.highcharts.options.DataLabels;
 import com.googlecode.wickedcharts.highcharts.options.Events;
@@ -24,14 +25,16 @@
 import com.googlecode.wickedcharts.highcharts.options.color.SimpleColor;

 /**
- * A point in a chart that has a label (or a name) and a color.
- * 
- * @see <a
- *      href="http://api.highcharts.com/highcharts#series.data">http://api.highcharts.com/highcharts#series.data</a>
+ * A point in a chart that has a label (or a name) and a color. Uses generics for the type of the X and Y values.
+ * Supported types are: all {@link Number Numbers}, {@link String} and {@link Date}. Use {@link Void} if a field is
+ * not used.
+ * @param <X> Type of the value on the X-axis.
+ * @param <Y> Type of the value on the Y-axis.
+ * @see <a href="http://api.highcharts.com/highcharts#series.data">http://api.highcharts.com/highcharts#series.data</a>
  * @author Tom Hombergs (tom.hombergs@gmail.com)
- * 
+ * @author pdegeus
  */
-public class Point implements Serializable {
+public class Point<X, Y> implements Serializable {

    private static final long serialVersionUID = 1L;

@@ -39,7 +42,7 @@

    private ColorReference color;

-   private Number y;
+   private Y y;

    private Events events;

@@ -55,7 +58,7 @@

    private Boolean selected;

-   private Number x;
+   private X x;

    private Integer wickedChartsId;

@@ -69,30 +72,34 @@
     * @param y
     *          the y value of this point.
     */
-   public Point(final Number y) {
-       this.y = y;
+   public Point(final Y y) {
+       this(null, null, y);
    }

-   public Point(final String name, final Number y) {
-       this.name = name;
-       this.y = y;
+   public Point(final String name, final Y y) {
+        this(name, null, y);
    }

-   public Point(final Number x, final Number y) {
+   public Point(final X x, final Y y) {
+        this(null, x, y);
+   }
+
+    public Point(final String name, final X x, final Y y) {
+        this.name = name;
-       this.x = x;
-       this.y = y;
-   }
+        this.x = x;
+        this.y = y;
+    }

    /**
     * Convencience constructor.
     */
-   public Point(final String name, final Number y, final Color color) {
+   public Point(final String name, final Y y, final Color color) {
        this.name = name;
        this.y = y;
        this.color = new SimpleColor(color);
    }

-   public Point(final String name, final Number y, final ColorReference color) {
+   public Point(final String name, final Y y, final ColorReference color) {
        this.name = name;
        this.y = y;
        this.color = color;
@@ -130,11 +137,11 @@
        return this.sliced;
    }

-   public Number getX() {
+   public X getX() {
        return this.x;
    }

-   public Number getY() {
+   public Y getY() {
        return this.y;
    }

@@ -183,12 +190,12 @@
        return this;
    }

-   public Point setX(final Number x) {
+   public Point setX(final X x) {
        this.x = x;
        return this;
    }

-   public Point setY(final Number y) {
+   public Point setY(final Y y) {
        this.y = y;
        return this;
    }
@thombergs thombergs added this to the 1.6.0 milestone Apr 21, 2014
@thombergs
Copy link
Member Author

#3 bartlomiej.lopatka
i'm using DATETIME type Axis in my project, and it works just fine with Points in PointSeries.

To add Date value in x Axis i'm using such code:
Date someDate = new Date();
Point point = new Point(someDate.getTime(), 100);

With Date.getTime() you'll get timestamp and your chart will be rendered correct (for example with gaps on days that are not added to series).

@thombergs
Copy link
Member Author

#4 pepijn@dvelop.nl
You're right, that seems to work too :) Thanks for the tip.
It still think however this change would make life easier in some cases, allowing all types of values for X and Y and adding a constructor to provide the X value.

@thombergs
Copy link
Member Author

Thanks for the patch! I will have a look at it for the next release. Since it would break the API, I would probably create a new class "GenericPoint<X,Y>" instead of adding the generic types to the existing Point class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants