LineChart DateAxis duplicated date causes off by one error

UI Components for JSF
Post Reply
nt10307
Posts: 2
Joined: 22 Nov 2017, 20:28

22 Nov 2017, 21:04

Problem with the line chart.

On the X-axis, I have a DateAxis that can span anywhere from 1 week to 1 year (based on a selectOneMenu). If the date 11/5/2017 is in the range of dates displayed, the label on the X-axis is duplicated. If you hover over the datapoint above the second iteration of 11/5/2017, the tooltip shows data for 11/6/2017. Each subsequent datapoint is off by one. The datapoint for 11/7/2017 shows over the label for 11/6/2017, the datapoint for 11/22/17 shows over 11/21/17, etc. This problem only appears if the date 11/5/2017 is within the range of dates displayed. Selecting a week to be displayed on the graph shows the information properly because 11/5/2017 is not in the range. Out of curiousity, I changed the year to 2016 to see if the problem exists there as well. It does, but rather than 11/5/2017, the issue shows up with 11/6/2017.

XHTML:

Code: Select all

		<h:form enctype="multipart/form-data" id="softwareMetricsForm" onkeypress="if (event.keyCode == 13) {
                            return false;
                        }">
                    <div style="z-index: 30; float: right; position: relative">
                        <b>
                            <h:outputLabel id="time" for="timePeriod" style="margin-left: 20px; margin-bottom: 5px; padding: 5px" value="Last "/>
                            <p:selectOneMenu id="timePeriod" value="#{softwareMetricsController.timePeriod}">
                                <f:selectItem itemLabel="Week" itemValue="#{7}" />
                                <f:selectItem itemLabel="Month" itemValue="#{30}" />
                                <f:selectItem itemLabel="Quarter" itemValue="#{90}" />
                                <f:selectItem itemLabel="Six Months" itemValue="#{180}" />
                                <f:selectItem itemLabel="Year" itemValue="#{365}" />
                                <p:ajax update="softwareMetricsForm:totalDownloadChart" listener="#{softwareMetricsController.chartUpdates()}"/>
                            </p:selectOneMenu> 
                            <h:outputText value=" of Software Downloads" style=" margin-bottom: 5px; padding: 5px"/>
                        </b>
                    </div>

                    <h:panelGroup id="totalDownloadChart">
                        <p:chart id="downloadChart" type="line" model="#{softwareMetricsController.downloadChart}" 
                                 style="margin-top: 30px; margin-bottom: 30px; z-index: 20; position: relative;"/>
                    </h:panelGroup>
                </h:form>
Backing Bean:

Code: Select all

    public void chartUpdates() {
        LineChartSeries softwareLCS = new LineChartSeries();
        softwareLCS.setLabel("Downloads");
        HashMap<String, Integer> hmap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Calendar calendar = Calendar.getInstance();
        int max = 0;
        int tickCount = timePeriod;

        hmap.put(sdf.format(calendar.getTime()), 0);
        
        for (int i = 0; i < timePeriod; i++) {
            calendar.add(Calendar.DATE, -1);
            hmap.put(sdf.format(calendar.getTime()), 0);
        }
        
        for (SoftwareDownload sd : getSdList()) {
            if (hmap.containsKey(sdf.format(sd.getEventDate()))) {
                hmap.put(sdf.format(sd.getEventDate()), hmap.get(sdf.format(sd.getEventDate())) + 1);

                if (hmap.get(sdf.format(sd.getEventDate())) > max) {
                    max = hmap.get(sdf.format(sd.getEventDate()));
                }
            }
        }

        for (Entry<String, Integer> entry : hmap.entrySet()) {
            softwareLCS.set(entry.getKey(), entry.getValue());
        }

        if (timePeriod > 30) {
            tickCount = 30;
        }

        setDownloadChart(createModel(softwareLCS, "Software Downloads", "Downloads", "Date", tickCount, max));
    }
    
    private LineChartModel createModel(LineChartSeries series, String chartTitle, String yAxisLabel, String xAxisLabel, int tickCount, int max) {
        LineChartModel model = new LineChartModel();

        model.getAxis(AxisType.Y).setLabel(yAxisLabel);
        model.getAxis(AxisType.Y).setMin(0);
        DateAxis axis = new DateAxis(xAxisLabel);
        axis.setTickAngle(-50);
        axis.setTickCount(tickCount);
        axis.setTickFormat("%m/%#d/%y");
        model.getAxes().put(AxisType.X, axis);
        int modelMax = 0;

        if (max >= 1000) {
            model.getAxis(AxisType.Y).setTickInterval("100");
        } else if (max >= 100) {
            model.getAxis(AxisType.Y).setTickInterval("20");
        } else if (max >= 50) {
            model.getAxis(AxisType.Y).setTickInterval("5");
        } else {
            model.getAxis(AxisType.Y).setTickInterval("2");
        }

        while ((max + Integer.parseInt(model.getAxis(AxisType.Y).getTickInterval())) >= modelMax) {
            modelMax += Integer.parseInt(model.getAxis(AxisType.Y).getTickInterval());
        }

        model.getAxis(AxisType.Y).setMax(modelMax);
        model.setTitle(chartTitle);
        model.addSeries(series);
        return model;
    }
PrimeFaces version - 6.0
JSF implementation - 2.2.13
Server - Wildfly 10.1.0

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

23 Nov 2017, 00:08

Is this a DST bug? I think your describing this issue: https://github.com/primefaces/primefaces/issues/810

Possibly related to core JqPlot: https://github.com/jqPlot/jqPlot/issues/116
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

nt10307
Posts: 2
Joined: 22 Nov 2017, 20:28

27 Nov 2017, 19:59

Yes, this appears to be a DST bug. Still trying to find a fix. Specifying GMT or UTC doesn't seem to help because the timezone appears to be reverted to local time under the hood.
PrimeFaces version - 6.0
JSF implementation - 2.2.13
Server - Wildfly 10.1.0

Melloware
Posts: 3717
Joined: 22 Apr 2013, 15:48

27 Nov 2017, 20:00

If you figure it out let me know.
PrimeFaces Developer | PrimeFaces Extensions Developer
GitHub Profile: https://github.com/melloware
PrimeFaces Elite 13.0.0 / PF Extensions 13.0.0
PrimeReact 9.6.1

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 30 guests