p:带有DateTime轴的图表

时间:2015-02-10 15:44:00

标签: primefaces charts jqplot

我尝试使用Primefaces 5.1的组件<p:chart>创建折线图。

我需要X轴显示时间。像这样:

enter image description here

但仅限于旧版本的文档。

我发现了几件事:

但是在版本5.1中没有这样的标记<p:lineChart>,甚至没有属性extender=""

我根据展示创建了我的图表:

但是这个例子并没有多大帮助,因为它只适用于日期。并且复杂化为String。我需要时间。

我想知道是否有人在较新版本的Primefaces中创建了类似内容。

修改

再多搜索一下,我找到了这个话题:

我使用了它提到的例子,我可以使它工作。

我可以看到,当需要DateAxis时,需要设置axis.setMax()。最后看起来像这样:

enter image description here

它按照我的需要工作,但有两件我不明白的事情:

首先:需要设置axis.setMax()的原因。

第二: axis.setTickFormat("%H:%#M:%S")中使用的模式是什么?

1 个答案:

答案 0 :(得分:12)

好的,我想我现在找到了一个可以接受的解决方案。

图表的初始问题是axis.setMax()

axis.setTickFormat中使用的模式是:

Code    Result                  Description
            == Years ==
%Y      2008                Four-digit year
%y      08                  Two-digit year
            == Months ==
%m      09                  Two-digit month
%#m     9                   One or two-digit month
%B      September           Full month name
%b      Sep                 Abbreviated month name
            == Days ==
%d      05                  Two-digit day of month
%#d     5                   One or two-digit day of month
%e      5                   One or two-digit day of month
%A      Sunday              Full name of the day of the week
%a      Sun                 Abbreviated name of the day of the week
%w      0                   Number of the day of the week (0 = Sunday, 6 = Saturday)
%o      th                  The ordinal suffix string following the day of the month
            == Hours ==
%H      23                  Hours in 24-hour format (two digits)
%#H     3                   Hours in 24-hour integer format (one or two digits)
%I      11                  Hours in 12-hour format (two digits)
%#I     3                   Hours in 12-hour integer format (one or two digits)
%p      PM                  AM or PM
            == Minutes ==
%M      09                  Minutes (two digits)
%#M     9                   Minutes (one or two digits)
            == Seconds ==
%S      02                  Seconds (two digits)
%#S     2                   Seconds (one or two digits)
%s      1206567625723       Unix timestamp (Seconds past 1970-01-01 00:00:00)
            == Milliseconds ==
%N      008                 Milliseconds (three digits)
%#N     8                   Milliseconds (one to three digits)
            == Timezone ==
%O      360                 difference in minutes between local time and GMT
%Z      Mountain Standard Time  Name of timezone as reported by browser
%G      -06:00              Hours and minutes between GMT
            == Shortcuts ==
%F      2008-03-26          %Y-%m-%d
%T      05:06:30            %H:%M:%S
%X      05:06:30            %H:%M:%S
%x      03/26/08            %m/%d/%y
%D      03/26/08            %m/%d/%y
%#c     Wed Mar 26 15:31:00 2008  %a %b %e %H:%M:%S %Y
%v      3-Sep-2008          %e-%b-%Y
%R      15:31               %H:%M
%r      3:31:00 PM          %I:%M:%S %p
            == Characters ==
%n      \n                  Newline
%t      \t                  Tab
%%      %                   Percent Symbol

模特:

private void createDateModel() {
    dateModel = new LineChartModel();
    LineChartSeries series1 = new LineChartSeries();
    series1.setLabel("Series 1");
    series1.set("2014-01-01 00:10:50", 51);
    series1.set("2014-01-01 00:10:51", 22);
    series1.set("2014-01-01 00:10:52", 65);
    series1.set("2014-01-01 00:10:53", 35);
    dateModel.addSeries(series1);
    dateModel.getAxis(AxisType.Y).setLabel("Values");
    DateAxis axis = new DateAxis("Dates");
    axis.setTickAngle(-50);
    axis.setMax("2014-01-01 00:10:56");
    axis.setTickFormat("%H:%#M:%S");
    dateModel.getAxes().put(AxisType.X, axis);
}

图表:

<p:chart type="line" model="#{controller.dateModel}" />