Draw a bar at each point
(PlotObject plotObject)
| 3484 | |
| 3485 | /** Draw a bar at each point */ |
| 3486 | void drawBarChart(PlotObject plotObject) { |
| 3487 | int n = Math.min(plotObject.xValues.length, plotObject.yValues.length); |
| 3488 | String[] xCats = labelsInBraces('x'); // do we have categories at the x axis instead of numbers? |
| 3489 | boolean separatedBars = plotObject.shape == SEPARATED_BAR || xCats != null; |
| 3490 | int halfBarWidthInPixels = n <= 1 ? Math.max(1, frameWidth/2-2) : 0; |
| 3491 | if (separatedBars && n > 1) |
| 3492 | halfBarWidthInPixels = Math.max(1, (int)Math.round(Math.abs |
| 3493 | (0.5*(plotObject.xValues[n-1] - plotObject.xValues[0])/(n-1) * xScale * SEPARATED_BAR_WIDTH))); |
| 3494 | int y0 = scaleYWithOverflow(0); |
| 3495 | boolean yZeroInFrame = !logYAxis && yBasePxl>frame.y && yBasePxl<frame.y+frame.height; |
| 3496 | int prevY = y0; |
| 3497 | for (int i = 0; i < n; i++) { |
| 3498 | int left=0, right=0; |
| 3499 | if (halfBarWidthInPixels == 0) { //bar boundaries in the middle between successive x values |
| 3500 | left = scaleX(i > 0 ? 0.5f*(plotObject.xValues[i-1]+plotObject.xValues[i]) : |
| 3501 | 1.5f*plotObject.xValues[i] - 0.5f*plotObject.xValues[i+1]); |
| 3502 | right = scaleX(i < n-1 ? 0.5f*(plotObject.xValues[i]+plotObject.xValues[i+1]) : |
| 3503 | 1.5f*plotObject.xValues[i] - 0.5f*plotObject.xValues[i-1]); |
| 3504 | } else { |
| 3505 | int x = scaleX(plotObject.xValues[i]); |
| 3506 | left = x - halfBarWidthInPixels; //separated bars or n<=1 : fixed bar width |
| 3507 | right = x + halfBarWidthInPixels; |
| 3508 | } |
| 3509 | if (left < frame.x) left = frame.x; |
| 3510 | if (left > frame.x+frame.width) left = frame.x+frame.width; |
| 3511 | if (right < frame.x) right = frame.x; |
| 3512 | if (right > frame.x+frame.width) right = frame.x+frame.width; |
| 3513 | int y = scaleYWithOverflow(plotObject.yValues[i]); |
| 3514 | if (plotObject.color2 != null) { |
| 3515 | ip.setColor(plotObject.color2); |
| 3516 | for (int x2 = Math.min(left,right); x2 <= Math.max(left,right); x2++) |
| 3517 | ip.drawLine(x2, y0, x2, y); //cant use ip.fillRect (ignores the clipRect), so we it fill line by line |
| 3518 | } |
| 3519 | ip.setColor(plotObject.color); |
| 3520 | ip.setLineWidth(sc(plotObject.lineWidth)); |
| 3521 | if (separatedBars) { |
| 3522 | ip.drawLine(left, y0, left, y); //up |
| 3523 | ip.drawLine(left, y, right, y); //right |
| 3524 | ip.drawLine(right, y, right, y0); //down |
| 3525 | if (yZeroInFrame) |
| 3526 | ip.drawLine(left, y0, right, y0);//baseline |
| 3527 | } else { |
| 3528 | ip.drawLine(left, prevY, left, y); //up or down |
| 3529 | ip.drawLine(left, y, right, y); //right |
| 3530 | if (i == n - 1) |
| 3531 | ip.drawLine(right, y, right, y0);//last down |
| 3532 | prevY = y; |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | /** Draw the symbol for the data point number 'pointIndex' (pointIndex < 0 when drawing the legend) */ |
| 3538 | @AstroImageJ(reason = "Restore lineWidth for dots to support bolded datum and MP style; custom plot shapes", modified = true) |
no test coverage detected