123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package com.hb.proj.diagram.diagnose;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.geom.Line2D;
- import java.util.List;
- /**
- * 功图绘制,用于功图诊断,作为被诊断对象、标准功图
- * 只需要闭合曲线,其他不需要绘制
- * 功图诊断时,最好标准功图和待诊断功图出自同一绘制方法、同大小
- * @author cwen
- *
- */
- public class DiagramRender {
- private Color canvasColor;
- private int canvasWidth;
- private int canvasHeight;
-
- private int originX ; //原点x值
- private int originY ; //原点y值
-
- private int xAxisEnd;
- private int yAxisEnd;
-
-
- private double xMinValue;
- private double yMinValue;
- private double xMaxValue;
- private double yMaxValue;
- private double xRatio;
- private double yRatio;
-
-
- public DiagramRender(){
- this(360,180);
- }
-
- public DiagramRender(int canvasWidth,int canvasHeight){
- this.canvasWidth=canvasWidth;
- this.canvasHeight=canvasHeight;
- this.canvasColor=new Color(255,255,255);
- this.originX=10;
- this.originY=canvasHeight-10;
- this.xAxisEnd=canvasWidth-10;
- this.yAxisEnd=10;
- this.xMaxValue=3;
- this.xMinValue=0;
- this.yMaxValue=50;
- this.yMinValue=0;
- }
-
- public void draw(Graphics2D g2,List<Double> disps,List<Double> oths,double stroke,double maxOth,double minOth) {
- this.xMinValue=0;
- this.xMaxValue=Math.ceil(stroke);
- this.yMinValue=Math.floor(minOth); //定格画,避免井的个体特性影响相似性判断,相当于各类图缩放在一个区域内
- this.yMaxValue=Math.ceil(maxOth);
-
- this.xRatio=(this.xAxisEnd-this.originX)/(this.xMaxValue-this.xMinValue);
- this.yRatio=(this.originY-this.yAxisEnd)/(this.yMaxValue-this.yMinValue);
-
- this.drawCanvas(g2);
- //this.drawCoordinate(g2);
- this.drawCurve(g2, disps, oths);
- }
-
-
-
- /**
- * 绘制画布
- * @param g2
- */
- public void drawCanvas(Graphics2D g2){
- g2.setColor(this.canvasColor);
- g2.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
- }
-
- /**
- * 简易坐标轴
- * @param g2
- */
- public void drawCoordinate(Graphics2D g2) {
- g2.setStroke(new BasicStroke(1));
- g2.setColor(new Color(212,212,212));
- g2.drawLine(this.originX, this.originY, this.originX, this.yAxisEnd); //y 轴
- g2.drawLine(this.xAxisEnd, this.originY, this.xAxisEnd, this.yAxisEnd); //y border
- g2.drawLine(this.originX, this.yAxisEnd, this.xAxisEnd, this.yAxisEnd); //x border
- g2.drawLine(this.originX, this.originY,this.xAxisEnd,this.originY); //x bottom-border
-
- int yGirdCount=5;
- double yGridDivide=(this.originY-this.yAxisEnd)/yGirdCount;
- double tempy=0;
- for(int i=1;i<yGirdCount;i++) {
- tempy=this.originY-i*yGridDivide;
- g2.draw(new Line2D.Double(this.originX, tempy, this.xAxisEnd,tempy));
- }
- }
-
- /**
- * 绘制曲线
- * @param g2
- * @param disps
- * @param oths
- */
- public void drawCurve(Graphics2D g2,List<Double> disps,List<Double> oths) {
- g2.setStroke(new BasicStroke(1));
- g2.setPaint(new Color(0,0,0));
- double ptx=getX(disps.get(0)),pty=getY(oths.get(0));
- double nptx=0,npty=0;
- for(int i=1,len=disps.size(),len2=oths.size();i<len && i<len2;i++) {
- nptx=getX(disps.get(i));
- npty=getY(oths.get(i));
- g2.draw(new Line2D.Double(ptx, pty, nptx,npty));
-
- ptx=nptx;
- pty=npty;
- }
-
- nptx=getX(disps.get(0));
- npty=getY(oths.get(0));
- g2.draw(new Line2D.Double(ptx, pty, nptx,npty));
- }
-
-
- private double getX(double val) {
- return this.originX+(val-this.xMinValue)*this.xRatio;
- }
- private double getY(double val) {
- return this.originY-(val-this.yMinValue)*this.yRatio;
- }
-
-
- }
|