use of com.pdfjet.Line in project Diaguard by Faltenreich.
the class PdfTimeline method drawChart.
private Point drawChart(PdfPage page, Point position, List<BloodSugar> bloodSugars) throws Exception {
chart.setColor(Color.transparent);
chart.setPosition(position.getX(), position.getY());
float[] coordinates = chart.drawOn(page);
TextLine label = new TextLine(cache.getFontNormal());
label.setColor(Color.gray);
Line line = new Line();
line.setColor(Color.gray);
float chartWidth = chart.getWidth();
float chartHeight = chart.getHeight();
float chartStartX = 0;
float chartEndX = chartStartX + chart.getWidth();
float chartStartY = 0;
float chartEndY = chartStartY + chartHeight;
float contentStartX = getLabelWidth();
float contentStartY = chartStartY + label.getHeight() + PADDING;
float contentEndX = chartEndX;
float contentEndY = chartEndY;
float contentWidth = contentEndX - contentStartX;
float contentHeight = contentEndY - contentStartY;
int xStep = DateTimeConstants.MINUTES_PER_HOUR * HOUR_INTERVAL;
float xMax = DateTimeConstants.MINUTES_PER_DAY;
TextLine header = new TextLine(cache.getFontBold());
header.setText(DateTimeUtils.toWeekDayAndDate(cache.getDateTime()));
header.setPosition(chartStartX, chartStartY + header.getHeight());
header.placeIn(chart);
header.drawOn(page);
// Labels for x axis
int minutes = 0;
while (minutes <= xMax) {
float x = contentStartX + ((float) minutes / xMax) * contentWidth;
label.setText(String.valueOf(minutes / 60));
label.setPosition(x - label.getWidth() / 2, chartStartY + header.getHeight());
label.placeIn(chart);
label.drawOn(page);
line.setStartPoint(x, chartStartY + header.getHeight() + 8);
line.setEndPoint(x, contentEndY);
line.placeIn(chart);
line.drawOn(page);
minutes += xStep;
}
if (showChartForBloodSugar) {
float yMin = 40;
float yMax = 210;
for (BloodSugar bloodSugar : bloodSugars) {
if (bloodSugar.getMgDl() > yMax) {
yMax = bloodSugar.getMgDl();
}
}
int yStep = (int) ((yMax - yMin) / 5);
yStep = Math.round((yStep + 10) / 10) * 10;
// Labels for y axis
int labelValue = yStep;
float labelY;
while ((labelY = contentStartY + contentHeight - ((labelValue / yMax) * contentHeight)) >= contentStartY) {
label.setText(PreferenceStore.getInstance().getMeasurementForUi(Category.BLOODSUGAR, labelValue));
label.setPosition(chartStartX, labelY + (label.getHeight() / 4));
label.placeIn(chart);
label.drawOn(page);
line.setStartPoint(chartStartX + label.getWidth() + PADDING, labelY);
line.setEndPoint(contentEndX, labelY);
line.placeIn(chart);
line.drawOn(page);
labelValue += yStep;
}
Point point = new Point();
point.setFillShape(true);
point.setRadius(POINT_RADIUS);
for (BloodSugar bloodSugar : bloodSugars) {
Entry entry = bloodSugar.getEntry();
float minute = entry.getDate().getMinuteOfDay();
float value = bloodSugar.getMgDl();
float x = contentStartX + ((minute / xMax) * contentWidth);
float y = contentStartY + (contentHeight - (value / yMax) * contentHeight);
point.setPosition(x, y);
int color = Color.black;
if (cache.getConfig().highlightLimits()) {
if (value > PreferenceStore.getInstance().getLimitHyperglycemia()) {
color = cache.getColorHyperglycemia();
} else if (value < PreferenceStore.getInstance().getLimitHypoglycemia()) {
color = cache.getColorHypoglycemia();
}
}
point.setColor(color);
point.placeIn(chart);
point.drawOn(page);
}
}
return new Point(position.getX(), coordinates[1]);
}
Aggregations