1. 程式人生 > 程式設計 >DragChartPanel可拖拽曲線應用詳解

DragChartPanel可拖拽曲線應用詳解

DragChartPanel 是java cs架構中的一種圖形展現的開源元件。

業務需求需要用到DragChartPanel ,這是一種根據jtable表格中的資料給與展示的圖形元件。它和其他圖形元件區別再與它可以進行拖拽,使用者通過它不僅可以看出資料變化的曲線,而且可以通過拖拽修改表格中的資料。

下面展示一下它的效果圖:

醜歸醜,但是很實用呀。

下面展示它的程式碼

初始化座標格圖:

chartpanel1 = new DragChartPanel(this);
chartpanel1.setXtitle("時 間");
chartpanel1.setYtitle("負 荷(MW)");
chartpanel1.setDragLabel(jLabel1);
chartpanel1.setTm((DefaultTableModel) jTable1.getModel());
chartpanel1.setStartRow(0);
chartpanel1.setChartPanel(jPanel11);

繪製曲線:

private void drawGraph(int col) {
jLabel1.setText("");
int count = jTable1.getRowCount();
String[][] chartData1 = new String[1][count];
String[] chartIndex = new String[count];
DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
for (int i = 0; i < tm.getRowCount(); i++) {
      chartIndex[i] = tm.getValueAt(i,0) == null ? "" : tm.getValueAt(i,0).toString();
      chartData1[0][i] = tm.getValueAt(i,col) == null ? "" : tm.getValueAt(i,col).toString();
    }
    if (chartpanel1 != null) {
      chartpanel1.setEditColumn(col);
      chartpanel1.setData(chartData1);
      chartpanel1.setXLab(chartIndex);
      chartpanel1.setTitle(tm.getColumnName(col));
      chartpanel1.setLs_linename(new String[]{tm.getColumnName(col)});
      chartpanel1.getChart();
    }
  }

這個元件有一個缺點。只支援對列進行修改,這是這個元件的一個特點(拖動曲線表格的列會發生改動)。
為了使用它,很多表的結構就不得不迎合這一缺點。

例如如下表結構:

都是按列展示的。對於有些情況很不合理!如何解決(思考中。。。。。。)

上面思考的問題解決了!!!

原始碼中有這樣一個欄位:

再使用的過程中會出現如下情況:

這個時候你get哪個model就無所謂了,為了避免影響,隨便弄一樣不會發生改變的model吧。

這樣,拖動曲線的時候,label的值會實時顯示曲線的座標。

那麼,給上面的label 加一個監聽,改變值的時候,將label的內容用“:”分割開,不剛好是x軸和y軸的值了嗎?

對應橫向表格中,直接再label監聽事件中就可以更改table的值。(完美!!!)

注意一下:有時拖動x軸的座標是很詳細很詳細的資料移動,是因為你選這了setIndex而不是setXLab

private void initChartline() {
     //填充 圖形
    String[][] ChartData = new String[1][24];
    String[] ChartIndex = new String[24];
    if (fixTable.getRowCount() > 0) {
      for (int i = 0; i < dataTable.getColumnCount() - 2; i++) {
        ChartIndex[i] = String.valueOf(i);
        ChartData[0][i] = dataTable.getValueAt(fixtable_sld,i) == null ? "" : dataTable.getValueAt(fixtable_sld,i).toString();
      }
      if (dchartpanel_1 != null) {
        //String pdate = (String) fixTable.getValueAt(fixTable.getSelectedRow(),3);
        dchartpanel_1.setLs_linename(new String[]{"聯絡線曲線" + "ceshi1" + "(MW)"});
        dchartpanel_1.setLcol(Lcol);
        dchartpanel_1.setXLab(ChartIndex);//setXLab 和setIndex的區別在於,setXLab拖動取值x軸是按照下標取值的,而setIndex則是拖動的位置,常常不是整數,推薦用前者
        dchartpanel_1.setData(ChartData);
        //dchartpanel_1.setIndex(ChartIndex);
        dchartpanel_1.getChart();
      }
    } 
  }
//曲線顏色
  private java.awt.Color[] Lcol = {    
    java.awt.Color.red,java.awt.Color.blue,java.awt.Color.blue};

它的原始碼:

/*
 * To change this template,choose Tools | Templates
 * and open the template in the editor.
 */
