2012/05/17

Making a Dashboard Within 5 Minutes

Almost every program has such requirement: Lots of charts being put together to present varieties of statistics of data. In English it is called Dashboard, while there hasn't been an appropriate equivalent in Chinese. Someone just call it dash “dash board”, which is somewhat weird.

It is not a problem to make a Dashboard as soon as you have TWaver's Charts. You could make it by just putting the many Charts into a JPanel and then perfecting it by making a Border around it  with some buttons like min button and max button to operate it.

Let's first look at its effect:


Here is the code to it. We first make a Chart as an example. We recommend you to use other Chart of various kinds to enrich the visual effects.

public class MyBarChart extends BarChart {

   private Element data1 = new Node();
   private Element data2 = new Node();

     public MyBarChart() {
  this.setBarType(TWaverConst.BAR_TYPE_GROUP);
  this.setShadowOffset(10);
  this.setYScaleTextVisible(true);
  this.setYScaleMinTextVisible(true);
  this.setUpperLimit(60);
  this.setYScaleValueGap(10);


  this.addXScaleText("Jan");
  this.addXScaleText("Feb");
  this.addXScaleText("Mar");


  this.addElement(data1, "USD", TWaverUtil.getRandomColor());
  this.addElement(data2, "RMB", TWaverUtil.getRandomColor());
      
  this.addValue(data1,getRandomData(),getRandomData(),getRandomData());
  this.addValue(data2,getRandomData(),getRandomData(),getRandomData()); }  
private int getRandomData() {
  return TWaverUtil.getRandomInt(60);
}
private void addElement(Element element, String name, Color color) {
  element.setName(name);
  element.putChartColor(color);
  box.addElement(element);
}
private void addValue(Element element, double value1, double value2, double value3) {
  element.addChartValue(value1);
  element.addChartValue(value2);
  element.addChartValue(value3);
}
}


Next, let's turn to the main program: Just put a Panel of GridLayout and a Border outside it into the window.



public class Test extends JComponent {
   public Test() {
      this.setBorder(BorderFactory.createLineBorder(Color.lightGray, 1));
      this.setLayout(new BorderLayout());
      this.add(createHeaderPane(), BorderLayout.NORTH);
      this.add(createChart(), BorderLayout.CENTER);
}

   private JComponent createChart() {
      return new MyBarChart() ;
}

   private JPanel createHeaderPane() {
      JPanel headerPane = new JPanel(new BorderLayout());
      headerPane.setOpaque(false);
      headerPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      headerPane.add(createNorthPane(), BorderLayout.NORTH);

      JTextArea txtDescription = new JTextArea("Here is the description for this portlet.");
      txtDescription.setEditable(false);
      txtDescription.setEnabled(false);
      txtDescription.setOpaque(false);
      txtDescription.setFont(new Font("Dialog", Font.ITALIC, 12));
      txtDescription.setDisabledTextColor(Color.gray);
      txtDescription.setLineWrap(true);
      txtDescription.setBorder(createUnderlineBorder());
      headerPane.add(txtDescription, BorderLayout.CENTER);

      return headerPane;
   }

   private JPanel createNorthPane() {
       JPanel northPane = new JPanel(new BorderLayout());
       northPane.setOpaque(false);
       JLabel lbTitle = new JLabel("Month Sales");
       lbTitle.setFont(new Font("Dialog", Font.BOLD, 12));
       lbTitle.setForeground(new Color(0, 0, 255, 100));
       northPane.add(lbTitle, BorderLayout.CENTER);
       JPanel buttonPane = createButtonPane();
       northPane.add(buttonPane, BorderLayout.EAST);
       return northPane;
    }

    private JPanel createButtonPane() {
       JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
       buttonPane.setOpaque(false);
       JButton btnDropDown = new JButton();
       btnDropDown.setOpaque(false);
       btnDropDown.setMargin(new Insets(0, 0, 0, 0));
       btnDropDown.setIcon(TWaverUtil.getIcon("/dashboard/dropdown.png"));
       btnDropDown.setBorder(null);
       buttonPane.add(btnDropDown);
       JButton btnClose = new JButton();
       btnClose.setMargin(new Insets(0, 0, 0, 0));
       btnClose.setIcon(TWaverUtil.getIcon("/dashboard/close.png"));
       btnClose.setBorder(null);
       btnClose.setOpaque(false);
       btnClose.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               Test.this.setVisible(false);
           }
       });
       buttonPane.add(btnClose);
       return buttonPane;
    }

    private static Border createUnderlineBorder() {
       return new Border() {
           public void paintBorder(Component c,
                            Graphics g,
                            int x,
                            int y,
                            int width,
                            int height) {
                    g.setColor(Color.lightGray);
                    g.drawLine(x,
                    y + height - 2,
                    x + width,
                    y + height - 2);
           }

           public Insets getBorderInsets(Component c) {
                    return new Insets(0, 0, 2, 0);
           }

           public boolean isBorderOpaque() {
                    return true;
           }
       };
    }


    public static void main(String[] args) {
       JFrame frame = new JFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       JPanel pane = new JPanel(new GridLayout(3, 3, 10, 10));
       pane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
       pane.setBackground(Color.white);
       frame.add(pane);
       for (int i = 0; i < 9; i++) {
           pane.add(new Test());
       }
       frame.setSize(1000, 700);
       TWaverUtil.centerWindow(frame);
       frame.setTitle("TWaver Dashboard");
       frame.setVisible(true);
    }
}

Finally, we have here attached all codes and pictures.

No comments:

Post a Comment