|
| 1 | +/* |
| 2 | + * Copyright 2016 AndroidPlot.com |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.androidplot.xy; |
| 18 | + |
| 19 | +import com.androidplot.xy.SimpleXYSeries; |
| 20 | + |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +/** |
| 24 | + * Convenience class for representing a series of candlestick values; |
| 25 | + * is NOT a descendant of {@link com.androidplot.xy.XYSeries} and therefore |
| 26 | + * cannot be directly added to an {@link com.androidplot.xy.XYPlot}. |
| 27 | + * |
| 28 | + * This class is NOT threadsafe. |
| 29 | + * |
| 30 | + * @since 0.9.8 |
| 31 | + */ |
| 32 | +public class CandlestickSeries { |
| 33 | + |
| 34 | + private SimpleXYSeries highSeries = new SimpleXYSeries(null); |
| 35 | + private SimpleXYSeries lowSeries = new SimpleXYSeries(null); |
| 36 | + private SimpleXYSeries openSeries = new SimpleXYSeries(null); |
| 37 | + private SimpleXYSeries closeSeries = new SimpleXYSeries(null); |
| 38 | + |
| 39 | + protected static List<Number> generateRange(int start, int end) { |
| 40 | + List<Number> range = new ArrayList<>(end - start); |
| 41 | + for(int i = start; i < end; i++) { |
| 42 | + range.add(i); |
| 43 | + } |
| 44 | + return range; |
| 45 | + } |
| 46 | + |
| 47 | + public CandlestickSeries(Item... items) { |
| 48 | + this(Arrays.asList(items)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new CandlestickSeries. |
| 53 | + * Calls {@link #CandlestickSeries(List, List)} with a list of xVals |
| 54 | + * generated using the formula x=i. |
| 55 | + * @param items |
| 56 | + */ |
| 57 | + public CandlestickSeries(List<Item> items) { |
| 58 | + this(generateRange(0, items.size()), items); |
| 59 | + } |
| 60 | + |
| 61 | + public CandlestickSeries(List<Number> xVals, List<Item> items) { |
| 62 | + if(xVals.size() != items.size()) { |
| 63 | + throw new IllegalArgumentException("xVals and yVals length must be identical."); |
| 64 | + } |
| 65 | + for(int i = 0; i < xVals.size(); i++) { |
| 66 | + Number x = xVals.get(i); |
| 67 | + highSeries.addLast(x, items.get(i).getHigh()); |
| 68 | + lowSeries.addLast(x, items.get(i).getLow()); |
| 69 | + openSeries.addLast(x, items.get(i).getOpen()); |
| 70 | + closeSeries.addLast(x, items.get(i).getClose()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public SimpleXYSeries getHighSeries() { |
| 75 | + return highSeries; |
| 76 | + } |
| 77 | + |
| 78 | + public void setHighSeries(SimpleXYSeries highSeries) { |
| 79 | + this.highSeries = highSeries; |
| 80 | + } |
| 81 | + |
| 82 | + public SimpleXYSeries getLowSeries() { |
| 83 | + return lowSeries; |
| 84 | + } |
| 85 | + |
| 86 | + public void setLowSeries(SimpleXYSeries lowSeries) { |
| 87 | + this.lowSeries = lowSeries; |
| 88 | + } |
| 89 | + |
| 90 | + public SimpleXYSeries getOpenSeries() { |
| 91 | + return openSeries; |
| 92 | + } |
| 93 | + |
| 94 | + public void setOpenSeries(SimpleXYSeries openSeries) { |
| 95 | + this.openSeries = openSeries; |
| 96 | + } |
| 97 | + |
| 98 | + public SimpleXYSeries getCloseSeries() { |
| 99 | + return closeSeries; |
| 100 | + } |
| 101 | + |
| 102 | + public void setCloseSeries(SimpleXYSeries closeSeries) { |
| 103 | + this.closeSeries = closeSeries; |
| 104 | + } |
| 105 | + |
| 106 | + public static class Item { |
| 107 | + private double low; |
| 108 | + private double high; |
| 109 | + private double open; |
| 110 | + private double close; |
| 111 | + |
| 112 | + /** |
| 113 | + * An individual candlestick value. Since it is illegal to include |
| 114 | + * null values for any member of a candlestick, this class is modeled with |
| 115 | + * double values instead of {@link Number} instances. |
| 116 | + * @param low |
| 117 | + * @param high |
| 118 | + * @param open |
| 119 | + * @param close |
| 120 | + */ |
| 121 | + public Item(double low, double high, double open, double close) { |
| 122 | + this.low = low; |
| 123 | + this.high = high; |
| 124 | + this.open = open; |
| 125 | + this.close = close; |
| 126 | + } |
| 127 | + |
| 128 | + public double getLow() { |
| 129 | + return low; |
| 130 | + } |
| 131 | + |
| 132 | + public void setLow(double low) { |
| 133 | + this.low = low; |
| 134 | + } |
| 135 | + |
| 136 | + public double getHigh() { |
| 137 | + return high; |
| 138 | + } |
| 139 | + |
| 140 | + public void setHigh(double high) { |
| 141 | + this.high = high; |
| 142 | + } |
| 143 | + |
| 144 | + public double getOpen() { |
| 145 | + return open; |
| 146 | + } |
| 147 | + |
| 148 | + public void setOpen(double open) { |
| 149 | + this.open = open; |
| 150 | + } |
| 151 | + |
| 152 | + public double getClose() { |
| 153 | + return close; |
| 154 | + } |
| 155 | + |
| 156 | + public void setClose(double close) { |
| 157 | + this.close = close; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments