java中的Axis OX和OY标题

时间:2014-12-28 22:52:08

标签: java graph draw

我有这段代码负责创建轴,不知道如何以及在何处实现轴的标题。我有一个关于X轴的问题,因为现在在轴上显示最小和最大值图表看起来很糟糕我如何更改它以列出更高和更低的值,以便图形将具有“中间”和不在轴上。感谢帮助!

 // create hatch marks and grid lines for y axis.
    for (int i = 0; i < numberYDivisions + 1; i++) {
        int x0 = padding + labelPadding;
        int x1 = pointWidth + padding + labelPadding;
        int y0 = getHeight() - ((i * (getHeight() - padding * 2 - labelPadding)) / numberYDivisions + padding + labelPadding);
        int y1 = y0;
        if (scoresY.size() > 0) {
            g2.setColor(gridColor);
            g2.drawLine(padding + labelPadding + 1 + pointWidth, y0, getWidth() - padding, y1);
            g2.setColor(Color.BLACK);
            String yLabel = (getMinScore() + (getMaxScore() - getMinScore()) * ((i * 100.0) / numberYDivisions)) + "";
            // String yLabel = scoresY.get(i) + "" ;
            FontMetrics metrics = g2.getFontMetrics();
            int labelWidth = metrics.stringWidth(yLabel);
            g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3);
        }
        g2.drawLine(x0, y0, x1, y1);
    }

    // and for x axis
    for (int i = 0; i < scoresX.size(); i++) {
        if (scoresX.size() > 1) {
            int x0 = i * (getWidth() - padding * 2 - labelPadding) / (scoresX.size() - 1) + padding + labelPadding;
            int x1 = x0;
            int y0 = getHeight() - padding - labelPadding;
            int y1 = y0 - pointWidth;
            if ((i % ((int) ((scoresX.size() / 10.0)))) == 0) {
                g2.setColor(gridColor);
                g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding);
                g2.setColor(Color.BLACK);
                String xLabel = scoresX.get(i) + "" ;
                FontMetrics metrics = g2.getFontMetrics();
                int labelWidth = metrics.stringWidth(xLabel);
                g2.drawString(xLabel, x0, y0 + metrics.getHeight());


            }
            g2.drawLine(x0, y0, x1, y1);
        }
    }

    // create x and y axes 
    g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
    g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);
    Stroke oldStroke = g2.getStroke();
    g2.setColor(lineColor);
    g2.setStroke(GRAPH_STROKE);
    for (int i = 0; i < graphPoints.size() - 1; i++) {
        int x1 = graphPoints.get(i).x;
        int y1 = graphPoints.get(i).y;
        int x2 = graphPoints.get(i + 1).x;
        int y2 = graphPoints.get(i + 1).y;
        g2.drawLine(x1, y1, x2, y2);

    }
    // and for x axis
    for (int i = 0; i < scoresX.size(); i++) {
        if (scores> 1) {
            ...
            if (....) {

                g2.drawLine(x0, getHeight() - padding - labelPadding - 1 - pointWidth, x1, padding);
                g2.setColor(Color.BLACK);
                String xLabel = scoresX.get(i) + "" ;
                FontMetrics metrics = g2.getFontMetrics();
                int labelWidth = metrics.stringWidth(xLabel);
                g2.drawString(xLabel, x0, y0 + metrics.getHeight());


            }
            g2.drawLine(x0, y0, x1, y1);
        }
    }

    // create x and y axes 
    g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
    g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);
    Stroke oldStroke = g2.getStroke();
    g2.setColor(lineColor);
    g2.setStroke(GRAPH_STROKE);
    for (....) {
        int x1 = ...
        int y1 = ...
        int x2 = ....
        int y2 = ...
        g2.drawLine(x1, y1, x2, y2);

    }*/

0 个答案:

没有答案