001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.console.freemarker;
018
019import java.util.*;
020
021import org.apache.juneau.marshall.collections.*;
022
023/**
024 * The built-in {@code annotations} decorator: ECharts {@code markPoint} presets (max, min, average) on every series,
025 * or explicit points on a carrier series {@code juneau-annotations-<n>}.
026 *
027 * <h5 class='section'>Example:</h5>
028 * <p class='bjava'>
029 *    Annotations.<jsm>max</jsm>().withMin().label(<js>"{name}"</js>);
030 * </p>
031 *
032 * @since 10.0.0
033 */
034public final class Annotations implements Decorator {
035
036   private final Set<String> presets = new LinkedHashSet<>();
037   private List<Map<String,Object>> points;
038   private String label;
039
040   private Annotations() {}
041
042   /** @return A new decorator marking each series' maximum. */
043   public static Annotations max() { return new Annotations().withMax(); }
044
045   /** @return A new decorator marking each series' minimum. */
046   public static Annotations min() { return new Annotations().withMin(); }
047
048   /** @return A new decorator marking each series' average. */
049   public static Annotations average() { return new Annotations().withAverage(); }
050
051   /** @param rows Explicit points {@code {x, y, label}}. @return A new decorator. */
052   public static Annotations points(List<? extends Map<String,?>> rows) {
053      var x = new Annotations();
054      x.points = Markers.copyRows(rows);
055      return x;
056   }
057
058   /** @return This object, also marking the maximum. */
059   public Annotations withMax() { presets.add("max"); return this; }
060
061   /** @return This object, also marking the minimum. */
062   public Annotations withMin() { presets.add("min"); return this; }
063
064   /** @return This object, also marking the average. */
065   public Annotations withAverage() { presets.add("average"); return this; }
066
067   /** @param v A label template ({@code template} fn tokens). @return This object. */
068   public Annotations label(String v) { label = v; return this; }
069
070   @Override
071   public JsonMap toJson() {
072      var m = JsonMap.of("use", "annotations");
073      if (! presets.isEmpty()) m.put("presets", List.copyOf(presets));
074      if (points != null) m.put("values", List.copyOf(points));
075      if (label != null) m.put("label", label);
076      return m;
077   }
078}