blob: 8101eb002fe29039c3ad3801f0b8653556491b55 [file] [log] [blame]
Jian Lib6859e12016-03-01 09:29:24 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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
17package org.onosproject.ui.chart;
18
19import com.google.common.collect.Maps;
20
21import java.util.Arrays;
22import java.util.Map;
23
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * A simple model of chart data.
29 *
30 * <p>
31 * Note that this is not a full MVC type model; the expected usage pattern
32 * is to create an empty chart, add data points (by consulting the business model),
33 * and produce the list of data points which contain a label and a set of data
34 * values for all serials.
35 */
36public class ChartModel {
37
38 // key is series name, value is series index
39 private final Map<String, Integer> seriesMap;
40 private final DataPoint[] dataPoints;
41
42 /**
43 * Constructs a chart model with initialized series set.
44 *
45 * @param series a set of series
46 */
47 public ChartModel(int size, String... series) {
48 checkNotNull(series, "series cannot be null");
49 checkArgument(series.length > 0, "must be at least one series");
50 seriesMap = Maps.newConcurrentMap();
51
52 for (int index = 0; index < series.length; index++) {
53 seriesMap.put(series[index], index);
54 }
55
56 checkArgument(size > 0, "must have at least one data point");
57 dataPoints = new DataPoint[size];
58 }
59
60 private void checkDataPoint(DataPoint dataPoint) {
61 checkArgument(dataPoint.getSize() == seriesCount(),
62 "data size should be equal to number of series");
63 }
64
65 /**
66 * Returns the number of series in this chart model.
67 *
68 * @return number of series
69 */
70 public int seriesCount() {
71 return seriesMap.size();
72 }
73
74 /**
75 * Shifts all of the data points to the left,
76 * and adds a new data point to the tail of the array.
77 *
78 * @param label label name
79 * @param values a set of data values
80 */
81 public void addDataPoint(String label, Double[] values) {
82 DataPoint dp = new DataPoint(label, values);
83 checkDataPoint(dp);
84
85 for (int index = 1; index < dataPoints.length; index++) {
86 dataPoints[index - 1] = dataPoints[index];
87 }
88 dataPoints[dataPoints.length - 1] = dp;
89 }
90
91 /**
92 * Returns all of series.
93 *
94 * @return an array of series
95 */
96 public String[] getSeries() {
97 return seriesMap.keySet().toArray(new String[seriesMap.size()]);
98 }
99
100 /**
101 * Returns all of data points.
102 *
103 * @return an array of data points
104 */
105 public DataPoint[] getDataPoints() {
106 return Arrays.copyOf(dataPoints, dataPoints.length);
107 }
108
109 /**
110 * Returns the last element inside all of data points.
111 *
112 * @return data point
113 */
114 public DataPoint getLastDataPoint() {
115 return dataPoints[dataPoints.length - 1];
116 }
117
118 /**
119 * A class of data point.
120 */
121 public class DataPoint {
122 // values for all series
123 private final Double[] values;
124 private final String label;
125
126 /**
127 * Constructs a data point.
128 *
129 * @param label label name
130 * @param values a set of data values for all series
131 */
132 public DataPoint(String label, Double[] values) {
133 this.label = label;
134 this.values = values;
135 }
136
137 /**
138 * Returns the label name of this data point.
139 *
140 * @return label name
141 */
142 public String getLabel() {
143 return label;
144 }
145
146 /**
147 * Returns the size of data point.
148 * This should be identical to the size of series.
149 *
150 * @return size of data point
151 */
152 public int getSize() {
153 return values.length;
154 }
155
156 /**
157 * Returns the value of the data point of the given series.
158 *
159 * @param series series name
160 * @return data value of a specific series
161 */
162 public Double getValue(String series) {
163 return values[seriesMap.get(series)];
164 }
165 }
166}