Java画折线图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java画折线图相关的知识,希望对你有一定的参考价值。

??? JFreeChart 是开放源代码站点SourceForge.net 上的一个 JAVA 项目,它主要用来各种各样的图表,这些图表包括:饼图、柱状图 ( 普通柱状图以及堆栈柱状图 )、线图、区域图、分布图、混合图、甘特图以及一些仪表盘等等。

??? 应用jfreechart来画图需要两个jar包:jfreechart.jar和jcommon.jar,直接去官网下载就成:

https://sourceforge.net/projects/jfreechart/files/

?? 下载完成后解压,然后将jfreechart.jar和jcommon.jar两个包导入到需要画表的项目中,导入的方法:

右击项目所在的包,选中Build Path,然后选择? Configure?Build Path,点击Libraries,再点击Add External JARS,将上述两个包添加进去就可以用啦

技术分享图片技术分享图片


最近在写排序算法,忽然想对比一下各种排序算法的实际运行时间,所以画个图表可以更加直观的进行比较。


代码实现画折线图

class?chart{?
????????//?步骤1:创建CategoryDataset对象(准备数据)?
????CategoryDataset?dataset?=?createDataset();??
????CategoryDataset?dataset1?=?createDataset1();
????//?步骤2:根据Dataset?生成JFreeChart对象,以及做相应的设置??
????JFreeChart?freeChart?=?createChart(dataset);?
????JFreeChart?freeChart1?=?createChart1(dataset1);?
????
????
????//根据CategoryDataset创建JFreeChart对象
????public?static?JFreeChart?createChart(CategoryDataset?categoryDateset){
????????
????????//?创建JFreeChart对象:ChartFactory.createLineChart??
????????JFreeChart?jfreechart?=?ChartFactory.createLineChart("The?Confrontation?Of?Sorts?In?Best?Case",?//?标题
????????????????"Sort?By",?????????//categoryAxisLabel?(category轴,横轴,X轴标签)
????????????????"Spent?Time",??????//?valueAxisLabel(value轴,纵轴,Y轴的标签)
????????????????categoryDateset,??//Dataset??
????????????????PlotOrientation.VERTICAL,?false,?//?legend?
????????????????false,??????????//Tooltips
????????????????false);????????//URLs
????????
????????//?使用CategoryPlot设置各种参数。??
????????CategoryPlot?plot?=?(CategoryPlot)jfreechart.getPlot();??
????????
????????//?背景色?透明度??
????????plot.setBackgroundAlpha(0.5f);??
????????
????????//?前景色?透明度??
????????plot.setForegroundAlpha(1.0f);??
????????
????????//?其他设置?参考?CategoryPlot类??
????????LineAndShapeRenderer?renderer?=?(LineAndShapeRenderer)plot.getRenderer();??
????????renderer.setBaseShapesVisible(true);?//?series?点(即数据点)可见??
????????renderer.setBaseLinesVisible(true);?//?series?点(即数据点)间有连线可见??
????????renderer.setUseSeriesOffset(true);?//?设置偏移量??
????????renderer.setBaseItemLabelGenerator(new?StandardCategoryItemLabelGenerator());??
????????renderer.setBaseItemLabelsVisible(true);??
????????return?jfreechart;??
????}
????
????public?static?CategoryDataset?createDataset()?{??
????????String[]?rowKeys?=?{"Sort"};??
????????String[]?colKeys?=?{"HeapSort","ShellSort","BubbleSort","QuickSort","RadixSort","SimpleSort"};??
????????
????????double[][]?data?=?{{TimeChat.btime_Heap,TimeChat.btime_Shell,TimeChat.btime_Bubble,TimeChat.btime_Quick,TimeChat.btime_Radix,TimeChat.btime_Simple},};???
????????//System.out.println(TimeChat.btime_Heap);
????????return?DatasetUtilities.createCategoryDataset(rowKeys,?colKeys,?data);??
????}
????
????//最坏情况下
?????public?static?JFreeChart?createChart1(CategoryDataset?categoryDateset){
????????
????????//?创建JFreeChart对象:ChartFactory.createLineChart??
????????JFreeChart?jfreechart?=?ChartFactory.createLineChart("The?Confrontation?Of?Sorts?In?Worst?Case",?//?标题
????????????????"Sort?By",???????????//categoryAxisLabel?(category轴,横轴,X轴标签)?
????????????????"Spent?Time",???????//valueAxisLabel(value轴,纵轴,Y轴的标签)
????????????????categoryDateset,????//?Dataset
????????????????PlotOrientation.VERTICAL,?false,?//?legend?
????????????????false,????????????//Tooltips?
????????????????false);??????????//?URLs?
????????
?
????????CategoryPlot?plot?=?(CategoryPlot)jfreechart.getPlot();?
????????plot.setBackgroundAlpha(0.5f);???
????????plot.setForegroundAlpha(0.5f);
????????LineAndShapeRenderer?renderer?=?(LineAndShapeRenderer)plot.getRenderer();?
????????renderer.setBaseShapesVisible(true);?//?series?点(即数据点)可见?
????????renderer.setBaseLinesVisible(true);?//?series?点(即数据点)间有连线可见?
????????renderer.setUseSeriesOffset(true);?//?设置偏移量?
????????renderer.setBaseItemLabelGenerator(new?StandardCategoryItemLabelGenerator());?
????????renderer.setBaseItemLabelsVisible(true);?
????????return?jfreechart;?
?????}
?????
?????public?static?CategoryDataset?createDataset1()?{?
????????String[]?rowKeys?=?{"Sort"};?
????????String[]?colKeys?=?{"HeapSort","ShellSort","BubbleSort","QuickSort","RadixSort","SimpleSort"};?
???????
????????double[][]?data?=?{{TimeChat.wtime_Heap,TimeChat.wtime_Shell,TimeChat.wtime_Bubble,TimeChat.wtime_Quick,TimeChat.wtime_Radix,TimeChat.wtime_Simple},};??
????????//System.out.println(TimeChat.btime_Heap);
????????return?DatasetUtilities.createCategoryDataset(rowKeys,?colKeys,?data);?
????}
?????
}

将图表建好之后需要借助ChartPanel类将其显示出来,或者可以将生成的图表以图片格式保存到本地,我选择将它直接显示出来:

public?class?TimeChart{
?????public?static?void?main(String[]?strings){
??????????TimeChat?tc?=?new?TimeChat();?//另一个类,初始化data[][]中的变量
??????????chart?ct?=?new?chart();
??????????ChartPanel?chartf?=?new?ChartPanel(ct.freeChart,true);
??????????ChartPanel?chartf1?=?new?ChartPanel(ct.freeChart1,true);
??????????JFrame?jf?=?new?JFrame();
??????????jf.add(chartf,BorderLayout.WEST);
??????????jf.add(chartf1,BorderLayout.EAST);
??????????jf.setVisible(true);
??????????jf.setSize(1400,?600);
??????????jf.setLocationRelativeTo(null);
?????}
}

运行效果如下

技术分享图片技术分享图片














以上是关于Java画折线图的主要内容,如果未能解决你的问题,请参考以下文章

用matlab画折线图

python的turtle模块画折线图

Android画折线图柱状图饼图(使用achartengine.jar)

如何用R画折线图,散点图,平滑曲线图

利用python画折线图

unity 画折线图,饼型图插件