当前位置: 首页 > news >正文

石家庄做网站建设的公司网站源码下载教程

石家庄做网站建设的公司,网站源码下载教程,淘宝网站建设维护会计科目,全国建筑人员证书查询在现代应用程序开发中,数据可视化是一个关键部分。本文将介绍如何使用 Java Swing 和 XChart 库创建各种类型的图表。XChart 是一个轻量级的图表库,支持多种类型的图表,非常适合在 Java 应用中进行快速的图表绘制。 1、环境配置 在开始之前&…

在现代应用程序开发中,数据可视化是一个关键部分。本文将介绍如何使用 Java Swing 和 XChart 库创建各种类型的图表。XChart 是一个轻量级的图表库,支持多种类型的图表,非常适合在 Java 应用中进行快速的图表绘制。

1、环境配置

在开始之前,我们需要确保项目中包含了 XChart 的依赖。以下是在 Maven 项目中的 pom.xml 文件中添加 XChart 依赖的方法:

<dependency><groupId>org.knowm.xchart</groupId><artifactId>xchart</artifactId><version>3.8.0</version>
</dependency>

2、创建不同类型的图表

以下是如何使用 XChart 创建不同类型的图表的示例代码。

面积图


public class AreaChartExample extends JFrame {public AreaChartExample() {// 创建图表XYChart chart = new XYChartBuilder().width(800).height(600).title("Area Chart Example").xAxisTitle("X").yAxisTitle("Y").build();// 添加数据double[] xData = new double[] {0.0, 1.0, 2.0, 3.0, 4.0};double[] yData = new double[] {2.0, 1.0, 0.0, 1.0, 2.0};chart.addSeries("y(x)", xData, yData).setMarker(SeriesMarkers.NONE).setFillColor(new Color(0, 0, 255, 50));// 将图表面板添加到 JFrameJPanel chartPanel = new XChartPanel<>(chart);getContentPane().add(chartPanel, BorderLayout.CENTER);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> {AreaChartExample example = new AreaChartExample();example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);example.pack();example.setVisible(true);});}
}

柱状图

public class BarChartExample extends JFrame {public BarChartExample() {// 创建图表CategoryChart chart = new CategoryChartBuilder().width(800).height(600).title("Bar Chart Example").xAxisTitle("Category").yAxisTitle("Value").build();// 添加数据List<String> categories = Arrays.asList("A", "B", "C");List<Number> series1Values = Arrays.asList(4, 5, 9);List<Number> series2Values = Arrays.asList(3, 8, 5);chart.addSeries("Series 1", categories, series1Values);chart.addSeries("Series 2", categories, series2Values);// 将图表面板添加到 JFrameJPanel chartPanel = new XChartPanel<>(chart);getContentPane().add(chartPanel, BorderLayout.CENTER);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> {BarChartExample example = new BarChartExample();example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);example.pack();example.setVisible(true);});}
}

气泡图

public BubbleChartExample() {// 创建图表XYChart chart = new XYChartBuilder().width(800).height(600).title("Bubble Chart Example").xAxisTitle("X").yAxisTitle("Y").build();// 添加数据double[] xData = new double[] {0.1, 0.4, 1.0, 1.5, 2.2, 3.0};double[] yData = new double[] {2.0, 1.5, 0.9, 1.4, 1.9, 2.2};double[] bubbleSize = new double[] {0.1, 0.2, 0.3, 0.4, 0.5, 0.6};chart.addSeries("y(x)", xData, yData, bubbleSize).setMarker(SeriesMarkers.CIRCLE);// 将图表面板添加到 JFrameJPanel chartPanel = new XChartPanel<>(chart);getContentPane().add(chartPanel, BorderLayout.CENTER);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> {BubbleChartExample example = new BubbleChartExample();example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);example.pack();example.setVisible(true);});}

折线图

public class LineChartExample extends JFrame {public LineChartExample() {// 创建图表XYChart chart = new XYChartBuilder().width(800).height(600).title("Line Chart Example").xAxisTitle("X").yAxisTitle("Y").build();// 自定义图表样式chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNE);chart.getStyler().setMarkerSize(16);// 添加数据double[] xData = new double[] {0.0, 1.0, 2.0, 3.0, 4.0};double[] yData = new double[] {2.0, 1.0, 0.0, 1.0, 2.0};chart.addSeries("y(x)", xData, yData);// 将图表面板添加到 JFrameJPanel chartPanel = new XChartPanel<>(chart);getContentPane().add(chartPanel, BorderLayout.CENTER);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> {LineChartExample example = new LineChartExample();example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);example.pack();example.setVisible(true);});}
}

饼图

public class PieChartExample extends JFrame {public PieChartExample() {// 创建图表PieChart chart = new PieChartBuilder().width(800).height(600).title("Pie Chart Example").build();// 添加数据chart.addSeries("Category A", 40);chart.addSeries("Category B", 30);chart.addSeries("Category C", 20);chart.addSeries("Category D", 10);// 将图表面板添加到 JFrameJPanel chartPanel = new XChartPanel<>(chart);getContentPane().add(chartPanel, BorderLayout.CENTER);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> {PieChartExample example = new PieChartExample();example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);example.pack();example.setVisible(true);});}
}

阶梯图

public class StepChartExample extends JFrame {public StepChartExample() {// 创建图表XYChart chart = new XYChartBuilder().width(800).height(600).title("Step Chart Example").xAxisTitle("X").yAxisTitle("Y").build();// 添加数据double[] xData = new double[] {0.0, 1.0, 2.0, 3.0, 4.0};double[] yData = new double[] {2.0, 1.0, 0.0, 1.0, 2.0};chart.addSeries("y(x)", xData, yData).setXYSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Step);// 将图表面板添加到 JFrameJPanel chartPanel = new XChartPanel<>(chart);getContentPane().add(chartPanel, BorderLayout.CENTER);}public static void main(String[] args) {SwingUtilities.invokeLater(() -> {StepChartExample example = new StepChartExample();example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);example.pack();example.setVisible(true);});}
}

3、总结

通过本文的示例代码,您可以在 Java 应用程序中轻松创建多种类型的图表。XChart 提供了简单且强大的 API,能够满足大多数数据可视化需求。希望这些示例能帮助您更好地理解如何使用 XChart 进行数据可视化。

http://www.yayakq.cn/news/808150/

相关文章:

  • 怎么做网站数据库备份如何快速构建一个网站
  • 怎么成立个人网站广州门户网站制作公司
  • 网站运营现状阜沙网站建设
  • 常德德山经开区建设局网站如何开发微信微网站
  • 网站建设价格表沈阳网站的建设
  • 如何用一个域名做多个网站怎么搭建一个网站
  • 扶余手机网站开发在货源网站自己拿样 加盟 做代理 哪个比较好?
  • dedecms 旅游网站模板用手机能创建网站吗
  • 微网站开发技术架构上海市一体化办公平台
  • 长沙专业网站优化定制以下属于免费推广的方式的是
  • 网站建设要那些东西手机做直播官方网站
  • 兰州网站建设方案视频号视频怎么下载
  • 烟台网站建站tp5网站开发逻辑架构
  • jsp做的网站效果网站建设技术需求
  • 泰格豪雅手表官方网站15年做啥网站致富
  • 清溪镇网站仿做网站建设总经理岗位职责
  • 舟山注册公司企业网站seo参考文献
  • 网站添加模块网站强制qq弹窗代码
  • 电商网站话费充值怎么做快速制作网站的方法
  • 聊城高端网站建设免费海外网站建设
  • 手机网站需要什么易企秀h5怎么制作
  • 建设一个网站需要考虑什么阳西网络问政平台官网
  • 用自己网站做淘宝客个人网站建设方案书备案
  • 有网站代码 如何建设网站购物网页版
  • 网站建设哪里公司好单页设计思路
  • 做网站数据库坏了郑州市经开区建设局网站
  • 品牌商城网站建设公司做水果网站需要多钱
  • 甘肃住房和城乡建设厅网站首页小本本教你做网站
  • 海山免费网站建设电子商务网站建设体会
  • 凡科自助建站网站泉州模板建站平台