Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 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 | package org.onosproject.ui.chart; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | |
| 23 | /** |
| 24 | * Provides static utility methods for dealing with charts. |
| 25 | */ |
| 26 | public final class ChartUtils { |
| 27 | |
| 28 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 29 | |
| 30 | // non-instantiable |
| 31 | private ChartUtils() { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Generates a JSON array node from the data points of the given chart model. |
| 36 | * |
| 37 | * @param cm the chart model |
| 38 | * @return the array node representation of data points |
| 39 | */ |
| 40 | public static ArrayNode generateDataPointArrayNode(ChartModel cm) { |
| 41 | ArrayNode array = MAPPER.createArrayNode(); |
| 42 | for (ChartModel.DataPoint dp : cm.getDataPoints()) { |
| 43 | array.add(toJsonNode(dp, cm)); |
| 44 | } |
| 45 | return array; |
| 46 | } |
| 47 | |
| 48 | /** |
Jian Li | 1077dd7 | 2016-04-26 11:10:20 -0700 | [diff] [blame] | 49 | * Generates a JSON object node from the annotations of the given chart model. |
| 50 | * |
| 51 | * @param cm the chart model |
| 52 | * @return the object node representation of the annotations |
| 53 | */ |
| 54 | public static ObjectNode generateAnnotObjectNode(ChartModel cm) { |
| 55 | ObjectNode node = MAPPER.createObjectNode(); |
| 56 | for (ChartModel.Annot a : cm.getAnnotations()) { |
| 57 | node.put(a.key(), a.valueAsString()); |
| 58 | } |
| 59 | return node; |
| 60 | } |
| 61 | |
| 62 | /** |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 63 | * Generate a JSON node from the data point and given chart model. |
| 64 | * |
| 65 | * @param dp the data point |
| 66 | * @param cm the chart model |
| 67 | * @return the node representation of a data point with series |
| 68 | */ |
| 69 | public static JsonNode toJsonNode(ChartModel.DataPoint dp, ChartModel cm) { |
| 70 | ObjectNode result = MAPPER.createObjectNode(); |
| 71 | String[] series = cm.getSeries(); |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 72 | String[] values = dp.getAsStrings(); |
Jian Li | 1546882 | 2016-04-15 16:28:11 -0700 | [diff] [blame] | 73 | int n = series.length; |
| 74 | for (int i = 0; i < n; i++) { |
| 75 | result.put(series[i], values[i]); |
| 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | } |