


How do I rotate the coordinate plane and y-axis labels in Java for data visualization?
Rotating Coordinate Plane for Data and Text in Java
This issue deals with challenges in plotting data points and rotating labels on the y-axis of a data plot. The provided code defines basic structures for plotting data and labels, but it encounters two difficulties:
- Data points are plotted incorrectly due to the origin and y-axis orientation.
- Y-axis labels are not visible on the screen.
Resolving the Issues
To address these issues, let's focus on the following modifications:
1. Rotating the Coordinate Plane
We can rotate the coordinate plane to align with the new origin (bottom-left corner of the blue rectangle) and invert the y-axis using the following code:
<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. Plotting Data
We adjust the data to fit the rotated coordinate plane by multiplying it by a scalar:
<code class="java">double Scalar = blueWidth/maxPlot; ArrayList<Double> scaledDiffs = new ArrayList<>(); for(int e = 0;e<myDiffs.size();e++){scaledDiffs.add(myDiffs.get(e)*Scalar);}</code>
3. Rotating Text for Y-Axis Labels
To rotate labels on the y-axis, we use the following code:
<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>
Complete Code
The modified code with these adjustments should resolve the issues:
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 < scaledDiffs.size(); w++) { if (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 }</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>
Additional Resource
For further information on graphics transformations, refer to:
- [Java 2D Graphics](https://docs.oracle.com/javase/8/docs/technotes/guides/2d/index.html)
The above is the detailed content of How do I rotate the coordinate plane and y-axis labels in Java for data visualization?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
