private void updateSize() {
        double width;
        
        if (isPlacedOnLeftOrRight()) {
            width = this.leftOrRightWidth;
            if (context != null) {

                context.setFont(font);
                String longestName = Utilities.calculateWidestSeriesName(context, series);
                context.adjustFontSizeToFitTextInWidth(longestName, width - 2 * paddingHorizontal - horizontalGap);
                markerDimension = context.getStandardLineHeight();
                context.adjustFontSizeToFitTextInWidth(longestName, width - 2 * paddingHorizontal - horizontalGap - markerDimension);
                markerDimension = context.getStandardLineHeight();
                height = series.size() * (markerDimension + verticalGap) + 2 * paddingVertical;
                font = context.getFont();
            }
        } else {
            width = topOrBottomWidth;
            if (context != null) {
                context.setFont(font);
                String longestName = Utilities.calculateWidestSeriesName(context, series);

                double maxLineWidth = width - 2 * paddingHorizontal;
                double totalHeight = paddingVertical;
                markerDimension = context.getStandardLineHeight();
                double currentWidthSum = 0;
                double currentLineMaxHeight = 0;

                double entryWidth = markerDimension + horizontalGap + context.calculateStringWidth(longestName);
                if (entryWidth > maxLineWidth) {
                    context.adjustFontSizeToFitTextInWidth("   " + longestName, width - 2 * paddingHorizontal);
                    markerDimension = context.getStandardLineHeight();
                }
                String name;
                boolean first = true;
                for (Iterator<DataSeries> it = series.iterator(); it.hasNext();) {
                    DataSeries dataSeries = it.next();
                    name = dataSeries.getName();
                    if (name != null) {
                        if(first){
                            first = false;
                            totalHeight += context.getLineHeight(font, name)+verticalGap;
                        }
                        entryWidth = markerDimension + horizontalGap + context.calculateStringWidth(name);
                        currentWidthSum += entryWidth;
                        currentLineMaxHeight = Math.max(context.getLineHeight(font, name), currentLineMaxHeight);
                        if (currentWidthSum > maxLineWidth) {
                            totalHeight += currentLineMaxHeight;
                            if(it.hasNext()){
                                totalHeight += verticalGap;
                            }
                            currentWidthSum = entryWidth;
                        }
                        currentWidthSum += 4*horizontalGap;
                        
                    }
                }
                font = context.getFont();
                totalHeight += paddingVertical;
                height = totalHeight;
            }
        }
    }