Reputation: 2102
I'm trying to create a line chart using the component <p:chart>
of the Primefaces 5.1.
And I need the X axis displays the time. Something like this:
But only against documentation of the old version.
I found several things like:
But in version 5.1 there is no such tag <p:lineChart>
, and not even the attribute extender=""
.
I created my chart based on the showcase:
But this example does not help much, because it is only working with date. And to complicate are as String. I need the hours.
I wonder if someone has already created something similar in the newer versions of Primefaces.
EDIT:
Searching a bit more, I found this topic:
I used the example mentioned it, and I could make it work.
What I could see, when it is a DateAxis is required to set the axis.setMax()
. At the end looked like this:
It worked as I needed, but two things I did not understand:
First: The reason to be required to set the axis.setMax()
.
Second: What is this pattern used in axis.setTickFormat("%H:%#M:%S")
?
Upvotes: 3
Views: 13195
Reputation: 2102
Ok, I think now I found a acceptable solution.
The initial problem with the chart was axis.setMax()
.
And the pattern used in the axis.setTickFormat
is:
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
The model:
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);
}
And the chart:
<p:chart type="line" model="#{controller.dateModel}" />
Upvotes: 14