package com.qctc.view.common.frame.pub;
 
import com.qctc.common.util.period;
import com.qctc.common.Constant;
import com.qctc.view.common.frame.BaseSubTabModule;
import com.qctc.view.common.frame.pub.datasysgraphdrag.datasysgraph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
 
/**
 *
 * @author Administrator
 */
public class DragChartPanel {
 
  private java.awt.Color[] Lcol = Constant.Lcol;
  private Color bgcolor = new Color(204,204,255);//背景色
  private Color rectcolor = Color.white;//圖形矩形內顏色
 
  public DragChartPanel(BaseSubTabModule parentModule,Color bgcolor,Color rectcolor) {
    this.bgcolor = bgcolor;
    this.rectcolor = rectcolor;
    this.parentModule = parentModule;
    initChartPanel();
  }
 
  public DragChartPanel(BaseSubTabModule parentModule) {
    this.parentModule = parentModule;
    initChartPanel();
  }
 
  public DragChartPanel() {
    initChartPanel();
  }
 
  public void initChartPanel() {
    //設定是否翻頁
    chart.setMutiPage(false);
    chart.setConvertBool(true);
 
    //設定底色
    chart.setBackColor(bgcolor);
//    chart.setBackColor(new Color(192,192,192));
    //設定圖形區域的邊框顏色
    chart.setBorderColor(Color.red);
 
    //設定圖形區域的底色
//    chart.setChartRectColor(new Color(204,250)); 
//    if(Constant.SYSTEM_AREA.equals("山西")){
    chart.setChartRectColor(rectcolor);
//    chart.setChartRectColor(new Color(74,74,107));
//    } 
 
    //設定xy座標軸顏色
    chart.setXyColor(new Color(30,30,30));
 
    //設定標題顏色
    chart.setTitleColor(new Color(30,30));
 
    //設定網格顏色
    chart.setGridColor(new Color(50,180,180));
 
    //設定是否顯示網格 1 顯示 其他不顯示
    chart.setShowHidegrid(1);
 
    //設定顯示點的大小
//    chart.setShowHidep(0);
//    if (Constant.SYSTEM_AREA.equals("山西")) {
    chart.setShowHidep(1);
//    }
 
    //滑鼠移動曲線上是否自動顯示橫縱座標十字線
    chart.setshowCrossLine(true);
    //設定滑鼠移動曲線顯示的十字線顏色
    chart.setCrossLineColor(new Color(150,150));
 
    //設定曲線型別:0 連續的曲線 3只顯示點
    chart.setLineType(0);
 
    //設定滑鼠提示框框邊顏色
    chart.setLwBorderColor(Color.red);
    //設定滑鼠顯示提示框底色
    chart.setLwColor(Color.yellow);
    //設定滑鼠顯示提示框提示字的顏色
    chart.setLwTextColor(Color.white);
 
    //設定橫豎網格線條數
    chart.setXzPoints(9);
    chart.setYzPoints(10);
 
    chartpanel.setLayout(new BorderLayout());
    chartpanel.add(getChart(),java.awt.BorderLayout.CENTER);
    chart.addMouseListener(new java.awt.event.MouseAdapter() {
 
      public void mousePressed(java.awt.event.MouseEvent evt) {
        dragState = true;
      }
 
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        dragState = false;
      }
    });
  }
 
  public boolean isDragState() {
    return dragState;
  }
  private boolean dragState = false;
  protected BaseSubTabModule parentModule;
  private JPanel chartpanel = new JPanel();
  // 
  private datasysgraph chart = new datasysgraph();
  //圖形引數
  private double maxv = -999999999,minv = 999999999;
  private boolean maxv_zd = true;
  private boolean minv_zd = true;
  private String[][] data = null;
  private String[] ls_linename = null;
  private String title = "";
  private String xtitle = "";
  private String ytitle = "";
  private int xpoints = 12;
  private int xmax = 96;
  private int xmin = 0;
  private String[] index = null;
  private int EditColumn = 0;//拖動關聯列
  private int StartRow = 0;//拖動起始行
  private JLabel DragLabel = null;//關聯table
  private DefaultTableModel tm = null;//關聯的tm
  private boolean sftd = true;
  private int ShowLabelCol = -1;
  private int[] LineShowLabelCols;
 
  public void setSftd(boolean sftd) {
    this.sftd = sftd;
  }
 
  public void setShowLabelCol(int ShowLabelCol) {
    this.ShowLabelCol = ShowLabelCol;
  }
 
  public void setLineShowLabelCols(int[] LineShowLabelCols) {
    this.LineShowLabelCols = LineShowLabelCols;
  }
 
  public void setTm(DefaultTableModel tm) {
    this.tm = tm;
  }
 
  public void setDragLabel(JLabel DragLabel) {
    this.DragLabel = DragLabel;
  }
 
  public void setEditColumn(int EditColumn) {
    this.EditColumn = EditColumn;
  }
 
  public void setStartRow(int StartRow) {
    this.StartRow = StartRow;
  }
 
  public void setXtitle(String xtitle) {
    this.xtitle = xtitle;
  }
 
  public void setYtitle(String ytitle) {
    this.ytitle = ytitle;
  }
 
  public void setIndex(String[] index) {
    this.index = index;
  }
 
  public void setData(String[][] data) {
    this.data = data;
  }
 
  public void setLs_linename(String[] ls_linename) {
    this.ls_linename = ls_linename;
  }
 
  public void setTitle(String title) {
    this.title = title;
  }
 
  public datasysgraph getChart() {
    try {
      createChart();
 
      return chart;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  public datasysgraph getChartByColor(Color[] col) {
    try {
      createChartByColor(col);
 
      return chart;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  /**
   * 設定chart要加入的JPanel
   * @param panel
   */
  public void setChartPanel(JPanel panel) {
    Container parent = chartpanel.getParent();
    if (parent != null) {
      parent.removeAll();
    }
    panel.add(chartpanel);
    panel.setMinimumSize(new Dimension(300,100));
  }
 
  private void createChart() {
    setMaxMin(this.data);
    setPara();
    setDragPara();
    setXY();
    buildChart();
  }
 
  private void createChartByColor(Color[] col) {
    setMaxMin(this.data);
    setPara();
    setDragPara();
    setXY();
    buildChartByColor(col);
  }
 
  /**
   * 生成圖
   */
  private void buildChart() {
    try {
      if (this.data != null) {
        int len = this.data.length;
        for (int i = 0; i < len; i++) {
          chart.AddItem(ls_linename[i],data[i].length,this.Lcol[i]);
          chart.setPointValue(ls_linename[i],this.index,this.data[i]);
        }
        chart.refresh();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * 生成圖
   */
  private void buildChartByColor(Color[] col) {
    try {
      if (this.data != null) {
        int len = this.data.length;
        for (int i = 0; i < len; i++) {
          chart.AddItem(ls_linename[i],col[i]);
          chart.setPointValue(ls_linename[i],this.data[i]);
        }
        chart.refresh();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * 設定引數
   */
  private void setPara() {
    try {
      chart.ClearAll();
      chart.setTitle(this.title);
      chart.setLtitlex(this.xtitle);
      chart.setLtitley(this.ytitle);
      chart.setMutiPage(false);
      chart.setYbzzb(this.getMaxv(),this.getMinv());
      chart.setShowLabelCol(ShowLabelCol);
      chart.setLineShowLabelCols(LineShowLabelCols);
 
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  private void setDragPara() {
    // 拖動資料列設定
    chart.setEditColumn(this.EditColumn);
    // 拖動起始行設定
    chart.setStartIndex(this.StartRow);
    chart.setJlabel(this.DragLabel);
    // 是否 實現拖動 設定
    chart.setIsDrag(this.sftd);
    chart.setTableDataModel(this.tm);
  }
  private String[] XLab = null;
 
  /**
   * 設定 圖形x軸標籤
   * @param XLab 
   */
  public void setXLab(String[] XLab) {
    this.XLab = XLab;
  }
 
  public void setXpoints(int xpoints) {
    this.xpoints = xpoints;
  }
  /**
   * x軸顯示座標間隔點數
   */
  private int XTabN = 10;
 
  /**
   * 設定xy軸
   */
  private void setXY() {
    try {
      chart.setXzPoints(this.xpoints);//x軸 分割線
      chart.setXmin(this.xmin);
 
      if (XLab == null) {
        chart.setXmax(this.xmax);
 
        this.xpoints = 12;
        period period1 = new period();
        String[] ls_pd = new String[97];
        for (int i = 0; i < 96; i++) {
          ls_pd[i] = period1.getPeriod()[i];
        }
        ls_pd[96] = "24:00";
 
        for (int i = 0; i <= 12; i++) {
          if (i == 0) {
            chart.SetXlab(i,ls_pd[i]);
          } else {
            chart.SetXlab(i,ls_pd[i * 8]);
          }
        }
        chart.setConvertBool(true);
        chart.setdxLab(ls_pd);
      } else {
 
 
        int len = this.data[0].length;
        this.XTabN = len / 8 + 1;
        this.xpoints = len / this.XTabN + 1;
        chart.setXzPoints(this.xpoints);//x軸 分割線
 
        this.index = new String[len];
        for (int i = 0; i < len; i++) {
          index[i] = String.valueOf(i);
        }
 
        this.xmax = this.xpoints * this.XTabN;
        chart.setXmax(this.xmax);
 
        String[] TmXLab = new String[this.xmax];
        for (int i = 0; i < this.xmax; i++) {
          if (i < XLab.length) {
            TmXLab[i] = XLab[i];
          } else {
            TmXLab[i] = "";
          }
        }
 
        for (int i = 0; i < this.xpoints; i++) {
          if (i * this.XTabN < len) {
            chart.SetXlab(i,TmXLab[i * this.XTabN]);
          } else {
            chart.SetXlab(i,"");
          }
        }
        chart.SetXlab(this.xpoints," ");
 
        chart.setConvertBool(true);
        chart.setdxLab(TmXLab);
 
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  /**
   * 獲取最值
   * @param data 
   */
  private void setMaxMin(String[][] data) {
    if (this.maxv_zd) {
      this.maxv = -999999999;
      try {
        if (data != null) {
          for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
              if (data[i][j] != null && !data[i][j].equals("")) {
                try {
                  double maxdd = Double.parseDouble( (data[i][j] == null || "null".equals(data[i][j]) || "".equals(data[i][j])) ? "0" : data[i][j]);
                  if ( maxdd > getMaxv()) {
                    this.maxv = maxdd;
                  }
                } catch (Exception e1) {
                  e1.printStackTrace();
                }
              }
            }
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      if (getMaxv() == -999999999) {
        this.maxv = 100;
      } else {
        if (getMaxv() > 0) {
          this.maxv = getMaxv() * 1.05;
        } else {
          this.maxv = getMaxv() * 0.95;
        }
      }
    }
 
    if (this.minv_zd) {
      this.minv = 999999999;
      try {
        if (data != null) {
          for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
              if (data[i][j] != null && !data[i][j].equals("")) {
                try {
                  double mindd = Double.parseDouble( (data[i][j] == null || "null".equals(data[i][j]) || "".equals(data[i][j])) ? "0" : data[i][j]);
                  if (mindd < getMinv()) {
                    this.minv = mindd;
                  }
                } catch (Exception e1) {
                  e1.printStackTrace();
                }
              }
            }
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      if (getMinv() == 999999999) {
        this.minv = 0;
      } else {
        if (getMinv() > 0) {
          this.minv = getMinv() * 0.95;
        } else {
          this.minv = getMinv() * 1.05;
        }
      }
    }
  }
 
  /**
   * @return the Lcol
   */
  public java.awt.Color[] getLcol() {
    return Lcol;
  }
 
  /**
   * @param Lcol the Lcol to set
   */
  public void setLcol(java.awt.Color[] Lcol) {
    this.Lcol = Lcol;
  }
 
  /**
   * @return the maxv
   */
  public double getMaxv() {
    return maxv;
  }
 
  /**
   * @param maxv the maxv to set
   */
  public void setMaxv(double maxv) {
    this.maxv = maxv;
    this.maxv_zd = false;
  }
 
  /**
   * @return the minv
   */
  public double getMinv() {
    return minv;
  }
 
  /**
   * @param minv the minv to set
   */
  public void setMinv(double minv) {
    this.minv = minv;
    this.minv_zd = false;
  }
 
  /**
   * 返回修改的曲線資料
   * 2013-04-08 hwh
   * @return 
   */
  public double[] getGraphDataY() {
    return chart.getGraphDataY();
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。