Skip to content

Commit

Permalink
fix tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
rudiejd committed Jul 23, 2021
1 parent a715069 commit ab8afac
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 12 deletions.
30 changes: 23 additions & 7 deletions src/main/java/org/vaadin/addons/chartjs/ChartJs.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.concurrent.atomic.AtomicInteger;

import org.vaadin.addons.chartjs.config.ChartConfig;
import org.vaadin.addons.chartjs.options.TooltipsCallbacks;
import org.vaadin.addons.chartjs.utils.JUtils;
import org.vaadin.addons.chartjs.ChartJsUtils;


Expand Down Expand Up @@ -63,7 +65,19 @@ public ChartJs() {
*/
public ChartJs(ChartConfig chartConfig) {
this();
configure(chartConfig);
this.chartConfig = chartConfig;
}

public void addFunction(String domPath, String functionStr) {
ChartJsUtils.safelyExecuteJs(getUI().orElse(UI.getCurrent()),
"document.getElementById('"+getChartId()+"').chartjs."+domPath+" = "+functionStr);
}

public void addAllFunctions() {
// tooltip callback functions
for (Map.Entry<String, String> entry : chartConfig.getOptions().tooltips().callbacks().asMap().entrySet()) {
addFunction("config.options.tooltips.callbacks."+entry.getKey(), JUtils.formatJavascriptFunction(entry.getKey(), entry.getValue(), TooltipsCallbacks.argumentMap().get(entry.getKey())));
}
}

/**
Expand All @@ -72,9 +86,10 @@ public ChartJs(ChartConfig chartConfig) {
*/
public void configure(ChartConfig chartConfig) {
this.chartConfig = chartConfig;
if (connected) {
ChartJsUtils.safelyExecuteJs(getUI().orElse(null),
"document.getElementById($0).chartjs.config = $1", getChartId(), chartConfig.buildJson());
if (connected) {
ChartJsUtils.safelyExecuteJs(getUI().orElse(UI.getCurrent()),
"document.getElementById($0).chartjs.config = $1", getChartId(), chartConfig.buildJson());
addAllFunctions();
}
}

Expand All @@ -83,9 +98,10 @@ public void configure(ChartConfig chartConfig) {
protected void onAttach(AttachEvent e) {
super.onAttach(e);
if (!connected) {
ChartJsUtils.safelyExecuteJs(getUI().orElse(null),
ChartJsUtils.safelyExecuteJs(getUI().orElse(UI.getCurrent()),
"document.getElementById($0).chartjs = new Chart(document.getElementById($1).getContext('2d'), $2)" ,
getChartId(), getChartCanvasId(), chartConfig.buildJson());
addAllFunctions();
}
connected = true;
}
Expand All @@ -109,15 +125,15 @@ public ChartConfig getConfig() {
public void update() {
configure(chartConfig);
if (connected) {
ChartJsUtils.safelyExecuteJs(getUI().orElse(null), "document.getElementById($0).chartjs.update()", getChartId());
ChartJsUtils.safelyExecuteJs(getUI().orElse(UI.getCurrent()), "document.getElementById($0).chartjs.update()", getChartId());
}
}

/**
* Destroy the chart. This will call chartjs.destroy();
*/
public void destroy() {
ChartJsUtils.safelyExecuteJs(getUI().orElse(null), "document.getElementById($0).chartjs.destroy()", getChartId());
ChartJsUtils.safelyExecuteJs(getUI().orElse(UI.getCurrent()), "document.getElementById($0).chartjs.destroy()", getChartId());
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/vaadin/addons/chartjs/ChartJsUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs;

import java.io.Serializable;
import java.util.Map;

import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.page.PendingJavaScriptResult;
Expand All @@ -23,5 +24,19 @@ public static void securelyAccessUI(UI ui, Command command) {
ui.access(command);
}
}

/**
* Insert into map if value is not null
* @param <T> typeof key
* @param <S> typeof value
* @param map to insert into
* @param key
* @param value
*/
public static <T, S> void putNotNull(Map<T, S> map, T key, S value) {
if (value != null) {
map.put(key, value);
}
}

}
10 changes: 9 additions & 1 deletion src/main/java/org/vaadin/addons/chartjs/DemoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public DemoView() {
.position(Position.LEFT)
.text("Chart.js Combo Bar Line Chart")
.and()
.tooltips()
.enabled(true)
.callbacks()
.label("tooltipItem.yLabel + ' dang units'")
.and()
.and()
.done();

List<String> labels = config.data().getLabels();
Expand All @@ -80,9 +86,11 @@ public DemoView() {
}

ChartJs chart = new ChartJs(config);
chart.setHeight("800px");
chart.setWidth("1000px");

add(chart);
add(myChart);
chart.update();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.BarChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -57,4 +58,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.BubbleChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -55,4 +56,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.io.Serializable;

import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.utils.JsonBuilder;

public interface ChartConfig extends JsonBuilder, Serializable {
public AbstractOptions<?> getOptions();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.PieChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -54,4 +55,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.LineChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -47,4 +48,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.PieChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -55,4 +56,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.PolarAreaChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -47,4 +48,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.RadarChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -49,4 +50,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vaadin.addons.chartjs.config;

import org.vaadin.addons.chartjs.data.Data;
import org.vaadin.addons.chartjs.options.AbstractOptions;
import org.vaadin.addons.chartjs.options.types.LineChartOptions;
import org.vaadin.addons.chartjs.utils.JUtils;

Expand Down Expand Up @@ -47,4 +48,9 @@ public JsonObject buildJson() {
}
return map;
}

@Override
public AbstractOptions<?> getOptions() {
return options;
}
}
3 changes: 0 additions & 3 deletions src/main/java/org/vaadin/addons/chartjs/options/Tooltips.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,6 @@ public JsonObject buildJson() {
JUtils.putNotNull(map, "displayColors", displayColors);
JUtils.putNotNull(map, "borderColor", borderColor );
JUtils.putNotNull(map, "borderWidth", borderWidth );
if (callbacks != null) {
JUtils.putNotNull(map, "callbacks", callbacks.buildJson());
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package org.vaadin.addons.chartjs.options;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.vaadin.addons.chartjs.ChartJsUtils;
import org.vaadin.addons.chartjs.utils.And;
import org.vaadin.addons.chartjs.utils.JUtils;
import org.vaadin.addons.chartjs.utils.JsonBuilder;
Expand Down Expand Up @@ -187,6 +194,42 @@ public TooltipsCallbacks<T> afterFooter(String afterFooter) {
this.afterFooter = afterFooter;
return this;
}

public Map<String, String> asMap() {
HashMap<String, String> ret = new HashMap<String, String>();
ChartJsUtils.putNotNull(ret, "beforeTitle", beforeTitle);
ChartJsUtils.putNotNull(ret, "title", title);
ChartJsUtils.putNotNull(ret, "afterTitle", afterTitle);
ChartJsUtils.putNotNull(ret, "beforeBody", beforeBody);
ChartJsUtils.putNotNull(ret, "beforeLabel", beforeLabel);
ChartJsUtils.putNotNull(ret, "label", label);
ChartJsUtils.putNotNull(ret, "labelColor", labelColor);
ChartJsUtils.putNotNull(ret, "labelTextColor", labelTextColor);
ChartJsUtils.putNotNull(ret, "afterLabel", afterLabel);
ChartJsUtils.putNotNull(ret, "afterBody", afterBody);
ChartJsUtils.putNotNull(ret, "beforeFooter", beforeFooter);
ChartJsUtils.putNotNull(ret, "footer", footer);
ChartJsUtils.putNotNull(ret, "afterFooter", afterFooter);
return ret;
}

public static Map<String, String[]> argumentMap() {
HashMap<String, String[]> ret = new HashMap<String, String[]>();
ret.put("beforeTitle", new String[] {"tooltipItems", "data"});
ret.put("title", new String[] {"tooltipItems", "data"});
ret.put("afterTitle", new String[] {"tooltipItems", "data"});
ret.put("beforeBody", new String[] {"tooltipItems", "data"});
ret.put("beforeLabel", new String[] {"tooltipItems", "data"});
ret.put("label", new String[] {"tooltipItem", "data"});
ret.put("labelColor", new String[] {"tooltipItem", "chart"});
ret.put("labelTextColor", new String[] {"tooltipItem", "chart"});
ret.put("afterLabel", new String[] {"tooltipItem", "data"});
ret.put("afterBody", new String[] {"tooltipItems", "data"});
ret.put("beforeFooter", new String[] {"tooltipItems", "data"});
ret.put("footer", new String[] {"tooltipItems", "data"});
ret.put("afterFooter", new String[] {"tooltipItems", "data"});
return ret;
}

@Override
public JsonObject buildJson() {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/vaadin/addons/chartjs/utils/JUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import elemental.json.Json;
import elemental.json.JsonArray;
import elemental.json.JsonObject;
import elemental.json.JsonString;
import elemental.json.JsonType;
import elemental.json.JsonValue;
import elemental.json.impl.JreJsonNull;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class JUtils {
/**
Expand Down Expand Up @@ -205,6 +209,13 @@ public static void putNotNullIntListOrSingle(JsonObject obj, String key, List<In
}
}
}

public static String formatJavascriptFunction(String name, String returnValue, String... args) {
String ret = String.format("function %s (%s) {"
+ "return "+returnValue+"; }", name, Stream.of(args).collect(Collectors.joining(", ")));
return ret;

}

/**
* Creates JSON entries for callback functions, ie. key is a property name of a callback function and value is
Expand All @@ -222,10 +233,11 @@ public static void putNotNullIntListOrSingle(JsonObject obj, String key, List<In
public static void putNotNullCallback(JsonObject obj, String key, String value, String... argumentNames) {
if (value != null) {
// add the property with the call back prefix
obj.put(CALLBACK_PREFIX + key, value);
obj.put(key, formatJavascriptFunction(key, value, argumentNames));
JUtils.putNotNullStringListOrSingle(obj, CALLBACK_PREFIX + key + CALLBACK_ARGS_POSTFIX,
Arrays.asList(argumentNames));
}

}

}

0 comments on commit ab8afac

Please sign in to comment.