📄 vbarchart3dbean.java
字号:
int stringLength = 0, stringHeight = 0;
// 横轴上刻度值的数量
int xTickCounter = category.length;
// 图表绘制区和纵轴之间的空白
int plotMargin = plotWidth / (2 * xTickCounter);
// 横轴上刻度值之间的长度
int hSpace;
if (xTickCounter == 1)
{
hSpace = plotWidth;
}
else
{
hSpace = (plotWidth - 2 * plotMargin) / (xTickCounter - 1);
}
// 每组直方图之间的空白
int barOuterMargin = 20;
// 每组直方图中,每个直方块之间的空白
int barInnerMargin = 5;
// 设定直方块的绘制宽度
int barLength =
(hSpace - barOuterMargin - series.length * barInnerMargin) / series.length;
stringHeight = g2d.getFontMetrics().getAscent();
for (int i = 0; i < xTickCounter; i++)
{
// 设置横轴上刻度绘制的横坐标
int xTickPosition = plotMargin + i * hSpace;
// 绘制垂直方向虚线
g2d.setColor(new Color(103, 115, 133));
// 绘制垂直方向虚线
drawVerticalLine(xTickPosition, basePointY, thickness,
alpha, plotHeight, g2d);
// 绘制横轴上的斜线
drawSlantLine(xTickPosition, basePointY, thickness, alpha, g2d);
// 绘制横轴上刻度值的说明文字
stringLength = g2d.getFontMetrics().stringWidth(category[i]);
g2d.setColor(new Color(246, 213, 164));
g2d.drawString(category[i], xTickPosition - stringLength / 2,
plotHeight + stringHeight + 5);
}
String str = "";
str = "";
// 纵轴上刻度值的数量
int vTickCounter = 10;
// 纵轴上刻度值之间的空白
int vSpace = plotHeight / vTickCounter;
for (int i = 0; i < vTickCounter; i++)
{
// 绘制水平方向虚线
g2d.setColor(new Color(103, 115, 133));
// g2d.drawLine(0, i * vSpace, plotWidth, i * vSpace);
// 绘制水平方向虚线
drawHorizontalLine(topLeftPointX, i * vSpace, thickness,
alpha, plotWidth, g2d);
// 绘制纵轴上的斜线
drawSlantLine(topLeftPointX, i * vSpace, thickness, alpha, g2d);
// 绘制纵轴上刻度值的说明文字
str += plotHeight - i * vSpace;
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.setColor(new Color(246, 213, 164));
g2d.drawString(str, - 5-stringLength, i * vSpace + stringHeight / 2);
str = "";
}
double[]sales = new double[series.length];
double totalBarLength = series.length * (barLength + barInnerMargin);
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
g2d.setStroke(new BasicStroke());
// 打开反锯齿功能,用于平滑处理3D矩形
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// 创建AlphaComposite对象,并设定透明度
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
alphaComposite);
g2d.setComposite(ac);
for (int i = 0; i < xTickCounter; i++)
{
// 初始化绘制数据
int bookSales = 0;
for (int j = 0; j < series.length; j++)
{
sales[j] = plotHeight - (65+(int)(Math.random() * 220));
// 计算直方块的绘制横坐标
double xVBarPosition = plotMargin + i * hSpace - totalBarLength / 2 +
j * (barLength + barInnerMargin);
g2d.setColor(paintColor[j % paintColor.length]);
// 初始化3D矩形的绘制参数
parallelogram.setBasePoint((int)xVBarPosition, basePointY);
parallelogram.setWidth(barLength);
parallelogram.setHeight((int)(plotHeight - sales[j]));
parallelogram.setThickness(thickness);
parallelogram.setAngle(alpha);
// 填充3D矩形
parallelogram.setFillColor(paintColor[j % paintColor.length]);
parallelogram.drawParallelogram(2, g2d);
// 描绘3D矩形轮廓
parallelogram.setOutlineColor(Color.BLACK);
parallelogram.drawParallelogram(1, g2d);
}
}
// 调用AlphaComposite的静态变量Src, 重新定义AlphaCompoiste对象
ac = AlphaComposite.Src;
g2d.setComposite(ac);
// 重新关闭反锯齿功能
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
// 恢复以前的AffineTransform对象
g2d.setTransform(old);
// 绘制坐标轴图例
drawLegend(series);
}
// 绘制图例
public void drawLegend(String[]legendItems)
{
g2d.setStroke(new BasicStroke());
g2d.setFont(new Font("宋体", Font.PLAIN, 12));
g2d.setPaint(Color.WHITE);
int itemHeight = g2d.getFontMetrics().getAscent();
int itemWidth[] = new int[legendItems.length];
// 图例的长度
int legendWidth = 20;
for (int i = 0; i < legendItems.length; i++)
{
itemWidth[i] = g2d.getFontMetrics().stringWidth(legendItems[i]);
if (i != legendItems.length - 1)
{
legendWidth += itemWidth[i] + 20;
}
else
{
legendWidth += itemWidth[i] + 15;
}
}
g2d.fillRect((width - legendWidth) / 2, height - 25, legendWidth, itemHeight + 5);
g2d.setColor(Color.BLACK);
g2d.drawRect((width - legendWidth) / 2, height - 25, legendWidth, itemHeight + 5);
int itemAnchor = (width - legendWidth) / 2+10;
g2d.setColor(Color.BLACK);
for (int i = 0; i < legendItems.length; i++)
{
g2d.setColor(paintColor[i % paintColor.length]);
g2d.fillRect(itemAnchor, height - 20, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(legendItems[i], itemAnchor + 15, height - 12);
itemAnchor += itemWidth[i] + 20;
}
}
// 绘制坐标轴标题
public void drawAxisLabel()
{
g2d.setFont(new Font("楷体_GB2312", Font.BOLD, 18));
g2d.setPaint(new Color(242, 159, 25));
int labelHeight = g2d.getFontMetrics().getAscent();
int labelLength = g2d.getFontMetrics().stringWidth(rangeAxisLabel);
// 获得当前的AffineTransform对象
AffineTransform old = g2d.getTransform();
g2d.translate(40, (height + labelLength) / 2);
g2d.rotate( - Math.PI / 2);
g2d.drawString(rangeAxisLabel, 0, 0);
// 恢复以前的AffineTransform对象
g2d.setTransform(old);
labelLength = g2d.getFontMetrics().stringWidth(domainAxisLabel);
g2d.drawString(domainAxisLabel, (width - labelLength) / 2, 370);
}
public void getChart()
{
this.initialize();
this.drawBackground();
this.drawTitle();
this.drawChart();
this.drawAxisLabel();
// 部署图形环境
g2d.dispose();
// 输出图像到WEB页面
try
{
this.response.reset();
this.response.setContentType("image/png");
OutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
}
catch (Exception e)
{
System.out.println("图表输出错误!");
}
}
}
/**************************************************************************
* (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors of this code have used their *
* best efforts in preparing the code. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these codes. The authors *
* shall not be liable in any event for incidental or consequential *
* damages in connection with, or arising out of, the furnishing, *
* performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -