jfreechart学习笔记二 象限图

用jfreechart画一个象限图,因为在jfreechart的例子中并没有象限图的举例,我怀疑他没有此类图的chart类型,所以处理会比较特殊一些

 

先上图,我要画的就是下面这个图,因为只是个举例,所以数据造的比较极端,都在最顶上

那就正式开始

1、先用数据构造一个dataset

 

	/**
     * 构造数据集合
     * @param resultMap
     * @return
     */
	private XYDataset createDataSet(
			Map<String, Map<String, Object>> resultMap,double minScore, double midScore, double maxScore,
			double minAmount, double midAmount, double maxAmount) {
		Set<String> keySet =  resultMap.keySet();//传递过来的数据
		XYSeriesCollection xyseriescollection = new XYSeriesCollection();//先构造一个jfreechart数据类型的集合,这个集合是用来放XYSeries类型数据的
		xyseriescollection.setIntervalWidth(4.0D);//设置数据的区域宽度
		xyseriescollection.getDomainBounds(true);//设置边界的时候计算x方向的区域宽度
		for(String key : keySet){
			Map<String, Object> dataMap = resultMap.get(key);
			String label = dataMap.get("vendorName").toString().trim()+"("+key.trim()+")";
			XYSeries xyseries = new XYSeries(label);//创建一个数据点
			double x = (Double)dataMap.get("score");//数据的X值
			double y = (Double)dataMap.get("amount");//数据的Y值

//如果数据点是顶点位置,为了防止看不清楚,将数据点的位置调整进入数据区域
			if(x==maxScore){
				x = x-1d; 
			}else if(x==minScore){
				x = x+1d;
			}
			
			if(y==maxAmount){
				y = y-1d; 
			}else if(y==minAmount){
				y = y+1d;
			}
			xyseries.add(x, y);
			xyseries.setDescription((Double)dataMap.get("amount")+":"+(Double)dataMap.get("score"));
			xyseriescollection.addSeries(xyseries);
		}
		return xyseriescollection;//返回此数据集合
	}

 

2、构造chart

 

/**
 * 构造采购策略图
 * @return
  */
