java中的条形图

发布于 2024-11-27 00:54:10 字数 3040 浏览 1 评论 0原文

我想更改每个条形的高度(例如红色部分为 10,蓝色部分为 20)。但是当我增加高度值时,它会从底部增加图表,而我希望更改到顶部!你知道这有什么问题吗?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChartPanel extends JPanel {
  private double[] values;
  private String[] names;
  private String title;

public ChartPanel(double[] v, String[] n, String t) {
 names = n;
 values = v;
 title = t;
}

public void paintComponent(Graphics g) {
 super.paintComponent(g);
 if (values == null || values.length == 0)
  return;
 double minValue = 0;
 double maxValue = 0;
 for (int i = 0; i < values.length; i++) {
   if (minValue > values[i])
     minValue = values[i];
   if (maxValue < values[i])
     maxValue = values[i];
  }

Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;

Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);

int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
  return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);

for (int i = 0; i < values.length; i++) {
  int valueX = i * barWidth + 1;
  int valueY = top;
  int height = (int) (values[i] * scale);
  if (values[i] >= 0)
    valueY += (int) ((maxValue - values[i]) * scale);
  else {
    valueY += (int) (maxValue * scale);
    height = -height;
  }

  g.setColor(Color.red);
  g.fillRect(valueX, valueY, barWidth - 80, height);
  g.setColor(Color.black);
  g.drawRect(valueX, valueY, barWidth - 80, height);


  g.setColor(Color.blue);
  g.fillRect(valueX, valueY + 20, barWidth - 80, height-20 );
  g.setColor(Color.black);
  g.drawRect(valueX, valueY + 20, barWidth - 80, height-20 );
  int labelWidth = labelFontMetrics.stringWidth(names[i]);
  x = i * barWidth + (barWidth - labelWidth) / 2;
  g.drawString(names[i], x, y);
 }
}

public static void main(String[] argv) {
JFrame f = new JFrame();
f.setSize(400, 300);
double[] values = new double[3];
String[] names = new String[3];
values[0] = 1;
names[0] = "Item 1";

values[1] = 2;
names[1] = "Item 2";

values[2] = 4;
names[2] = "Item 3";

f.getContentPane().add(new ChartPanel(values, names, "title"));

WindowListener wndCloser = new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
};
   f.addWindowListener(wndCloser);
   f.setVisible(true);
 }
}

I want to change the height of each bar (for example 10 for red part and 20 for blue part). But when I increase the height value it will increase the chart from bottom whereas I want the change to top! Do you know what is wrong with it?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ChartPanel extends JPanel {
  private double[] values;
  private String[] names;
  private String title;

public ChartPanel(double[] v, String[] n, String t) {
 names = n;
 values = v;
 title = t;
}

public void paintComponent(Graphics g) {
 super.paintComponent(g);
 if (values == null || values.length == 0)
  return;
 double minValue = 0;
 double maxValue = 0;
 for (int i = 0; i < values.length; i++) {
   if (minValue > values[i])
     minValue = values[i];
   if (maxValue < values[i])
     maxValue = values[i];
  }

Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;

Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);

int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);

int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
  return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);

for (int i = 0; i < values.length; i++) {
  int valueX = i * barWidth + 1;
  int valueY = top;
  int height = (int) (values[i] * scale);
  if (values[i] >= 0)
    valueY += (int) ((maxValue - values[i]) * scale);
  else {
    valueY += (int) (maxValue * scale);
    height = -height;
  }

  g.setColor(Color.red);
  g.fillRect(valueX, valueY, barWidth - 80, height);
  g.setColor(Color.black);
  g.drawRect(valueX, valueY, barWidth - 80, height);


  g.setColor(Color.blue);
  g.fillRect(valueX, valueY + 20, barWidth - 80, height-20 );
  g.setColor(Color.black);
  g.drawRect(valueX, valueY + 20, barWidth - 80, height-20 );
  int labelWidth = labelFontMetrics.stringWidth(names[i]);
  x = i * barWidth + (barWidth - labelWidth) / 2;
  g.drawString(names[i], x, y);
 }
}

public static void main(String[] argv) {
JFrame f = new JFrame();
f.setSize(400, 300);
double[] values = new double[3];
String[] names = new String[3];
values[0] = 1;
names[0] = "Item 1";

values[1] = 2;
names[1] = "Item 2";

values[2] = 4;
names[2] = "Item 3";

f.getContentPane().add(new ChartPanel(values, names, "title"));

WindowListener wndCloser = new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
};
   f.addWindowListener(wndCloser);
   f.setVisible(true);
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2024-12-04 00:54:10

因为Java的图形以左上角为原点,所以当你添加高度时,它会向下而不是向上增加。尝试更改此:

  g.setColor(Color.red);
  g.fillRect(valueX, valueY, barWidth - 80, height);
  g.setColor(Color.black);
  g.drawRect(valueX, valueY, barWidth - 80, height);

为此:

  g.setColor(Color.red);
  g.fillRect(valueX, valueY - 20, barWidth - 80, height);
  g.setColor(Color.black);
  g.drawRect(valueX, valueY - 20, barWidth - 80, height);

我尝试了此并将其添加到顶部栏的红色部分。

这是原始代码的截图:

在此处输入图像描述

更改后:

在此处输入图像描述

Because Java's graphics uses the top left corner as the origin, when you add to the height it will increase down instead of up. Try changing this:

  g.setColor(Color.red);
  g.fillRect(valueX, valueY, barWidth - 80, height);
  g.setColor(Color.black);
  g.drawRect(valueX, valueY, barWidth - 80, height);

To this:

  g.setColor(Color.red);
  g.fillRect(valueX, valueY - 20, barWidth - 80, height);
  g.setColor(Color.black);
  g.drawRect(valueX, valueY - 20, barWidth - 80, height);

I tried this and it added to the red portion of the bar at the top.

Here is a shot of the original code:

enter image description here

And with the change:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文