【问题标题】:Custom java BarChart Scaling issues自定义 java BarChart 缩放问题
【发布时间】:2018-01-01 18:11:37
【问题描述】:

我一直在尝试用 Java 为一个学校项目制作自定义 BarChart,但由于某种原因,它存在一些奇怪的缩放问题。这是代码。

static class BarChart extends JPanel
{
    private int[] chartValues;
    private String[] chartLabels;
    private String chartTitle;

    public BarChart(String title,int[] values,String[]labels)
    {
        this.chartTitle = title;
        this.chartValues = values;
        this.chartLabels = labels;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Random r = new Random();

        if(this.chartValues == null || this.chartValues.length==0)
        {
            return;
        }

        Dimension chartDimension = this.getSize();
        int panelWidth = chartDimension.width;
        int panelHeight = chartDimension.height;
        int barWidth = panelWidth / this.chartValues.length;
        int maxValue = this.chartValues[0];
        int minValue = this.chartValues[0];
        for(int tempValue:this.chartValues)
        {
            maxValue = Math.max(maxValue, tempValue);
            minValue = Math.min(minValue, tempValue);
        }

        Font titleFont = new Font("Book Antiqua", Font.BOLD, 15);
        FontMetrics titleFontMetrics  = g.getFontMetrics(titleFont);

        Font labelFont = new Font("Book Antiqua", Font.PLAIN, 14);
        FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

        int titleWidth = titleFontMetrics.stringWidth(this.chartTitle);
        int stringHeight = titleFontMetrics.getAscent();
        int stringWidth = (panelWidth - titleWidth) / 2;
        g.setFont(titleFont);
        g.drawString(this.chartTitle, stringWidth, stringHeight);

        int top = titleFontMetrics.getHeight();
        int bottom = labelFontMetrics.getHeight();

        if(maxValue==minValue)
        {
            return;
        }

        double barScale = (panelHeight - top - bottom)/(maxValue - minValue);

        stringHeight = panelHeight - labelFontMetrics.getDescent();

        int xPos = 5;
        g.setFont(labelFont);
        for(int i=0; i<this.chartValues.length;i++)
        {
            int tempValue = this.chartValues[i];
            int barHeight = (int) ( (double)tempValue * barScale);
            int yPos =  top;

            if(tempValue>=0)
            {
                yPos += (int) ((maxValue - tempValue)* barScale);
            }
            else
            {
                yPos += (int) (maxValue * barScale);
                barHeight = - barHeight;
            }

            g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));

            g.fillRect(xPos, yPos, barWidth, barHeight);
            g.setColor(Color.BLUE);
            g.drawRect(xPos, yPos, barWidth, barHeight);

            xPos += barWidth;
        }

        g.setColor(Color.BLACK);
        g.drawString("Xronos", stringWidth, stringHeight);


    }
}

但是当我使用值为 {1,5,4,7,120} 的 main 运行它时,我会根据屏幕分辨率得到这个。 Wrong image (the height between bars and label is too much). Correct height between bars and label。我真的很感激任何帮助。如果这个愚蠢的问题是一个问题,对不起。

【问题讨论】:

标签: java swing bar-chart


【解决方案1】:

你在这里做整数除法:

double barScale = (panelHeight - top - bottom)/(maxValue - minValue);

试试

double barScale = (panelHeight - top - bottom)/(double)(maxValue - minValue);

改为

【讨论】:

  • 是的,这就是问题所在,现在看来已经解决了。
猜你喜欢
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多