-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWHighChart.java
146 lines (122 loc) · 4.62 KB
/
WHighChart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package nl.topicus.whighcharts.components;
import java.io.IOException;
import java.util.Collection;
import org.apache.wicket.Application;
import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.model.IModel;
import org.odlabs.wiquery.core.javascript.JsStatement;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import nl.topicus.whighcharts.components.modules.WHighChartsExportingJavaScriptResourceReference;
import nl.topicus.whighcharts.options.WHighChartGlobalSettings;
import nl.topicus.whighcharts.options.WHighChartOptions;
import nl.topicus.whighcharts.options.axis.IWHighChartAxisCategoriesProvider;
import nl.topicus.whighcharts.options.series.ISeries;
import nl.topicus.whighcharts.options.series.ISeriesEntry;
public class WHighChart<V, E extends ISeriesEntry<V>> extends WebMarkupContainer
{
private static final long serialVersionUID = 1L;
private WHighChartOptions<V, E> options = new WHighChartOptions<V, E>(this);
private WHighChartGlobalSettings globalSettings = new WHighChartGlobalSettings();
public WHighChart(String id)
{
this(id, null);
}
public WHighChart(String id, IModel< ? extends Collection< ? extends ISeries<V, E>>> model)
{
super(id, model);
setOutputMarkupId(true);
}
public WHighChartOptions<V, E> getOptions()
{
return options;
}
public WHighChartGlobalSettings getGlobalSettings()
{
return globalSettings;
}
@Override
public void renderHead(IHeaderResponse response)
{
response.render(
JavaScriptHeaderItem.forReference(WHighChartsJavaScriptResourceReference.get()));
response.render(
JavaScriptHeaderItem.forReference(WHighChartsExtraJavaScriptResourceReference.get()));
response.render(JavaScriptHeaderItem
.forReference(WHighChartsDefaultsJavaScriptResourceReference.get()));
if (getOptions().getExporting().getEnabled() != null
&& getOptions().getExporting().getEnabled().booleanValue())
response.render(JavaScriptHeaderItem
.forReference(WHighChartsExportingJavaScriptResourceReference.get()));
response.render(JavaScriptHeaderItem.forScript("var " + getMarkupId() + ";",
"highchart_" + getMarkupId()));
response.render(OnDomReadyHeaderItem.forScript(statement().render().toString()));
}
public JsStatement statement()
{
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().withSerializationInclusion(Include.NON_NULL);
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
if (Application.exists() && RuntimeConfigurationType.DEVELOPMENT
.equals(Application.get().getConfigurationType()))
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String optionsStr = "{}";
String globalOptions = "{}";
try
{
if (getModel() != null)
{
getOptions().getSeries().clear();
getOptions().getSeries().addAll(getModel().getObject());
if (getModel() instanceof IWHighChartAxisCategoriesProvider)
{
IWHighChartAxisCategoriesProvider categoriesProvider =
(IWHighChartAxisCategoriesProvider) getModel();
if (getOptions().getxAxis().getCategories() == null
|| getOptions().getxAxis().getCategories().isEmpty())
{
getOptions().getxAxis()
.setCategories(categoriesProvider.getxAxisCategories());
}
if (getOptions().getyAxis().getCategories() == null
|| getOptions().getyAxis().getCategories().isEmpty())
{
getOptions().getyAxis()
.setCategories(categoriesProvider.getyAxisCategories());
}
}
}
optionsStr = mapper.writeValueAsString(options);
globalOptions =
"Highcharts.setOptions(" + mapper.writeValueAsString(getGlobalSettings()) + ");\n";
}
catch (JsonGenerationException e)
{
e.printStackTrace();
}
catch (JsonMappingException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
JsStatement jsStatement = new JsStatement().append(
globalOptions + getMarkupId() + " = new Highcharts.Chart( " + optionsStr + " );\n");
return jsStatement;
}
@SuppressWarnings("unchecked")
public IModel< ? extends Collection< ? extends ISeries<V, E>>> getModel()
{
return (IModel< ? extends Collection< ? extends ISeries<V, E>>>) getDefaultModel();
}
}