一、Javafx Chart
JavaFX支持各种饼图和XY图表。在XY平面上表示的图表包括AreaChart,BarChart,BubbleChart,LineChart,ScatterChart,StackedAreaChart,StackedBarChart等。
注意:在Server JRE和无法初始化Javafx Toolkit的环境,无法导出图片。
二、Chart类型
(1)PieChart
(2)LineChart
(3)AreaChart
(4)BubbleChart
(5)BarChart
(6)ScatterChart
(7)StackedAreaChart
(8)StackedBarChart
三、Chart导出图片
import com.sun.javafx.application.PlatformImpl;
import java.io.File;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.Chart;
import javafx.scene.image.WritableImage;
import javax.imageio.ImageIO;public class ChartTest {public static void export() {//creating the chartfinal Chart chart = createChart();//防止坐标刻度不显示chart.setAnimated(false);//必须添加到场景,否则图表不绘制Scene scene = new Scene(chart, 800, 600);WritableImage writableImage = new WritableImage(800, (int) 600);chart.snapshot(new SnapshotParameters(), writableImage);File fileA = new File("./chart.png");try {ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null), "png", fileA);} catch (Exception s) {s.printStackTrace();}}public static Chart createChart(){Chart chart = null;//build chartreturn chart;}public static void main(String[] args) throws InterruptedException {PlatformImpl.startup(() -> {export();System.err.println("finish");System.exit(0);//退出程序});}
}