在 Java 中旋转数据和文本的坐标平面
此问题解决了在 y 轴上绘制数据点和旋转标签的挑战数据图。提供的代码定义了绘制数据和标签的基本结构,但遇到了两个困难:
- 由于原点和 y 轴方向,数据点绘制不正确。
- Y-轴标签在屏幕上不可见。
解决问题
为了解决这些问题,让我们重点进行以下修改:
1.旋转坐标平面
我们可以旋转坐标平面以与新原点(蓝色矩形的左下角)对齐,并使用以下代码反转 y 轴:
<code class="java">g2d.translate(leftStartPlotWindow, blueTop);//translate origin to bottom-left corner of blue rectangle g2d.scale(1, -1);//invert the y-axis</code>
2。绘制数据
我们通过将数据乘以标量来调整数据以适合旋转的坐标平面:
<code class="java">double Scalar = blueWidth/maxPlot; ArrayList<double> scaledDiffs = new ArrayList(); for(int e = 0;e<mydiffs.size><p><strong>3。旋转 Y 轴标签的文本</strong></p> <p>要在 y 轴上旋转标签,我们使用以下代码:</p> <pre class="brush:php;toolbar:false"><code class="java">g2d.rotate(Math.toRadians(-90), 0, 0);//rotate text 90 degrees counter-clockwise g.drawString(yString, -(height/2)-(yStrWidth/2), yStrHeight); g2d.rotate(Math.toRadians(+90), 0, 0);//rotate text 90 degrees clockwise</code>
完整代码
经过这些调整的修改后的代码应该可以解决以下问题:
DataGUI.java
<code class="java">//... Previous code omitted for brevity import java.awt.geom.AffineTransform; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; class DataGUI extends JFrame{ //... Previous code omitted for brevity @Override public void paint(Graphics g) { // ... Previous code omitted for brevity int height = getHeight(); int width = getWidth(); ins = getInsets(); // Obtain data about graphics environment and text Graphics2D g2d = (Graphics2D) g; FontMetrics fontMetrics = g2d.getFontMetrics(); String xString = "x-axis Label"; int xStrWidth = fontMetrics.stringWidth(xString); int xStrHeight = fontMetrics.getHeight(); // Set parameters for the inner rectangle int hPad = 10; int vPad = 6; int testLeftStartPlotWindow = ins.left + 5 + (3 * yStrHeight); int testInnerWidth = width - testLeftStartPlotWindow - ins.right - hPad; // Find minimum and maximum values getMaxMinDiffs(); getMaxPlotVal(); // Determine the maximum number of ticks for the axes double increment = 5.0; int numTicks = (int) (maxPlot / increment); int remainder = testInnerWidth % numTicks; int leftStartPlotWindow = testLeftStartPlotWindow - remainder; // Calculate the bottom padding and blue rectangle dimensions int bottomPad = (3 * xStrHeight) - vPad; int blueTop = ins.bottom + (vPad / 2) + titleStrHeight; int blueHeight = height - bottomPad - blueTop; int blueWidth = blueHeight; int blueBottom = blueHeight + blueTop; // Start drawing // ... Previous code omitted for brevity // Scale the diffs to fit the window double Scalar = blueWidth / maxPlot; List<double> scaledDiffs = new ArrayList(); for (Double myDiff : myDiffs) { scaledDiffs.add(myDiff * Scalar); } // Rotate the graphics context for plotting data AffineTransform at = g2d.getTransform(); g2d.translate(leftStartPlotWindow, blueTop); g2d.scale(1, -1); // Plot the scaled diffs for (int w = 0; w 0) { double prior = scaledDiffs.get(w - 1); int priorInt = (int) prior; double current = scaledDiffs.get(w); int currentInt = (int) current; g2d.drawOval(priorInt, currentInt, 4, 4); } } // Restore the transform for conventional rendering g2d.setTransform(at); // ... Rest of the code omitted for brevity } // ... Previous code omitted for brevity }</double></code>
DataPanel.java
<code class="java">//... Previous code omitted for brevity @Override protected void paintComponent(Graphics g) { //... Previous code omitted for brevity int blueTop = ins.bottom+(vPad/2)+titleStrHeight; int blueHeight = height-bottomPad-blueTop; int blueWidth = blueHeight; int blueBottom = blueHeight+blueTop; //... Previous code omitted for brevity // Rotate the graphics context for y-axis labels g2d.rotate(Math.toRadians(-90), 0, 0); g.drawString(yString, -(height/2)-(yStrWidth/2), yStrHeight); // Restore the graphics context for normal rendering g2d.rotate(Math.toRadians(+90), 0, 0); //... Previous code omitted for brevity }</code>
其他资源
有关图形转换的更多信息,请参阅:
- [Java 2D Graphics](https:// docs.oracle.com/javase/8/docs/technotes/guides/2d/index.html)
以上是如何在Java中旋转坐标平面和y轴标签以实现数据可视化?的详细内容。更多信息请关注PHP中文网其他相关文章!

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。

Java的平台独立性是指编写的代码可以在任何安装了JVM的平台上运行,无需修改。1)Java源代码编译成字节码,2)字节码由JVM解释执行,3)JVM提供内存管理和垃圾回收功能,确保程序在不同操作系统上运行。

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算显着提升了Java的平台独立性。 1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。 2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技术如Docker增强而非替代Java的平台独立性。1)确保跨环境的一致性,2)管理依赖性,包括特定JVM版本,3)简化部署过程,使Java应用更具适应性和易管理性。

JRE是Java应用程序运行的环境,其作用是让Java程序在不同操作系统上运行无需重新编译。JRE的工作原理包括JVM执行字节码、类库提供预定义类和方法、配置文件和资源文件设置运行环境。

JVM通过自动内存管理和垃圾回收确保Java程序高效运行。1)内存分配:为新对象在堆中分配内存。2)引用计数:跟踪对象引用,检测垃圾。3)垃圾回收:使用标记-清除、标记-整理或复制算法回收不再引用的对象。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6
视觉化网页开发工具