public void draw() {
        if (context != null) {
            double width;
            
            if(isPlacedOnLeftOrRight()){
                width = this.leftOrRightWidth;
            }else{
                width = this.topOrBottomWidth;
            }
            
            Rectangle2D legendArea = new Rectangle2D.Double(0, 0, getWidth(), getHeight());
            context.setStroke(legendBorder);
            context.setPaint(legendBackground);
            context.fill(legendArea);
            context.setPaint(legendBorderColor);
            context.draw(legendArea);
            
            updateSize();
            context.setFont(font);
            double moveTextVerticallyBy = context.getFontMetrics().getDescent();
            Rectangle2D marker;
            if (isPlacedOnLeftOrRight()) {                
                int i = 0;
                for (DataSeries dataSeries : series) {

                    marker = new Rectangle2D.Double(paddingHorizontal, paddingVertical + i * (markerDimension + verticalGap), markerDimension, markerDimension);

                    context.setStroke(markerBorder);

                    context.setPaint(dataSeries.getSeriesColor());
                    context.fill(marker);

                    context.setPaint(markerBorderColor);
                    context.draw(marker);

                    context.setPaint(textColor);
                    if (dataSeries.getName() != null) {
                        context.drawText(dataSeries.getName(), paddingHorizontal + markerDimension + horizontalGap, paddingVertical + i * (markerDimension + verticalGap) + markerDimension / 2, Context2D.HORIZONTAL_ALIGN_LEFT, Context2D.VERTICAL_ALIGN_MIDDLE);
                    }

                    i++;
                }
            } else {
                
                double maxLineWidth = width - 2 * paddingHorizontal;
                double currentY = paddingVertical;
                double currentWidthSum = 0;
                double currentX = paddingHorizontal;
                double currentLineMaxHeight = 0;
                double entryWidth;
                String name;
                for (DataSeries dataSeries : series) {
                    name = dataSeries.getName();
                    if (name != null) {
                        entryWidth = markerDimension + horizontalGap + context.calculateStringWidth(name);
                        currentWidthSum += entryWidth;
                        if (currentWidthSum > maxLineWidth) {
                            currentY += currentLineMaxHeight+verticalGap;
                            currentWidthSum = entryWidth;
                            currentX = paddingHorizontal;
                        }
                        
                        marker = new Rectangle2D.Double(currentX, currentY, markerDimension, markerDimension);
                        context.setStroke(markerBorder);

                        context.setPaint(dataSeries.getSeriesColor());
                        context.fill(marker);

                        context.setPaint(markerBorderColor);
                        context.draw(marker);
                        
                        context.setPaint(textColor);
                        context.drawText(name, currentX + markerDimension + horizontalGap, currentY + markerDimension - moveTextVerticallyBy, Context2D.HORIZONTAL_ALIGN_LEFT, Context2D.VERTICAL_ALIGN_BASELINE);
                        
                        currentLineMaxHeight = Math.max(context.getLineHeight(font, name), currentLineMaxHeight);
                        currentX += entryWidth+4*horizontalGap;
                        currentWidthSum += 4*horizontalGap;
                    }
                    
                }
                
            }
            
        }
    }