Performance improvement

This commit is contained in:
Andrea Cavalli 2022-09-15 18:37:57 +02:00
parent 2c1bd94f84
commit 7802386392

View File

@ -92,7 +92,7 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
var xValueLineOffset = topPadding + graphHeight; var xValueLineOffset = topPadding + graphHeight;
var yLabels = getYLabels(graph, graphHeight, valuesFontMetrics, scaleY); var yLabels = getYLabels(graph, graphHeight, valuesFontMetrics, scaleY, y.mode());
RasterSize yLabelsAreaSize = computeYLabelsAreaSize(y.mode(), graphHeight, valuesFontMetrics, yLabels); RasterSize yLabelsAreaSize = computeYLabelsAreaSize(y.mode(), graphHeight, valuesFontMetrics, yLabels);
var yValuesWidth = yLabelsAreaSize.width(); var yValuesWidth = yLabelsAreaSize.width();
var yValueLineOffset = leftPadding + yAxisNameWidth + yValuesToYAxisNamePadding + yValuesWidth; var yValueLineOffset = leftPadding + yAxisNameWidth + yValuesToYAxisNamePadding + yValuesWidth;
@ -133,7 +133,7 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
} }
} }
var xLabels = getXLabels(graph, graphWidth, valuesFontMetrics, scaleX); var xLabels = getXLabels(graph, graphWidth, valuesFontMetrics, scaleX, x.mode());
RasterSize yAxisNameCenterOffset = new RasterSize(leftPadding + valuesFontMetrics.getHeight() / 2d, valuesFontMetrics.getHeight() RasterSize yAxisNameCenterOffset = new RasterSize(leftPadding + valuesFontMetrics.getHeight() / 2d, valuesFontMetrics.getHeight()
// Add half of graph height // Add half of graph height
@ -752,7 +752,11 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
private static List<LabelWithOffset> getXLabels(Graph graph, private static List<LabelWithOffset> getXLabels(Graph graph,
double labelsAreaWidth, double labelsAreaWidth,
FontMetrics valuesFontMetrics, FontMetrics valuesFontMetrics,
NiceScale scaleX) { NiceScale scaleX,
AxisMode mode) {
if (mode == AxisMode.HIDE) {
return List.of();
}
var bounds = graph.data().bounds(); var bounds = graph.data().bounds();
var minX = bounds.minX(); var minX = bounds.minX();
var maxX = bounds.maxX(); var maxX = bounds.maxX();
@ -766,11 +770,15 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
double currentRasterOffset = 0; double currentRasterOffset = 0;
double currentValue = minX; double currentValue = minX;
while (currentValue <= maxX && i < MAX_LABELS && (scaleX.getTickSpacing() > 0)) { while (currentValue <= maxX && i < MAX_LABELS && (scaleX.getTickSpacing() > 0)) {
var formatted = format.apply(currentValue); if (mode.showLabels()) {
var stringWidth = valuesFontMetrics.stringWidth(formatted); var formatted = format.apply(currentValue);
if (currentRasterOffset - stringWidth / 2d > prevRasterLabelEndOffset) { var stringWidth = valuesFontMetrics.stringWidth(formatted);
labels.add(new LabelWithOffset(currentValue, currentRasterOffset, formatted)); if (currentRasterOffset - stringWidth / 2d > prevRasterLabelEndOffset) {
prevRasterLabelEndOffset = currentRasterOffset + stringWidth / 2d; labels.add(new LabelWithOffset(currentValue, currentRasterOffset, formatted));
prevRasterLabelEndOffset = currentRasterOffset + stringWidth / 2d;
} else {
labels.add(new LabelWithOffset(currentValue, currentRasterOffset, ""));
}
} else { } else {
labels.add(new LabelWithOffset(currentValue, currentRasterOffset, "")); labels.add(new LabelWithOffset(currentValue, currentRasterOffset, ""));
} }
@ -788,7 +796,11 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
private static List<LabelWithOffset> getYLabels(Graph graph, private static List<LabelWithOffset> getYLabels(Graph graph,
double labelsAreaHeight, double labelsAreaHeight,
FontMetrics valuesFontMetrics, FontMetrics valuesFontMetrics,
NiceScale scaleY) { NiceScale scaleY,
AxisMode mode) {
if (mode == AxisMode.HIDE) {
return List.of();
}
var bounds = graph.data().bounds(); var bounds = graph.data().bounds();
var minY = bounds.minY(); var minY = bounds.minY();
var maxY = bounds.maxY(); var maxY = bounds.maxY();
@ -804,9 +816,13 @@ public class AWTGraphRenderer implements IGraphRenderer<AWTDrawer> {
double currentRasterOffset = labelsAreaHeight; double currentRasterOffset = labelsAreaHeight;
double currentValue = minY; double currentValue = minY;
while (currentValue <= maxY && i < MAX_LABELS && (scaleY.getTickSpacing() > 0)) { while (currentValue <= maxY && i < MAX_LABELS && (scaleY.getTickSpacing() > 0)) {
if (currentRasterOffset + stringBottom < prevRasterLabelEndOffset) { if (mode.showLabels()) {
labels.add(new LabelWithOffset(currentValue, currentRasterOffset, format.apply(currentValue))); if (currentRasterOffset + stringBottom < prevRasterLabelEndOffset) {
prevRasterLabelEndOffset = currentRasterOffset - stringTop; labels.add(new LabelWithOffset(currentValue, currentRasterOffset, format.apply(currentValue)));
prevRasterLabelEndOffset = currentRasterOffset - stringTop;
} else {
labels.add(new LabelWithOffset(currentValue, currentRasterOffset, ""));
}
} else { } else {
labels.add(new LabelWithOffset(currentValue, currentRasterOffset, "")); labels.add(new LabelWithOffset(currentValue, currentRasterOffset, ""));
} }