在vaadin图表4.0中包含moment.js,以设置轴格式

时间:2018-08-27 14:22:01

标签: momentjs vaadin vaadin8 vaadin-charts

您好,我正在使用Vaadin 8和Vaadin图表4.0,并尝试根据用户格式输入和区域设置将图表的数据轴之一格式化为不同的日期时间格式。我尝试注入moment.js库,并获得一个引用异常,指出矩变量不存在。我尝试了以下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)我尝试将script标签附加在函数本身的开头。

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);

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以通过在IeUIServlet中注册BootstrapListener来将script标记注入文档的开头:

        @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\");