blob: 7ba0bddd2d16783996c825e8e76062567bd317b4 [file] [log] [blame]
Jian Li15468822016-04-15 16:28:11 -07001/*
2 * Copyright 2016-present 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 */
16package org.onosproject.ui.chart;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22
23/**
24 * Provides static utility methods for dealing with charts.
25 */
26public 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 Li1077dd72016-04-26 11:10:20 -070049 * 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 Li15468822016-04-15 16:28:11 -070063 * 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();
72 Double[] values = dp.getAll();
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}