PrimeFaces - setting up a date chart

I use PrimeFaces 3.4.1 to plot a timeline (date on the x axis, int value on the y axis).

At the moment, I have something like this:

XHTML

<p:lineChart id="timeChart" value="#{myGraphBean.model}" legendPosition="e" title="Time Chart" minY="0" maxY="25" style="height:300px"/> 

Java bean :

 private final static int MAX_VALUE = 20; private final static int NUMBER_OF_POINTS = 20; private final static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yy"); private void createLinearModel() { model = new CartesianChartModel(); Calendar day = Calendar.getInstance(); day.set(Calendar.HOUR_OF_DAY, 0); day.set(Calendar.MINUTE, 0); day.set(Calendar.SECOND, 0); day.set(Calendar.MILLISECOND, 0); LineChartSeries series = new LineChartSeries(); series.setLabel("My series"); for (int i = 0; i < NUMBER_OF_POINTS; i++) { series.set(dateFormat.format(day.getTime()), getRandomValue()); day.add(Calendar.DAY_OF_MONTH, 1); } model.addSeries(series); } private int getRandomValue() { return rand.nextInt(MAX_VALUE); } 

So my bean just creates some random int value for each day. (My application creates actual data points, this is just a dummy example)

In my application, I have been generating data for a long time, for example, 6 months. Now when I do something, I get terrible graphics:

Bad graph

What I would like to do:

I would like my data to be indicated, but I only show a checkmark, say, every month.

I tried changing the checkmark on the x axis following a few other messages ( ex1 , ex2 ), but I couldn't get it to work.

Using the primfaces extender, I tried using things like tickInterval: '1 month' by setting the min attributes, but nothing tickInterval: '1 month' . In most cases, it just breaks the schedule.

Questions:

  • The jqPlot document says: โ€œNote that although jqPlot will parse most of any date available to a person, it is safer to use javascript timestamps when possible. It is also better to specify a date and time rather than just one date. This is due to inconsistent browser processing local time compared to UTC with bare dates. " In my bean plot, should I fill out the series using formatted date (String) after the javascript timestamp ("yyyy-MM-dd h: mma") or directly using java.util.Date?

  • How to change the tick along the x axis (letโ€™s say, 1 month), having a marker only every month, but still the chart goes through all the daytime points (i.e. I donโ€™t want to average my points for a month)?

+7
source share
1 answer

In Java, fill LineChartSeries objects every milliseconds with Date#getTime() .

On the facelets side, download jqPlot from the site and import the following jqPlot plugins:

 <h:outputScript name="Javascript/jqplot.canvasAxisTickRenderer.js" /> <h:outputScript name="Javascript/jqplot.canvasAxisTickRenderer.min.js" /> <h:outputScript name="Javascript/jqplot.dateAxisRenderer.js" /> <h:outputScript name="Javascript/jqplot.dateAxisRenderer.min.js" /> 

And use this js extender for the p:lineChart :

 function chartExtender() { this.cfg.axes = { xaxis : { renderer : $.jqplot.DateAxisRenderer, rendererOptions : { tickRenderer:$.jqplot.CanvasAxisTickRenderer }, tickOptions : { fontSize:'10pt', fontFamily:'Tahoma', angle:-40 } }, yaxis : { rendererOptions : { tickRenderer:$.jqplot.CanvasAxisTickRenderer }, tickOptions: { fontSize:'10pt', fontFamily:'Tahoma', angle:30 } } }; this.cfg.axes.xaxis.ticks = this.cfg.categories; } 

Useful links:

http://forum.primefaces.org/viewtopic.php?f=3&t=25289#p79916

http://forum.primefaces.org/viewtopic.php?f=3&t=23891&p=78637#p78637

+6
source

All Articles