private JFreeChart createAmountChart(XYDataset dataSet,
			double minScore, double midScore, double maxScore,
			double minAmount, double midAmount, double maxAmount) {
        //因为象限图比较特殊,所以不采用jfreechart提供的chart类型,创建一个最基本的chart,剩下的都靠我们自己来处理了	

        //创建数据区域对象
        XYPlot plot = new XYPlot();
        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.setForegroundAlpha(0.5f);

		
		//数据区的设置
		plot.setNoDataMessage("没有数据");
		plot.setDomainPannable(true);
		plot.setRangePannable(true);
		plot.setDomainZeroBaselineVisible(true);
		plot.setRangeZeroBaselineVisible(true);
                                //将x轴方向的网格线设置为0
		plot.setDomainGridlineStroke(new BasicStroke(0.0f));
		plot.setDomainMinorGridlineStroke(new BasicStroke(0.0f));
		plot.setDomainGridlinePaint(Color.red);//设置x轴的网格线的颜色,我这里因为设置没网格线了,所以没有用处
                     //设置Y轴的网格线
		plot.setRangeGridlineStroke(new BasicStroke(0.0f));
		plot.setRangeMinorGridlineStroke(new BasicStroke(0.0f));
		plot.setRangeGridlinePaint(Color.blue);
		plot.setDomainGridlinesVisible(false);
		plot.setRangeGridlinesVisible(false);
		plot.setBackgroundPaint(Color.WHITE);//设置数据区域的背景色
		plot.setOutlineVisible(false);//是否显示数据区域的外边框
		
		plot.setDataset(dataSet);//给数据区域设置展示数据

        //创建XY轴对象,并进行设置	
        NumberAxis xAxis = new NumberAxis("评分");
        xAxis.setAutoRangeIncludesZero(false);
        xAxis.setAutoTickUnitSelection(false);
        xAxis.setTickUnit(new NumberTickUnit(5.0));
        xAxis.setTickMarkInsideLength(2.0F);
        xAxis.setTickMarkOutsideLength(2.0F);
		xAxis.setMinorTickCount(1);
		xAxis.setMinorTickMarksVisible(true);
		xAxis.setRange(minScore, maxScore);
        NumberAxis yAxis = new NumberAxis("采购量");
        yAxis.setAutoRangeIncludesZero(false);
        yAxis.setAutoTickUnitSelection(false);
        yAxis.setTickUnit(new NumberTickUnit(5.0));
        yAxis.setTickMarkInsideLength(2.0F);
        yAxis.setTickMarkOutsideLength(2.0F);
        yAxis.setMinorTickCount(1);
        yAxis.setMinorTickMarksVisible(true);
        yAxis.setRange(minAmount, maxAmount);
        
        plot.setDomainAxis(xAxis);//数据区域加入X轴
        plot.setRangeAxis(yAxis);//数据区域加入Y轴


		//设置标记,因为象限图需要把数据区分成四个区域,所以采用标记的方式来做这个处理
		

                               //先设置X轴上的标记,也就是跟Y轴平行的那根线
		ValueMarker makers = new ValueMarker(midScore);
	
		makers.setPaint(Color.BLACK);
		plot.addDomainMarker(makers,Layer.FOREGROUND);
		
                                //再设置Y轴上的标记,跟X轴平行的线
		makers = new ValueMarker(midAmount);
		makers.setPaint(Color.BLACK);
		plot.addRangeMarker(makers,Layer.FOREGROUND);
		//上面X,Y轴的Marker设置完,就能在数据区域内出现“十”字形的两根线将数据区域分成四块了
	
		//数据区域分成四块后,那么要用不同的背景来区分,而且公司希望好看点,最好有渐变效果,那么就要做一些特殊处理
		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(false,true);//创建一个渲染器
		GradientPaint paint = new GradientPaint(0, 0, new Color(255, 195, 035), 0,250 , Color.white);//创建一个渐变模式的Paint对象
		XYPolygonAnnotation xypolygonannotation = new XYPolygonAnnotation(
				new double[] { midScore,midAmount,midScore, maxAmount,maxScore, maxAmount,maxScore, midAmount }, new BasicStroke(5f),
				Color.white, paint);//创建一块区域的注解
		xylineandshaperenderer.addAnnotation(xypolygonannotation,
				Layer.BACKGROUND);//渲染器加入注解块

//同上,因为是四个象限,所以需要加入四个注解块
			paint = new GradientPaint(0, 0, new Color(99, 173, 255), -50,500 , Color.white);
		xypolygonannotation = new XYPolygonAnnotation(
				new double[] { midScore,minAmount,midScore, midAmount,maxScore, midAmount,maxScore, minAmount }, new BasicStroke(5f),
				Color.white, paint);
		xylineandshaperenderer.addAnnotation(xypolygonannotation,
				Layer.BACKGROUND);
		
		paint = new GradientPaint(0, 0, new Color(99, 173, 255), -50,500 , Color.white);
		xypolygonannotation = new XYPolygonAnnotation(
				new double[] { midScore,minAmount,midScore, midAmount,maxScore, midAmount,maxScore, minAmount }, new BasicStroke(5f),
				Color.white, paint);
		xylineandshaperenderer.addAnnotation(xypolygonannotation,
				Layer.BACKGROUND);
		
		paint = new GradientPaint(0, 0, new Color(99, 173, 255), -300,500 , Color.white);
		xypolygonannotation = new XYPolygonAnnotation(
				new double[] { minScore,minAmount,minScore, midAmount,midScore, midAmount,midScore, minAmount }, new BasicStroke(5f),
				Color.white, paint);
		xylineandshaperenderer.addAnnotation(xypolygonannotation,
				Layer.BACKGROUND);
		
		paint = new GradientPaint(0, 0, new Color(255, 102, 101), -50,250 , Color.white);
		xypolygonannotation = new XYPolygonAnnotation(
				new double[] { minScore,midAmount,minScore, maxAmount,midScore, maxAmount,midScore, midAmount }, new BasicStroke(5f),
				Color.white, paint);
		xylineandshaperenderer.addAnnotation(xypolygonannotation,
				Layer.BACKGROUND);
		XYSeriesCollection xySeries = (XYSeriesCollection) dataSet;
		
		List<XYSeries> xySeriesList = xySeries.getSeries();
		for(XYSeries series : xySeriesList){
			XYPointerAnnotation xypointerannotation = new XYPointerAnnotation(
			(String) series.getKey(), series.getX(0).doubleValue(), series.getY(0).doubleValue(), 2.3561944901923448D);
			xypointerannotation.setBaseRadius(35D);
			xypointerannotation.setTipRadius(10D);
			xypointerannotation.setFont(new Font("SansSerif", 0, 9));
			xypointerannotation.setPaint(Color.BLACK);
			xypointerannotation.setTextAnchor(TextAnchor.HALF_ASCENT_RIGHT);
			plot.addAnnotation(xypointerannotation);
		}

		xylineandshaperenderer.setBaseItemLabelsVisible(true);
		xylineandshaperenderer.setAutoPopulateSeriesShape(false);
		xylineandshaperenderer.setAutoPopulateSeriesOutlinePaint(false);
		xylineandshaperenderer.setAutoPopulateSeriesOutlineStroke(false);
		xylineandshaperenderer.setBaseShape(new Ellipse2D.Double(-3D, -3D, 8D, 8D));//渲染器设置数据的展现形状,这里采用圆形
		xylineandshaperenderer.setBaseOutlinePaint(Color.WHITE);
		xylineandshaperenderer.setBaseOutlineStroke(new BasicStroke(5.0f));

      

		plot.setRenderer(xylineandshaperenderer);//数据区域采用此渲染器
		

		
		
		        
        //因为chart构造器的原因,将chart放在最后进行创建
        JFreeChart chart = new JFreeChart("采购额策略分析",plot);
       
		
		return chart;
	}

 

