java 五边形雷达图
时间: 2023-10-13 20:07:18 浏览: 81
Java中可以用JavaFX库来绘制五边形雷达图。以下是一个简单的示例代码:
```
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class RadarChart extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Canvas canvas = new Canvas(600, 600);
root.getChildren().add(canvas);
Scene scene = new Scene(root, 600, 600, Color.WHITE);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.BLACK);
gc.setLineWidth(1);
int centerX = 300;
int centerY = 300;
int radius = 200;
int sides = 5;
double angle = Math.toRadians(360.0 / sides);
double[] values = {0.8, 0.6, 0.4, 0.2, 0.7};
// 绘制五边形
for (int i = 0; i < sides; i++) {
double x = centerX + radius * Math.sin(i * angle);
double y = centerY - radius * Math.cos(i * angle);
gc.strokeLine(centerX, centerY, x, y);
}
gc.strokeLine(centerX + radius, centerY, centerX - radius, centerY);
// 绘制数据区域
gc.setFill(Color.BLUE);
gc.setGlobalAlpha(0.5);
for (int i = 0; i < sides; i++) {
double x = centerX + values[i] * radius * Math.sin(i * angle);
double y = centerY - values[i] * radius * Math.cos(i * angle);
if (i == 0) {
gc.beginPath();
gc.moveTo(x, y);
} else {
gc.lineTo(x, y);
}
}
gc.closePath();
gc.fill();
primaryStage.setTitle("Radar Chart");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
运行结果如下:
![Radar Chart](https://i.imgur.com/DxgWVX7.png)
阅读全文