protected void drawLegend(
            double[] regressionParameters) {
//        int[] result = new int[3];

        int newLegendWidth = 0;
        int newLegendHeight = 0;
//        int legendExtraWidthLeft = 0;
//        int legendExtraWidthRight = 0;
        int textHeight = getTextHeight();
        int legendExtraHeightBottom = textHeight;

        int legendStartRow = getDataEndRow() + getxAxisHeight();
//        int legendStartRow = this.dataEndRow + this.xAxisHeight / 2;
        int symbolRow;
        int row;
        int symbolCol;
        int col;
        int symbolWidth = 10;
        // Legend Title
        setPaint(Color.DARK_GRAY);
        int legendItemWidth = 0;

        String text = "Legend";
        int textWidth = getTextWidth(text);
        newLegendHeight += textHeight;
        row = legendStartRow + newLegendHeight;
        //col = dataStartCol - yAxisWidth;
        col = textHeight;
        legendItemWidth += textWidth;
        newLegendWidth = Math.max(newLegendWidth, legendItemWidth);
        drawString(
                text,
                col,
                row);
        Point2D.Double point = new Point2D.Double();

        // Point marker
        legendItemWidth = 0;
        newLegendHeight += textHeight;
        symbolRow = legendStartRow + newLegendHeight;
        legendItemWidth += symbolWidth;
        symbolCol = col + symbolWidth / 2;
        point.setLocation(
                symbolCol,
                symbolRow);
        setPaint(Color.DARK_GRAY);
        draw(point);
        row += ((3 * textHeight) / 2) - 2;
        newLegendHeight += (textHeight / 2) - 2;
        col += symbolWidth + 4;
        setPaint(Color.GRAY);
        text = "Data Point";
        textWidth = getTextWidth(text);
        legendItemWidth += textWidth;
        newLegendWidth = Math.max(newLegendWidth, legendItemWidth);
        drawString(
                text,
                col,
                row);
        // Y = X line
        setPaint(Color.LIGHT_GRAY);
        //int itemSymbolWidth = (symbolCol + symbolWidth / 2) - (symbolCol - symbolWidth / 2);
        //legendItemWidth = itemSymbolWidth;        
        //legendItemWidth = symbolWidth + 4;
        symbolRow += textHeight;
        draw(new Line2D.Double(
                symbolCol - symbolWidth / 2,
                (symbolRow + textHeight / 2) - 2,
                symbolCol + symbolWidth / 2,
                (symbolRow - textHeight / 2) + 2));
        setPaint(Color.GRAY);
        row += textHeight;
        text = "Y = X";
        textWidth = getTextWidth(text);
        drawString(
                text,
                col,
                row);
        legendItemWidth = textWidth;
        newLegendWidth = Math.max(newLegendWidth, legendItemWidth);
        newLegendHeight += textHeight + 4;

        // Regression line
        setPaint(Color.DARK_GRAY);
        legendItemWidth = symbolWidth + 4;
        symbolRow += textHeight;
        row += textHeight;
        draw(new Line2D.Double(
                symbolCol - symbolWidth / 2,
                (symbolRow + textHeight / 2) - 2,
                symbolCol + symbolWidth / 2,
                (symbolRow - textHeight / 2) + 2));
        setPaint(Color.GRAY);
        // Y = mX + c
        // generalise m
        int scale = 4;
        BigDecimal m;
        if (Double.isNaN(regressionParameters[1])) {
            m = BigDecimal.ZERO;
        } else {
            m = BigDecimal.valueOf(regressionParameters[1]);
        }
        RoundingMode roundingMode = getRoundingMode();
        m = m.setScale(scale, roundingMode);
        m = m.stripTrailingZeros();
//        m = Generic_BigDecimal.roundStrippingTrailingZeros(
//                m, 
//                decimalPlacePrecision,
//                _RoundingMode);
        BigDecimal c;
        if (Double.isNaN(regressionParameters[0])) {
            c = BigDecimal.ZERO;
        } else {
            c = BigDecimal.valueOf(regressionParameters[0]);
        }
        c = c.setScale(scale, roundingMode);
        c = c.stripTrailingZeros();
        BigDecimal rsquare;
        if (Double.isNaN(regressionParameters[2])) {
            rsquare = BigDecimal.ZERO;
        } else {
            rsquare = BigDecimal.valueOf(regressionParameters[2]);
        }
        rsquare = rsquare.setScale(3, roundingMode);
        rsquare = rsquare.stripTrailingZeros();
        String equation;
        if (c.compareTo(BigDecimal.ZERO) != -1) {
            equation = "Y = (" + m + " * X) + " + c + "";
        } else {
            equation = "Y = (" + m + " * X) - " + c.negate() + "";
        }
        drawString(
                equation,
                col,
                row);
        textWidth = getTextWidth(equation);
        legendItemWidth += textWidth;
        newLegendWidth = Math.max(newLegendWidth, legendItemWidth);
        newLegendHeight += textHeight;

        // Rsquare component
        String rsquare_String = "RSquare = " + rsquare;
        textWidth = getTextWidth(rsquare_String);
        legendItemWidth = textWidth;
        row += textHeight;
        drawString(
                rsquare_String,
                col,
                row);
        //setLegendHeight(row - legendStartRow);
        newLegendWidth = Math.max(newLegendWidth, legendItemWidth);
        newLegendHeight += (2 * textHeight);

        int imageWidth = getImageWidth();
        int dataWidth = getDataWidth();
        int extraWidthLeft = getExtraWidthLeft();
        if (newLegendWidth > getLegendWidth()) {
            //int diff = newLegendWidth - legendWidth;
            if (newLegendWidth > imageWidth) {
                setImageWidth(newLegendWidth);
                setExtraWidthRight(newLegendWidth - extraWidthLeft - dataWidth);
            }
            setLegendWidth(newLegendWidth);
        }
        int extraHeightBottom = getExtraHeightBottom();
        if (newLegendHeight > getLegendHeight()) {
            //int diff = newLegendHeight - legendHeight;
            //int heightForLegend = legendStartRow - dataStartRow + newLegendHeight;
            int newExtraHeightBottom = newLegendHeight + getxAxisHeight();
            if (newExtraHeightBottom > extraHeightBottom) {
                int diff2 = newExtraHeightBottom - extraHeightBottom;
                setExtraHeightBottom(newExtraHeightBottom);
                setImageHeight(getImageHeight() + diff2);
            }
            setLegendHeight(newLegendHeight);
        }
//        result[0] = legendExtraWidthLeft;
//        result[1] = legendExtraWidthRight;
//        result[2] = legendExtraHeightBottom;

//        return result;

    }