问题描述
请问哪个高手会JFreeChart插件制作散点图,请帮帮忙,现在急用,请给个小例子或者给解释一下都可以,请高手指点一下,谢谢……
解决方案
解决方案二:
引用楼主majingbo11011的回复:
请问哪个高手会JFreeChart插件制作散点图,请帮帮忙,现在急用,请给个小例子或者给解释一下都可以,请高手指点一下,谢谢……
我这有个可参考下。packagecom;importorg.jfree.ui.ApplicationFrame;importjava.awt.Color;importjava.awt.Dimension;importjava.text.NumberFormat;importjavax.swing.JPanel;importorg.jfree.chart.*;importorg.jfree.chart.axis.*;importorg.jfree.chart.plot.*;importorg.jfree.chart.renderer.category.LineAndShapeRenderer;importorg.jfree.chart.title.TextTitle;importorg.jfree.data.DataUtilities;importorg.jfree.data.DefaultKeyedValues;importorg.jfree.data.category.CategoryDataset;importorg.jfree.data.general.DatasetUtilities;importorg.jfree.ui.RefineryUtilities;importorg.jfree.util.SortOrder;publicclassParetoChartDemo1extendsApplicationFrame{publicParetoChartDemo1(Strings){super(s);JPaneljpanel=createDemoPanel();jpanel.setPreferredSize(newDimension(550,270));setContentPane(jpanel);}publicstaticJFreeChartcreateChart(CategoryDatasetacategorydataset[]){JFreeChartjfreechart=ChartFactory.createBarChart("FreshmeatSoftwareProjects","Language","Projects",acategorydataset[0],PlotOrientation.VERTICAL,true,true,false);jfreechart.addSubtitle(newTextTitle("ByProgrammingLanguage"));jfreechart.addSubtitle(newTextTitle("Asat5March2003"));jfreechart.setBackgroundPaint(Color.white);CategoryPlotcategoryplot=(CategoryPlot)jfreechart.getPlot();categoryplot.setBackgroundPaint(Color.lightGray);categoryplot.setRangeGridlinePaint(Color.white);CategoryAxiscategoryaxis=categoryplot.getDomainAxis();categoryaxis.setLowerMargin(0.02D);categoryaxis.setUpperMargin(0.02D);categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);NumberAxisnumberaxis=(NumberAxis)categoryplot.getRangeAxis();numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());LineAndShapeRendererlineandshaperenderer=newLineAndShapeRenderer();NumberAxisnumberaxis1=newNumberAxis("Percent");numberaxis1.setNumberFormatOverride(NumberFormat.getPercentInstance());categoryplot.setRangeAxis(1,numberaxis1);categoryplot.setDataset(1,acategorydataset[1]);categoryplot.setRenderer(1,lineandshaperenderer);categoryplot.mapDatasetToRangeAxis(1,1);categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);returnjfreechart;}publicstaticCategoryDataset[]createDatasets(){DefaultKeyedValuesdefaultkeyedvalues=newDefaultKeyedValues();defaultkeyedvalues.addValue("C",newInteger(4843));defaultkeyedvalues.addValue("C++",newInteger(2098));defaultkeyedvalues.addValue("C#",newInteger(26));defaultkeyedvalues.addValue("Java",newInteger(1901));defaultkeyedvalues.addValue("Perl",newInteger(2507));defaultkeyedvalues.addValue("PHP",newInteger(1689));defaultkeyedvalues.addValue("Python",newInteger(948));defaultkeyedvalues.addValue("Ruby",newInteger(100));defaultkeyedvalues.addValue("SQL",newInteger(263));defaultkeyedvalues.addValue("UnixShell",newInteger(485));defaultkeyedvalues.sortByValues(SortOrder.DESCENDING);org.jfree.data.KeyedValueskeyedvalues=DataUtilities.getCumulativePercentages(defaultkeyedvalues);CategoryDatasetcategorydataset=DatasetUtilities.createCategoryDataset("Languages",defaultkeyedvalues);CategoryDatasetcategorydataset1=DatasetUtilities.createCategoryDataset("Cumulative",keyedvalues);return(newCategoryDataset[]{categorydataset,categorydataset1});}publicstaticJPanelcreateDemoPanel(){CategoryDatasetacategorydataset[]=createDatasets();JFreeChartjfreechart=createChart(acategorydataset);returnnewChartPanel(jfreechart);}publicstaticvoidmain(Stringargs[]){ParetoChartDemo1paretochartdemo1=newParetoChartDemo1("ParetoChartDemo1");paretochartdemo1.pack();RefineryUtilities.centerFrameOnScreen(paretochartdemo1);paretochartdemo1.setVisible(true);}}
解决方案三:
谢谢你啦,但是我现在想用一个散点图,你给的这个是柱形图和折线图……不过还是谢谢你啦……
解决方案四:
解决方案五:
等待……
解决方案六:
这是快速散点图,float的data存储数据。定义两个轴为XY。调用时传入标题,X轴上的数据,Y轴上的数据。packageanalysis;importorg.jfree.chart.ChartFrame;importorg.jfree.chart.JFreeChart;importorg.jfree.chart.axis.DateAxis;importorg.jfree.chart.axis.NumberAxis;importorg.jfree.chart.plot.FastScatterPlot;importorg.jfree.ui.RefineryUtilities;publicclassScatterPlot{publicstaticvoiddata(Stringtitle,String[]a,String[]b){float[][]data=newfloat[2][a.length];for(inti=0;i<a.length;i++){data[0][i]=Float.parseFloat(a[i]);data[1][i]=Float.parseFloat(b[i]);}finalDateAxisdomainAxis=newDateAxis("X");//domainAxis.setAutoRangeIncludesZero(false);finalNumberAxisrangeAxis=newNumberAxis("Y");rangeAxis.setAutoRangeIncludesZero(false);finalFastScatterPlotplot=newFastScatterPlot(data,domainAxis,rangeAxis);//FastScatterPlotplot1=ChartFactory.createScatterPlot(title,title,title,dataset,orientation,legend,tooltips,urls);finalJFreeChartchart=newJFreeChart(title,plot);ChartFrameframe=newChartFrame(title,chart);frame.pack();RefineryUtilities.centerFrameOnScreen(frame);frame.setVisible(true);}}
解决方案七:
java代码/****@paramxydatalist*@parambloods*@return*/publicstaticXYDatasetcreatexydataset(List<PressureBean>xydatalist,Stringbloods){DefaultXYDatasetxydataset=newDefaultXYDataset();intsize=xydatalist.size();double[][]datas=newdouble[2][size];for(inti=0;i<size;i++){PressureBeanpres=xydatalist.get(i);intsys=pres.getSyspress();//收缩压intdia=pres.getDiapress();//舒张压datas[0][i]=sys;datas[1][i]=dia;}xydataset.addSeries(bloods,datas);returnxydataset;}publicstaticJFreeChartcreateChart(XYDatasetxydataset,Stringbloodcattile,Stringshou,Stringshu,StringnobloodData,Stringbloods,Stringnomal,Stringfore,Stringone,Stringtwo,List<PressureBean>list,Loglog){//有可能用户在后面的版本中故意输入不正常数值,但是为了保证图片画图的完整,这里先计算//用户血压值的最大值。intmaxpress=160;intaddmax=20;if(list!=null&&list.size()>0){Iterator<PressureBean>it=list.iterator();while(it.hasNext()){PressureBeanpres=it.next();if(maxpress<pres.getDiapress()){maxpress=pres.getDiapress();}if(maxpress<pres.getSyspress()){maxpress=pres.getSyspress();}}maxpress+=addmax;log.info("highpressvalueis="+maxpress);}JFreeChartjfreechart=ChartFactory.createScatterPlot(bloodcattile,shou,shu,xydataset,PlotOrientation.VERTICAL,true,false,false);jfreechart.setBackgroundPaint(Color.white);jfreechart.setBorderPaint(Color.GREEN);jfreechart.setBorderStroke(newBasicStroke(1.5f));XYPlotxyplot=(XYPlot)jfreechart.getPlot();xyplot.setNoDataMessage(nobloodData);xyplot.setNoDataMessageFont(newFont("",Font.BOLD,14));xyplot.setNoDataMessagePaint(newColor(87,149,117));xyplot.setBackgroundPaint(newColor(255,253,246));ValueAxisvaaxis=xyplot.getDomainAxis();vaaxis.setAxisLineStroke(newBasicStroke(1.5f));ValueAxisva=xyplot.getDomainAxis(0);va.setAxisLineStroke(newBasicStroke(1.5f));va.setAxisLineStroke(newBasicStroke(1.5f));//坐标轴粗细va.setAxisLinePaint(newColor(215,215,215));//坐标轴颜色xyplot.setOutlineStroke(newBasicStroke(1.5f));//边框粗细va.setLabelPaint(newColor(10,10,10));//坐标轴标题颜色va.setTickLabelPaint(newColor(102,102,102));//坐标轴标尺值颜色ValueAxisaxis=xyplot.getRangeAxis();axis.setAxisLineStroke(newBasicStroke(1.5f));XYLineAndShapeRendererxylineandshaperenderer=(XYLineAndShapeRenderer)xyplot.getRenderer();xylineandshaperenderer.setSeriesOutlinePaint(0,Color.WHITE);xylineandshaperenderer.setUseOutlinePaint(true);NumberAxisnumberaxis=(NumberAxis)xyplot.getDomainAxis();numberaxis.setAutoRangeIncludesZero(false);numberaxis.setTickMarkInsideLength(2.0F);numberaxis.setTickMarkOutsideLength(0.0F);numberaxis.setAxisLineStroke(newBasicStroke(1.5f));numberaxis.setUpperBound(maxpress);numberaxis.setLowerBound(60);//最小值设置为60NumberAxisnumberaxis1=(NumberAxis)xyplot.getRangeAxis();numberaxis1.setTickMarkInsideLength(2.0F);numberaxis1.setTickMarkOutsideLength(0.0F);numberaxis1.setUpperBound(105d);numberaxis1.setLowerBound(35);numberaxis1.setAxisLineStroke(newBasicStroke(1.5f));//if(xydataset!=null){XYBoxAnnotationbox=newXYBoxAnnotation(0,0,89,59);//正常血压所在区域内边界XYBoxAnnotationbox1=newXYBoxAnnotation(0,0,119,79);//高血压前期所在区域内边界XYBoxAnnotationbox2=newXYBoxAnnotation(0,0,139,89);//高血压一期所在区域内边界XYBoxAnnotationbox3=newXYBoxAnnotation(0,0,159,99);//高血压二期所在区域内边界XYTextAnnotationtext1=newXYTextAnnotation(nomal,70,62.5);//标识“正常”XYTextAnnotationtext=newXYTextAnnotation(fore,70,82.5);//“高血压前期”XYTextAnnotationtext2=newXYTextAnnotation(one,70,91.5);//“高血压一期”XYTextAnnotationtext3=newXYTextAnnotation(two,70,101.5);//“高血压二期”//将上面的边界线条,说明文字加入到xyplot中。xyplot.addAnnotation(box);xyplot.addAnnotation(box1);xyplot.addAnnotation(box2);xyplot.addAnnotation(box3);xyplot.addAnnotation(text);xyplot.addAnnotation(text1);xyplot.addAnnotation(text2);xyplot.addAnnotation(text3);//}returnjfreechart;}publicstaticvoiddrawScatterChart(IrisIoInterfaceio,Loglog,XYDatasetxydataSet,Stringtitle,Stringshou,Stringshu,Stringnodata,Stringboolds,Stringnomal,Stringfore,Stringone,Stringtwo,List<PressureBean>list){JFreeChartchart=createChart(xydataSet,title,shou,shu,nodata,boolds,nomal,fore,one,two,list,log);HttpServletRequestrequest=io.getRequest();Stringfilename="";StringgraphURL="";try{filename=ServletUtilities.saveChartAsPNG(chart,400,300,null,io.getSession());graphURL=request.getContextPath()+"/displayChart?filename="+filename;}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();log.error(e);}io.setData("filename",filename,BeanShare.BEAN_SHARE_REQUEST);io.setData("scatterurl",graphURL,BeanShare.BEAN_SHARE_REQUEST);}