Dragchartpanel drag curve application details

Dragchartpanel is an open source component for graphical presentation in Java CS architecture.

Business requirements need to use dragchartpanel, which is a graphical component displayed according to the data in JTable table. It is different from other graphic components and can be dragged. Users can not only see the curve of data change, but also modify the data in the table by dragging.

Here's the rendering:

Ugly is ugly, but it's very practical.

Here's the code

Initialize grid:

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

Draw curve:

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();
    }
  }

This component has one drawback. Only column modification is supported, which is a feature of this component (the column of the curve table will be changed when dragging). In order to use it, the structure of many tables has to cater to this disadvantage.

For example, the following table structure:

They are displayed by column. It is unreasonable for some situations! How to solve (thinking...)

The above problems have been solved!!!

There is such a field in the source code:

During reuse, the following situations will occur:

At this time, it doesn't matter which model you get. In order to avoid influence, just get a model that won't change.

In this way, when dragging the curve, the value of label will display the coordinates of the curve in real time.

Then, add a monitor to the above label. When changing the value, separate the label content with ":". Isn't it just the value of x-axis and y-axis?

In the corresponding horizontal table, you can change the value of table directly in the label listening event. (perfect!!!)

Note: sometimes dragging the x-axis coordinates is a very detailed data movement because you selected setindex instead of 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};

Its source code:

/*
 * 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();
  }
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>