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