PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/googlecode/charts4j/XYLineChart.java

http://charts4j.googlecode.com/
Java | 159 lines | 71 code | 17 blank | 71 comment | 11 complexity | bffe8269cf56a7b6ecbc96b158f8f277 MD5 | raw file
  1/**
  2 *
  3 * The MIT License
  4 *
  5 * Copyright (c) 2011 the original author or authors.
  6 *
  7 * Permission is hereby granted, free of charge, to any person obtaining a copy
  8 * of this software and associated documentation files (the "Software"), to deal
  9 * in the Software without restriction, including without limitation the rights
 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11 * copies of the Software, and to permit persons to whom the Software is
 12 * furnished to do so, subject to the following conditions:
 13
 14 * The above copyright notice and this permission notice shall be included in
 15 * all copies or substantial portions of the Software.
 16
 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23 * THE SOFTWARE.
 24 */
 25
 26package com.googlecode.charts4j;
 27
 28import java.util.Set;
 29
 30import com.googlecode.charts4j.collect.ImmutableList;
 31import com.googlecode.charts4j.collect.Lists;
 32import com.googlecode.charts4j.collect.Sets;
 33import com.googlecode.charts4j.parameters.ChartType;
 34
 35/**
 36 * XY Line chart constructed with the {@link GCharts} static factory class.
 37 *
 38 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 39 *
 40 * @see XYLine
 41 * @see GCharts
 42 */
 43public class XYLineChart extends AbstractLineChart {
 44
 45    /** List of lines to be ploted this chart. **/
 46    private final ImmutableList<Plot> xylines;
 47
 48    /**
 49     * Create a XY line chart.
 50     *
 51     * @param xylines
 52     *            xy lines to be rendered by this chart
 53     * @see XYLine
 54     */
 55    XYLineChart(final ImmutableList<? extends Plot> xylines) {
 56        super(xylines);
 57        this.xylines = Lists.copyOf(xylines);
 58    }
 59
 60    /**
 61     * {@inheritDoc}
 62     */
 63    @Override
 64    protected void prepareData() {
 65        super.prepareData();
 66        for (Plot xyl : xylines) {
 67            final PlotImpl xyline = (PlotImpl) xyl;
 68            final PlotImpl xy = removeDuplicatePoints(xyline);
 69            parameterManager.addData(xy.getXData());
 70            parameterManager.addData(xy.getYData());
 71        }
 72        parameterManager.setChartTypeParameter(ChartType.XY_LINE_CHART);
 73    }
 74
 75    /**
 76     * Method that purges duplicate points.
 77     *
 78     * @param xyline
 79     *            plot that will be purged of duplicate lines.
 80     * @return plot purged of duplicates
 81     */
 82    private PlotImpl removeDuplicatePoints(final PlotImpl xyline) {
 83        final double[] xData = xyline.getXData().getData();
 84        final double[] yData = xyline.getYData().getData();
 85
 86        if (xData.length != yData.length) {
 87            return xyline;
 88        }
 89
 90        // Removing duplicates. Can assume xData and yData are the same length.
 91        final Set<XYPoint> set = Sets.newLinkedHashSet();
 92        for (int i = 0; i < xData.length; i++) {
 93            set.add(new XYPoint(xData[i], yData[i]));
 94        }
 95
 96        final double[] x = new double[set.size()];
 97        final double[] y = new double[set.size()];
 98
 99        int i = 0;
100        for (XYPoint point : set) {
101            x[i] = point.x;
102            y[i] = point.y;
103            i++;
104        }
105        return new PlotImpl(Data.newData(x), Data.newData(y));
106    }
107
108    /**
109     * XY point.
110     *
111     * @author Julien Chastang (julien.c.chastang at gmail dot com)
112     */
113    private static final class XYPoint {
114        /** X.  **/
115        private final double x;
116
117        /** Y.  **/
118        private final double y;
119
120        /**
121         * Construct an XY point.
122         *
123         * @param x x
124         * @param y y
125         */
126        private XYPoint(final double x, final double y) {
127            super();
128            this.x = x;
129            this.y = y;
130        }
131
132        /**
133         * {@inheritDoc}
134         */
135        @Override
136        public boolean equals(final Object obj) {
137            if (this == obj) {
138                return true;
139            }
140            if (!(obj instanceof XYPoint)) {
141                return false;
142            }
143            final XYPoint objA = (XYPoint) obj;
144            return (this.x == objA.x) && (this.y == objA.y);
145        }
146
147        /**
148         *
149         * {@inheritDoc}
150         */
151        @Override
152        public int hashCode() {
153            int hash = 1;
154            hash = hash * 31 + Double.valueOf(x).hashCode();
155            hash = hash * 31 + Double.valueOf(y).hashCode();
156            return hash;
157        }
158    }
159}