/** Draws the histogram labels */ protected void drawHistLabels() { parent.pushStyle(); parent.textMode(MODEL); parent.textFont(font); parent.textSize(fontSize); parent.fill(fontColor); parent.noStroke(); if (type == GPlot.VERTICAL) { if (rotateLabels) { parent.textAlign(RIGHT, CENTER); for (int i = 0; i < plotPoints.getNPoints(); i++) { if (plotPoints.isValid(i) && plotPoints.getX(i) >= 0 && plotPoints.getX(i) <= dim[0]) { parent.pushMatrix(); parent.translate(plotPoints.getX(i), labelsOffset); parent.rotate(-HALF_PI); parent.text(plotPoints.getLabel(i), 0, 0); parent.popMatrix(); } } } else { parent.textAlign(CENTER, TOP); for (int i = 0; i < plotPoints.getNPoints(); i++) { if (plotPoints.isValid(i) && plotPoints.getX(i) >= 0 && plotPoints.getX(i) <= dim[0]) { parent.text(plotPoints.getLabel(i), plotPoints.getX(i), labelsOffset); } } } } else { if (rotateLabels) { parent.textAlign(CENTER, BOTTOM); for (int i = 0; i < plotPoints.getNPoints(); i++) { if (plotPoints.isValid(i) && -plotPoints.getY(i) >= 0 && -plotPoints.getY(i) <= dim[1]) { parent.pushMatrix(); parent.translate(-labelsOffset, plotPoints.getY(i)); parent.rotate(-HALF_PI); parent.text(plotPoints.getLabel(i), 0, 0); parent.popMatrix(); } } } else { parent.textAlign(RIGHT, CENTER); for (int i = 0; i < plotPoints.getNPoints(); i++) { if (plotPoints.isValid(i) && -plotPoints.getY(i) >= 0 && -plotPoints.getY(i) <= dim[1]) { parent.text(plotPoints.getLabel(i), -labelsOffset, plotPoints.getY(i)); } } } } parent.popStyle(); }
/** Updates the differences, leftSides and rightSides arrays */ protected void updateArrays() { int nPoints = plotPoints.getNPoints(); if (nPoints == 1) { leftSides.set(0, (type == GPlot.VERTICAL) ? 0.2f * dim[0] : 0.2f * dim[1]); rightSides.set(0, leftSides.get(0)); } else if (nPoints > 1) { // Calculate the differences between consecutive points for (int i = 0; i < nPoints - 1; i++) { if (plotPoints.isValid(i) && plotPoints.isValid(i + 1)) { float separation = separations[i % separations.length]; float diff; if (type == GPlot.VERTICAL) { diff = plotPoints.getX(i + 1) - plotPoints.getX(i); } else { diff = plotPoints.getY(i + 1) - plotPoints.getY(i); } if (diff > 0) { differences.set(i, (diff - separation) / 2f); } else { differences.set(i, (diff + separation) / 2f); } } else { differences.set(i, 0f); } } // Fill the leftSides and rightSides arrays leftSides.set(0, differences.get(0)); rightSides.set(0, differences.get(0)); for (int i = 1; i < nPoints - 1; i++) { leftSides.set(i, differences.get(i - 1)); rightSides.set(i, differences.get(i)); } leftSides.set(nPoints - 1, differences.get(nPoints - 2)); rightSides.set(nPoints - 1, differences.get(nPoints - 2)); } }
/** * Draws the histogram * * @param plotBasePoint the histogram base point in the plot reference system */ public void draw(GPoint plotBasePoint) { if (visible) { // Calculate the baseline for the histogram float baseline = 0; if (plotBasePoint.isValid()) { baseline = (type == GPlot.VERTICAL) ? plotBasePoint.getY() : plotBasePoint.getX(); } // Draw the rectangles parent.pushStyle(); parent.rectMode(CORNERS); parent.strokeCap(SQUARE); for (int i = 0; i < plotPoints.getNPoints(); i++) { if (plotPoints.isValid(i)) { // Obtain the corners float x1, x2, y1, y2; if (type == GPlot.VERTICAL) { x1 = plotPoints.getX(i) - leftSides.get(i); x2 = plotPoints.getX(i) + rightSides.get(i); y1 = plotPoints.getY(i); y2 = baseline; } else { x1 = baseline; x2 = plotPoints.getX(i); y1 = plotPoints.getY(i) - leftSides.get(i); y2 = plotPoints.getY(i) + rightSides.get(i); } if (x1 < 0) { x1 = 0; } else if (x1 > dim[0]) { x1 = dim[0]; } if (-y1 < 0) { y1 = 0; } else if (-y1 > dim[1]) { y1 = -dim[1]; } if (x2 < 0) { x2 = 0; } else if (x2 > dim[0]) { x2 = dim[0]; } if (-y2 < 0) { y2 = 0; } else if (-y2 > dim[1]) { y2 = -dim[1]; } // Draw the rectangle float lw = lineWidths[i % lineWidths.length]; parent.fill(bgColors[i % bgColors.length]); parent.stroke(lineColors[i % lineColors.length]); parent.strokeWeight(lw); if (Math.abs(x2 - x1) > 2 * lw && Math.abs(y2 - y1) > 2 * lw) { parent.rect(x1, y1, x2, y2); } else if ((type == GPlot.VERTICAL && x2 != x1 && !(y1 == y2 && (y1 == 0 || y1 == -dim[1]))) || (type == GPlot.HORIZONTAL && y2 != y1 && !(x1 == x2 && (x1 == 0 || x1 == dim[0])))) { parent.rect(x1, y1, x2, y2); parent.line(x1, y1, x1, y2); parent.line(x2, y1, x2, y2); parent.line(x1, y1, x2, y1); parent.line(x1, y2, x2, y2); } } } parent.popStyle(); // Draw the labels if (drawLabels) { drawHistLabels(); } } }