三、生成图片

 

ChartUtilities.saveChartAsJPEG(img, chart, 500, 500);

一句话即可,当然,在此之前先创建img文件对象。

 

到此,我需要的象限图就完成了,不过还有个小毛病,在最底下指向数据的提示不见了,这是因为它一般放在数据下面,但是我这个数据是个极端数据,所以这个提示超过了数据区域的范围,也就看不见了,不知道大家有木有好的解决方法。先到这里

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于JFreeChart的时间序列,我们可以分为基本时间序列和高级时间序列两个部分。首先,我们来看一下基本时间序列的实现方法。 在JFreeChart中,我们可以通过使用TimeSeries类来表示时间序列数据。其中,TimeSeries类包含了一组时间序列数据和相应的时间戳信息。我们可以创建一个TimeSeries对象,并向其中添加数据点。然后,我们可以将这个TimeSeries对象传递给ChartFactory.createTimeSeriesChart()方法,创建一个基本的时间序列。 以下是一个基本的时间序列的示例代码: ```java import java.awt.Color; import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.axis.DateTickUnit; import org.jfree.chart.axis.DateTickUnitType; import org.jfree.chart.axis.TickUnitSource; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.time.Day; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; public class BasicTimeSeriesChart extends JPanel { private static final long serialVersionUID = 1L; public BasicTimeSeriesChart() { TimeSeries series = new TimeSeries("Random Data"); Day day = new Day(new Date(2021, 1, 1)); for (int i = 0; i < 365; i++) { double value = Math.random() * 100; series.add(day, value); day = (Day) day.next(); } TimeSeriesCollection dataset = new TimeSeriesCollection(series); JFreeChart chart = ChartFactory.createTimeSeriesChart("Basic Time Series Chart", "Time", "Value", dataset); XYPlot plot = (XYPlot) chart.getPlot(); plot.setBackgroundPaint(Color.white); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesPaint(0, Color.blue); renderer.setSeriesShapesVisible(0, false); plot.setRenderer(renderer); DateAxis axis = (DateAxis) plot.getDomainAxis(); axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); TickUnitSource ticks = axis.createStandardDateTickUnits(DateTickUnitType.MONTH, 1); axis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 1, ticks.size(), new SimpleDateFormat("MMM-yyyy"))); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 300)); add(chartPanel); } } ``` 在这个示例代码中,我们创建了一个TimeSeries对象,并向其中添加了365个随机数据点。然后,我们将这个TimeSeries对象传递给ChartFactory.createTimeSeriesChart()方法,创建了一个基本的时间序列。 接下来,我们对这个时间序列进行了定制化。我们设置了表的背景颜色为白色,并将数据线的颜色设置为蓝色。我们还定制了x轴的刻度,将它们设置为每个月一次,并使用“MMM-yyyy”格式来显示日期。 最后,我们将创建的表添加到一个ChartPanel对象中,并将这个ChartPanel对象添加到了JPanel中,以便将表显示在GUI中。 这就是一个基本的JFreeChart时间序列的实现方法。下一步,我们将介绍如何创建更高级的时间序列

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值