99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀【gloomyfish】Box zoom on Category Plot in JFreeChart?
【gloomyfish】Box zoom on Category Plot in JFreeChart?
2014-11-26
收藏

【gloomyfish】Box zoom on Category Plot in JFreeChart


Background:

 

currently JFreechart did not support domain axis zoom with category plot, the domain and value axis is zoomable only for XYPlot, however when category dataset contains huge categories while user could not select some categories to view by box zoom. the category plot is becoming un-usable one for user. obviously user would like to see box zoom with category plot.

 

Summary:

 

from box zoom on XYPlot in JFreechart, i read all relevant source code about zooming in JFreeChart and i found that there is a way to support box zoom on category plot by following steps:

 

a. support drawing the zoom rectangle in category data area (plot)

b. identify the domain axis and each category start point on domain axis.

c. measure the each category start point with zoom box

d. remove any categories if the start coordinate value is out of zoom rectangle.

 

Basic Design:

 

in order to support box zoom on category plot, we need to overwrite following methods which has been implemented in ChartPanel by JFreeChart:

 

1. mousePressed() - record the start zoom point

2. mouseDragged() - draw zoom box rectangle on category plot

3. mouseReleased() - zoom in the categories which is selected in rectangle.

4. paintComponent() - supporting to draw zoom rectangle

 

Run Result:

mouse selected rectangle - box zoom

box zoom

 

zooming the rectangle

box zoom 2

 

Code Implementation:

 

