diff -r 240636b0c18c modules/controls/src/main/java/com/sun/javafx/charts/Legend.java --- a/modules/controls/src/main/java/com/sun/javafx/charts/Legend.java Thu Sep 26 14:14:55 2013 +1200 +++ b/modules/controls/src/main/java/com/sun/javafx/charts/Legend.java Mon Sep 30 16:56:32 2013 -0700 @@ -139,7 +139,7 @@ if(height == -1) { if(columns <= 1) return tileSize.getWidth() + snappedLeftInset() + snappedRightInset(); } else { - rows = (int) Math.floor( contentHeight / (tileSize.getHeight() + GAP) ); + rows = (int) Math.floor( (contentHeight + GAP)/ (tileSize.getHeight() + GAP) ); columns = (rows == 0) ? (int)Math.ceil(getItems().size()) : (int)Math.ceil(getItems().size() / (double)rows); } @@ -154,7 +154,7 @@ if(width == -1) { if(rows <= 1) return tileSize.getHeight() + snappedTopInset() + snappedBottomInset(); } else { - columns = (int) Math.floor( contentWidth / (tileSize.getWidth() + GAP) ); + columns = (int) Math.floor( (contentWidth+GAP) / (tileSize.getWidth() + GAP) ); rows = (columns == 0) ? (int)Math.ceil(getItems().size()) : (int)Math.ceil(getItems().size() / (double)columns); } diff -r 240636b0c18c modules/controls/src/main/java/javafx/scene/chart/Chart.java --- a/modules/controls/src/main/java/javafx/scene/chart/Chart.java Thu Sep 26 14:14:55 2013 +1200 +++ b/modules/controls/src/main/java/javafx/scene/chart/Chart.java Mon Sep 30 16:56:32 2013 -0700 @@ -350,10 +350,15 @@ if (legend != null) { boolean shouldShowLegend = isLegendVisible(); if (shouldShowLegend) { + // Fix for RT-32573 - calling legend.PrefWidth/prefHeight forces the columns/rows count + // to update accurately. Ideally we would have a better, more dynamic way of calculating + // the row/column fields in the Legend class, rather than just during the pref size calculations. if (getLegendSide().equals(Side.TOP)) { final double legendHeight = snapSize(legend.prefHeight(width-left-right)); - final double legendWidth = snapSize(legend.prefWidth(-1)); - legend.resizeRelocate(left + (((width - left - right)-legendWidth)/2), top, legendWidth, legendHeight); + final double legendWidth = Math.min(width-left-right, snapSize(legend.prefWidth(legendHeight))); + legend.prefHeight(legendWidth); + final double x = Math.max(0, left + (((width - left - right) - legendWidth) / 2.0)); + legend.resizeRelocate(x, top, legendWidth, legendHeight); if ((height - bottom - top - legendHeight) < MIN_HEIGHT_TO_LEAVE_FOR_CHART_CONTENT) { shouldShowLegend = false; } else { @@ -361,8 +366,10 @@ } } else if (getLegendSide().equals(Side.BOTTOM)) { final double legendHeight = snapSize(legend.prefHeight(width-left-right)); - final double legendWidth = snapSize(legend.prefWidth(-1)); - legend.resizeRelocate(left + (((width - left - right)-legendWidth)/2), height-bottom-legendHeight, legendWidth, legendHeight); + final double legendWidth = Math.min(width-left-right, snapSize(legend.prefWidth(legendHeight))); + legend.prefHeight(legendWidth); + final double x = Math.max(0, left + (((width - left - right) - legendWidth) / 2.0)); + legend.resizeRelocate(x, height-bottom-legendHeight, legendWidth, legendHeight); if ((height - bottom - top - legendHeight) < MIN_HEIGHT_TO_LEAVE_FOR_CHART_CONTENT) { shouldShowLegend = false; } else { @@ -370,7 +377,8 @@ } } else if (getLegendSide().equals(Side.LEFT)) { final double legendWidth = snapSize(legend.prefWidth(height-top-bottom)); - final double legendHeight = snapSize(legend.prefHeight(-1)); + final double legendHeight = Math.min(height-top-bottom, snapSize(legend.prefHeight(legendWidth))); + legend.prefWidth(legendHeight); legend.resizeRelocate(left,top +(((height-top-bottom)-legendHeight)/2),legendWidth,legendHeight); if ((width - left - right - legendWidth) < MIN_WIDTH_TO_LEAVE_FOR_CHART_CONTENT) { shouldShowLegend = false; @@ -379,7 +387,8 @@ } } else if (getLegendSide().equals(Side.RIGHT)) { final double legendWidth = snapSize(legend.prefWidth(height-top-bottom)); - final double legendHeight = snapSize(legend.prefHeight(-1)); + final double legendHeight = Math.min(height-top-bottom, snapSize(legend.prefHeight(legendWidth))); + legend.prefWidth(legendHeight); legend.resizeRelocate(width-right-legendWidth,top +(((height-top-bottom)-legendHeight)/2),legendWidth,legendHeight); if ((width - left - right - legendWidth) < MIN_WIDTH_TO_LEAVE_FOR_CHART_CONTENT) { shouldShowLegend = false;