1

您好,我正在使用 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);

任何帮助,将不胜感激。

4

2 回答 2

0

一些可能的(纯 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
于 2018-08-28T15:55:23.670 回答
0

您可以通过在您的 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,如下所示:

var g = $wnd.moment.tz(\"2014-06-01 12:00\", \"America/New_York\");
于 2018-08-29T11:58:00.137 回答