package test.it;  import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.event.MouseEvent; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D;  import javax.swing.JPanel; import javax.swing.JPopupMenu;  import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.Zoomable; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.experimental.chart.plot.CombinedCategoryPlot; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities;  /**  * A demo for the {@link CombinedCategoryPlot} class.  */ public class CombinedCategoryPlotDemo1 extends ApplicationFrame {      /** 	 *  	 */ 	private static final long serialVersionUID = 8114720685282689420L;  	/**      * Creates a new demo instance.      *      * @param title  the frame title.      */     public CombinedCategoryPlotDemo1(String title) {         super(title);         JPanel chartPanel = createDemoPanel();         chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));         setContentPane(chartPanel);     }      /**      * Creates a dataset.      *      * @return A dataset.      */     public static CategoryDataset createDataset2() {          DefaultCategoryDataset result = new DefaultCategoryDataset();          String series1 = "Third";         String series2 = "Fourth";          String type1 = "Type 1";         String type2 = "Type 2";         String type3 = "Type 3";         String type4 = "Type 4";         String type5 = "Type 5";         String type6 = "Type 6";         String type7 = "Type 7";         String type8 = "Type 8";          result.addValue(11.0, series1, type1);         result.addValue(14.0, series1, type2);         result.addValue(13.0, series1, type3);         result.addValue(15.0, series1, type4);         result.addValue(15.0, series1, type5);         result.addValue(17.0, series1, type6);         result.addValue(17.0, series1, type7);         result.addValue(18.0, series1, type8);          result.addValue(15.0, series2, type1);         result.addValue(17.0, series2, type2);         result.addValue(16.0, series2, type3);         result.addValue(18.0, series2, type4);         result.addValue(14.0, series2, type5);         result.addValue(14.0, series2, type6);         result.addValue(12.0, series2, type7);         result.addValue(11.0, series2, type8);          return result;      }      /**      * Creates a chart.      *      * @return A chart.      */     private static JFreeChart createChart() {          CategoryDataset dataset2 = createDataset2();         NumberAxis rangeAxis2 = new NumberAxis("Value");         rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());          CategoryAxis domainAxis = new CategoryAxis("Category");         CategoryPlot plot = new CategoryPlot(dataset2, domainAxis, new NumberAxis("Range"), new BarRenderer());         JFreeChart result = new JFreeChart(                 "Combined Domain Category Plot Demo",                 new Font("SansSerif", Font.BOLD, 12), plot, true);         return result;      }      /**      * Creates a panel for the demo (used by SuperDemo.java).      *      * @return A panel.      */     public static JPanel createDemoPanel() {         JFreeChart chart = createChart();         return new ChartPanel(chart){         	/** 			 *  			 */ 			private static final long serialVersionUID = -4857405671081534981L; 			private Point2D zoomPoint = null;         	private Rectangle2D zoomRectangle = null;         	private boolean fillZoomRectangle = true;         	private JPopupMenu popup;         	         	private Paint zoomOutlinePaint = Color.blue;         	private Paint zoomFillPaint = new Color(0, 0, 255, 63);                      	public void mousePressed(MouseEvent e) {         		if (e.isPopupTrigger()) {         			if(popup == null) {         				popup = createPopupMenu(true,true,true,true);         			}                     if (this.popup != null) {                         displayPopupMenu(e.getX(), e.getY());                         return;                     }                 }         		         		PlotOrientation orientation = ((Zoomable)this.getChart().getPlot()).getOrientation();         		System.out.println("Orientation --->> " + orientation.toString());         		if(orientation == PlotOrientation.HORIZONTAL) {         			return;         		}         		         		if (this.zoomRectangle == null) {                     Rectangle2D screenDataArea = getScreenDataArea(e.getX(), e.getY());                     if (screenDataArea != null) {                         this.zoomPoint = getPointInRectangle(e.getX(), e.getY(), screenDataArea);                     } else {                         this.zoomPoint = null;                     }                 }         	}         	         	private Point2D getPointInRectangle(int x, int y, Rectangle2D area) {                 double xx = Math.max(area.getMinX(), Math.min(x, area.getMaxX()));                 double yy = Math.max(area.getMinY(), Math.min(y, area.getMaxY()));                 return new Point2D.Double(xx, yy);             }          	public void mouseReleased(MouseEvent e) {         		if (e.isPopupTrigger()) {         			if(popup == null) {         				popup = createPopupMenu(true,true,true,true);         			}                     if (this.popup != null) {                         displayPopupMenu(e.getX(), e.getY());                         zoomRectangle = null;                         return;                     }                 }         		if(this.getChart().getCategoryPlot().getDataset().getColumnCount() < 2) {         			repaint();         			zoomRectangle = null;         			return;        			         		}         		if (zoomRectangle == null) {         			// do nothing         		} else {         			// do something here. zoom rectangle with data         			System.out.println("fucking........");         			System.out.println("reset dataset here");         			CategoryDataset dataset = this.getChart().getCategoryPlot().getDataset();         			System.out.println("category count = " + dataset.getColumnCount());         			System.out.println("category type = " + dataset.getRowCount());         			Comparable[] rowKeys = new Comparable[dataset.getRowCount()];         			rowKeys[0] = dataset.getRowKey(0);         			rowKeys[1] = dataset.getRowKey(1);         			Comparable[] columnKeys = new Comparable[dataset.getColumnCount()];         			for(int i=0; i<columnKeys.length; i++) {         				columnKeys[i] = dataset.getColumnKey(i);         			}         			double[] endValueAxis = new double[dataset.getColumnCount()];         			double[] startValueAxis = new double[dataset.getColumnCount()];         			double minX = zoomRectangle.getBounds2D().getMinX();         			double maxX = zoomRectangle.getBounds2D().getMaxX();                     CategoryPlot plot = this.getChart().getCategoryPlot();                      ChartRenderingInfo info = this.getChartRenderingInfo();                      Rectangle2D dataArea = info.getPlotInfo().getDataArea();         			         			CategoryAxis categoryaxis=this.getChart().getCategoryPlot().getDomainAxis();         			for(int i=0; i<dataset.getColumnCount(); i++) {         				endValueAxis[i] = categoryaxis.getCategoryEnd(i, dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge());         				startValueAxis[i] = categoryaxis.getCategoryStart(i, dataset.getColumnCount(), dataArea, plot.getDomainAxisEdge());         			}         			for(int i=0; i<endValueAxis.length; i++) {         				if(minX > startValueAxis[i] || maxX < startValueAxis[i]) {         					DefaultCategoryDataset defaultDataset = (DefaultCategoryDataset)dataset;         					defaultDataset.removeValue(rowKeys[0], columnKeys[i]);         					defaultDataset.removeValue(rowKeys[1], columnKeys[i]);         				}         			}        			         		}         		zoomRectangle = null;         	}          	public void mouseDragged(MouseEvent e) {         		// if no initial zoom point was set, ignore dragging...                 if (this.zoomPoint == null) {                     return;                 }                          		Graphics2D g2 = (Graphics2D) getGraphics();         		Rectangle2D scaledDataArea = getScreenDataArea((int) this.zoomPoint.getX(), (int) this.zoomPoint.getY());         		double ymax = Math.min(e.getY(), scaledDataArea.getMaxY());         		double xmax = Math.min(e.getX(), scaledDataArea.getMaxX());                 this.zoomRectangle = new Rectangle2D.Double(this.zoomPoint.getX(), this.zoomPoint.getY(),                 		xmax - this.zoomPoint.getX(), ymax - this.zoomPoint.getY());                 repaint();                 g2.dispose();         	}         	         	public void paintComponent(Graphics g) {         		super.paintComponent(g);         		Graphics2D g2 = (Graphics2D) g.create();         		drawZoomRectangle(g2, false);         		g2.dispose();         	}         	         	private void drawZoomRectangle(Graphics2D g2, boolean xor) {                 if (this.zoomRectangle != null) {                     if (xor) {                          // Set XOR mode to draw the zoom rectangle                         g2.setXORMode(Color.gray);                     }                     if (this.fillZoomRectangle) {                         g2.setPaint(this.zoomFillPaint);                         g2.fill(this.zoomRectangle);                     }                     else {                         g2.setPaint(this.zoomOutlinePaint);                         g2.draw(this.zoomRectangle);                     }                     if (xor) {                         // Reset to the default 'overwrite' mode                         g2.setPaintMode();                     }                 }             }         };     }      /**      * Starting point for the demonstration application.      *      * @param args  ignored.      */     public static void main(String[] args) {         String title = "Combined Category Plot Demo 1";         CombinedCategoryPlotDemo1 demo = new CombinedCategoryPlotDemo1(title);         demo.pack();         RefineryUtilities.centerFrameOnScreen(demo);         demo.setVisible(true);     }  }

I just adding some codes in the category demo program with JFreeChart, the code implementation need to be improved in future.

 

Drawback:

 

could not restore to original dataset since i just removed the categories, one way is to implement this like this:

just take back original dataset when there is only one category in plot.

 

Discussion:

...

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }