在 vaadin 图表 4.0 中包含 moment.js 以设置轴格式
Include moment.js in vaadin charts 4.0 to set axis format
您好,我正在使用 Vaadin 8 和 Vaadin 图表 4.0,我正在尝试根据用户格式输入和区域设置将图表的数据轴之一格式化为不同的日期时间格式。我尝试注入 moment.js 库并得到一个引用异常,指出 moment 变量不存在。我尝试了下面列出的 2 种不同方法:
(1)使用@JavaScript注解注入moment.js。
@JavaScript({"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js", "https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"})
@WebServlet(urlPatterns = "/*", name = "IeUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = IeUI.class, productionMode = true, heartbeatInterval = 500, closeIdleSessions = false, widgetset = "softcom.ievaadin.AppWidgetSet")
public static class IeUIServlet extends VaadinServlet implements SessionInitListener {//, SessionDestroyListener {
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
// Add any system level init here
//Main.initSystem();
}
@Override
public void destroy() {
super.destroy(); //To change body of generated methods, choose Tools | Templates.
Main.destroy();
}
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().getSession().setMaxInactiveInterval(-1);
}
}
以及设置格式化程序的地方
YAxis ya = new YAxis();
String js = "function() {";
js += "try {"
+ "var g = moment.tz(\"2014-06-01 12:00\", \"America/New_York\");"
+ "alert(g);"
+ "return g;}"
+ "catch(e) {"
+ "alert(e);}}";
ya.getLabels().setFormatter(js);
(2) 我尝试在函数本身的头部附加脚本标签。
String js = "attempt {"
+ "var script = document.createElement('script');"
+ "script.src = \"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js\";"
+ "document.head.appendChild(script);"
+ "} catch(e) {"
+ "alert('first one'); }"
+ "try {"
+ "var b = moment().format();"
+ "return b;"
+ "} catch (e) {"
+ "alert(e); }";
JavaScript.getCurrent().execute(attempt);
String js = "function() {";
js += "try {"
+ "var g = moment.tz(\"2014-06-01 12:00\", \"America/New_York\");"
+ "alert(g);"
+ "return g;}"
+ "catch(e) {"
+ "alert(e);}}";
ya.getLabels().setFormatter(js);
如有任何帮助,我们将不胜感激。
一些可能的(纯 Vaadin)方法:
1) 自定义标签:mychart.getConfiguration().getxAxis().setDateTimeLabelFormats(dateTimeLabelFormats);
2) 自定义时区和偏移量:
JavaScript.getCurrent().execute("Highcharts.setOptions({\n" +
" global: {\n" +
" useUTC: true,\n" +
" timezoneOffset: "+ timezoneOffsetInMinutes + "\n" +
" }\n" +
"});");
3) 使用用户友好的日期时间格式配置工具提示:
DataSeriesItem dataSeriesItem;
Date datetime = ...;
if (useUTC) {
dataSeriesItem = new DataSeriesItem(datetime.toInstant(), value);
} else {
dataSeriesItem = new DataSeriesItem(datetime, value);
}
dataSeriesItem.setId(xxxxx); // <= string with desired user/locale-dependant formatting of related datetime
您可以通过在 IeUIServlet 中注册一个 BootstrapListener 来在文档的头部注入脚本标签:
@Override
protected void servletInitialized() {
getService().addSessionInitListener(event -> {
event.getSession().addBootstrapListener(new BootstrapListener() {
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
response.getDocument().head().append("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js\"></script>");
response.getDocument().head().append("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js\"></script>");
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
}
});
});
}
并在您的 javascript 中预先添加 $wnd 到 moment,如下所示:
var g = $wnd.moment.tz(\"2014-06-01 12:00\", \"America/New_York\");
您好,我正在使用 Vaadin 8 和 Vaadin 图表 4.0,我正在尝试根据用户格式输入和区域设置将图表的数据轴之一格式化为不同的日期时间格式。我尝试注入 moment.js 库并得到一个引用异常,指出 moment 变量不存在。我尝试了下面列出的 2 种不同方法:
(1)使用@JavaScript注解注入moment.js。
@JavaScript({"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js", "https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"})
@WebServlet(urlPatterns = "/*", name = "IeUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = IeUI.class, productionMode = true, heartbeatInterval = 500, closeIdleSessions = false, widgetset = "softcom.ievaadin.AppWidgetSet")
public static class IeUIServlet extends VaadinServlet implements SessionInitListener {//, SessionDestroyListener {
@Override
protected void servletInitialized() throws ServletException {
super.servletInitialized();
// Add any system level init here
//Main.initSystem();
}
@Override
public void destroy() {
super.destroy(); //To change body of generated methods, choose Tools | Templates.
Main.destroy();
}
@Override
public void sessionInit(SessionInitEvent event) throws ServiceException {
event.getSession().getSession().setMaxInactiveInterval(-1);
}
}
以及设置格式化程序的地方
YAxis ya = new YAxis();
String js = "function() {";
js += "try {"
+ "var g = moment.tz(\"2014-06-01 12:00\", \"America/New_York\");"
+ "alert(g);"
+ "return g;}"
+ "catch(e) {"
+ "alert(e);}}";
ya.getLabels().setFormatter(js);
(2) 我尝试在函数本身的头部附加脚本标签。
String js = "attempt {"
+ "var script = document.createElement('script');"
+ "script.src = \"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js\";"
+ "document.head.appendChild(script);"
+ "} catch(e) {"
+ "alert('first one'); }"
+ "try {"
+ "var b = moment().format();"
+ "return b;"
+ "} catch (e) {"
+ "alert(e); }";
JavaScript.getCurrent().execute(attempt);
String js = "function() {";
js += "try {"
+ "var g = moment.tz(\"2014-06-01 12:00\", \"America/New_York\");"
+ "alert(g);"
+ "return g;}"
+ "catch(e) {"
+ "alert(e);}}";
ya.getLabels().setFormatter(js);
如有任何帮助,我们将不胜感激。
一些可能的(纯 Vaadin)方法:
1) 自定义标签:mychart.getConfiguration().getxAxis().setDateTimeLabelFormats(dateTimeLabelFormats);
2) 自定义时区和偏移量:
JavaScript.getCurrent().execute("Highcharts.setOptions({\n" +
" global: {\n" +
" useUTC: true,\n" +
" timezoneOffset: "+ timezoneOffsetInMinutes + "\n" +
" }\n" +
"});");
3) 使用用户友好的日期时间格式配置工具提示:
DataSeriesItem dataSeriesItem;
Date datetime = ...;
if (useUTC) {
dataSeriesItem = new DataSeriesItem(datetime.toInstant(), value);
} else {
dataSeriesItem = new DataSeriesItem(datetime, value);
}
dataSeriesItem.setId(xxxxx); // <= string with desired user/locale-dependant formatting of related datetime
您可以通过在 IeUIServlet 中注册一个 BootstrapListener 来在文档的头部注入脚本标签:
@Override
protected void servletInitialized() {
getService().addSessionInitListener(event -> {
event.getSession().addBootstrapListener(new BootstrapListener() {
@Override
public void modifyBootstrapPage(BootstrapPageResponse response) {
response.getDocument().head().append("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js\"></script>");
response.getDocument().head().append("<script src=\"https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js\"></script>");
}
@Override
public void modifyBootstrapFragment(BootstrapFragmentResponse response) {
}
});
});
}
并在您的 javascript 中预先添加 $wnd 到 moment,如下所示:
var g = $wnd.moment.tz(\"2014-06-01 12:00\", \"America/New_York\");