Studyon Minte9.com
Java SE




Chart



1) Chat with grid and numbers.
2) Add and Delete records
3) Drag mouse up to add a line / Double click on line to delete it / Drag window to resize graph


1)  Chat with grid and numbers.

package testexample; import java.awt.*; import java.awt.geom.Line2D; import javax.swing.*; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 350, 250); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setTitle("Chart with Grid & Numbers"); frame.setVisible(true); } public Test() { add( new JPanel() { @Override public void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g = (Graphics2D) g1; int[][] data = new int[][]{ // --- Look Here --- // {1, 5}, {3, 15}, {4, 10}, {6, 13}, {7, 17}, }; addAxisGrid(g, data); // Axis & Grids addChart(g, data); // Chart } }); } private void addAxisGrid(Graphics2D g, int[][] data) { int x = 20; int y = 20; int h = 200; int w = 320; g.setStroke(new BasicStroke(1)); g.setColor(Color.gray); g.draw(new Line2D.Double(x, x, x, h)); // vertical ax g.draw(new Line2D.Double(x, h, w, h)); // horizontal ax for (int i=0; i<18; i++) { // horizontal (grid) and vertical (numbers) g.setColor(Color.decode("#dddddd")); // --- Look Here --- // g.draw(new Line2D.Double(x+2, h - 10 - i*10, w, h - 10 - i*10)); // horizontal ax g.setColor(Color.gray); if (i%5 == 0 && i > 0) { g.setFont(new Font("Arial", 1, 10)); g.drawString(String.valueOf(i), i<10 ? 10 : 5, h+5-i*10); // --- Look Here --- // } } for (int i=0; i<=30; i++) { // horizontal numbers // --- Look Here --- // g.setColor(Color.gray); if (i%5 == 0) { g.setFont(new Font("Arial", 1, 10)); g.drawString(String.valueOf(i), i<10 ? x+i*10 : x+i*10-5, h+15); } } } private void addChart(Graphics2D g, int[][] data) { // --- Look Here --- // g.setPaint(new GradientPaint(50, 50, Color.green, 50, 150, Color.red)); g.setStroke(new BasicStroke(3)); int start = 30; int height = 150; int x1 = 20; int x2 = 20; int y1 = 20; int y2 = 198; for (int i=0; i<data.length; i++) { int[] row = data[i]; int day = row[0]; int cnt = row[1]; x1 = start - 10 + day*10; x2 = x1; y1 = y2 - cnt*10; y2 = y2; g.draw(new Line2D.Double(x1, y1, x2, y2)); // --- Look Here --- // } } }
  2) Add and Delete record
package testexample; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Line2D; import java.util.LinkedHashMap; import java.util.Map; import javax.swing.*; public class Test extends JFrame { private int x = 20; private int y = 20; private int height = 220; private int weight = 320; private int step = 0; private Map<Integer, Integer> data = new LinkedHashMap<Integer, Integer>(){{ put(1, 5); put(3, 15); put(4, 10); put(6, 13); put(7, 17); }}; private JLabel labelDay = new JLabel("Day"); private JTextField textDay = new JTextField("5"); private JLabel labelNo = new JLabel("No"); private JTextField textNo= new JTextField("30"); private JButton buttonAdd = new JButton("Add"); private JButton buttonDelete = new JButton("Delete"); public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 350, 320); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setTitle("Chart with Grid & Numbers"); frame.setVisible(true); } public Test() { setStep(); // Components JPanel chart = new JPanel() { @Override public void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g = (Graphics2D) g1; addAxisAndGrid(g); paintChart(g); } }; // Set font Font font = new Font("Arial", Font.PLAIN, 12); labelDay.setFont(font); textDay.setFont(font); labelNo.setFont(font); textNo.setFont(font); buttonAdd.addActionListener(new ActionListener() { // --- Look Here --- // @Override public void actionPerformed(ActionEvent e) { addNewRecord(); setStep(); repaint(); } }); buttonDelete.addActionListener(new ActionListener() { // --- Look Here --- // @Override public void actionPerformed(ActionEvent e) { int i = Integer.parseInt(textDay.getText()); System.out.println(i); data.remove(i); // remove day setStep(); repaint(); } }); // Layout GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup() .addComponent(chart) .addGroup(layout.createSequentialGroup() .addGap(20) .addComponent(labelDay).addGap(5) .addComponent(textDay).addGap(5) .addComponent(labelNo).addGap(5) .addComponent(textNo).addGap(5) .addComponent(buttonAdd) .addComponent(buttonDelete) .addGap(20) ) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(chart, 250, 250, 250) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER) .addComponent(labelDay) .addComponent(textDay) .addComponent(labelNo) .addComponent(textNo) .addComponent(buttonAdd) .addComponent(buttonDelete) ) .addGap(20) ); } private void addNewRecord() { int day = Integer.valueOf(textDay.getText()); int no = Integer.valueOf(textNo.getText()); data.put(day, no); } private void setStep() { int max = 0; for (Object i : data.keySet()) { int cnt = data.get(i); if (cnt > max) max = cnt; } step = 200/max; } private void addAxisAndGrid(Graphics2D g) { g.setStroke(new BasicStroke(1)); g.setColor(Color.gray); g.draw(new Line2D.Double(x, x, x, height)); // vertical ax g.draw(new Line2D.Double(x, height, weight, height)); // horizontal ax int noOfGrids = (height-y)/step; for (int i=0; i<noOfGrids; i++) { // Horizontal Grid if (i < noOfGrids) { // skip last g.setColor(Color.decode("#dddddd")); g.draw(new Line2D.Double(x+2, height-step-i*step, weight, height-step-i*step)); // horizontal ax } } for (int i=0; i<=noOfGrids; i++) { // Vertical Numbers if (i%5 == 0 && i > 0) { g.setColor(Color.gray); g.setFont(new Font("Arial", 1, 10)); g.drawString(String.valueOf(i), i<10 ? x-10 : x-15, height+5-i*step); } } for (int i=0; i<=30; i++) { // horizontal numbers (30 days) g.setColor(Color.gray); if (i%5 == 0) { g.setFont(new Font("Arial", 1, 10)); g.drawString(String.valueOf(i), i<10 ? x+i*10 : x+i*10-5, height+15); } } } private void paintChart(Graphics2D g) { g.setPaint(new GradientPaint(x, y, Color.green, x, height, Color.red)); g.setStroke(new BasicStroke(3)); for (Object i : data.keySet()) { int day = (Integer) i; int cnt = data.get(i); int x1 = x + day*10; int x2 = x1; int y2 = height-2; int y1 = y2 - cnt*step; g.draw(new Line2D.Double(x1, y1, x2, y2)); } } }
  3) Drag mouse up to add a line / Double click on line to delete it / Drag window to resize graph
package testexample; import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.util.*; import javax.swing.*; public class Test extends JFrame { final static private Test frame = new Test(); public static void main(String[] args) { frame.setBounds(200, 200, 350, 280); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setTitle("Chart (mouse dragged to draw line)"); frame.setVisible(true); } public Test() { final Chart chart = new Chart(); add(chart); getRootPane().addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { // --- Look Here --- // Dimension d = frame.getSize(); chart.step = (d.height - 80)/20; chart.height = d.height - 60; chart.width = d.width - 30; chart.stepMonth = (chart.width)/chart.days; } }); } } class Chart extends JPanel { private int x = 20; private int y = 20; public int height = 220; public int width = 320; public int days = 30; private int currDay = 0; public int step = 10; public int stepMonth = (width - 20)/days; private int stroke = 3; private final Map<Integer, Integer> data = new LinkedHashMap<Integer, Integer>(){{ put(1, 5); put(3, 15); put(4, 10); put(6, 13); put(7, 17); }}; { addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { // --- Look Here --- // currDay = (e.getX() - x)/stepMonth; if (e.getButton() == 1) { // Left clicked if (data.get(currDay) == null) { // Not exists if (e.getY() == height - y) { addNewRecord(String.valueOf(currDay), "1"); repaint(); } } } if (e.getClickCount() == 2) { // Double click if (data.get(currDay) != null) { // If exists deleteRecord(currDay); // Delete it repaint(); } } } }); addMouseMotionListener(new MouseMotionAdapter(){ // --- Look Here --- // @Override public void mouseDragged(MouseEvent e) { if (currDay > 0) { data.put(currDay, (height - e.getY())/step); repaint(); } } }); } @Override public void paintComponent(Graphics g1) { super.paintComponent(g1); Graphics2D g = (Graphics2D) g1; addAxisAndGrid(g); paintChart(g); } private void addAxisAndGrid(Graphics2D g) { g.setStroke(new BasicStroke(1)); g.setColor(Color.gray); g.draw(new Line2D.Double(x, x, x, height)); // Vertical ax g.draw(new Line2D.Double(x, height, width, height)); // Horizontal ax int noOfGrids = (height-y)/step; for (int i=0; i<noOfGrids; i++) { // Horizontal Grid if (i < noOfGrids) { // skip last g.setColor(Color.decode("#dddddd")); g.draw(new Line2D.Double(x+2, height-step-i*step, width, height-step-i*step)); // Horizontal grids } } for (int i=0; i<=noOfGrids; i++) { // Vertical Numbers if (i%5 == 0 && i > 0) { g.setColor(Color.gray); g.setFont(new Font("Arial", 1, 10)); g.drawString(String.valueOf(i), i<10 ? x-10 : x-15, height+5-i*step); } } for (int i=0; i<=days; i++) { // Horizontal numbers (30 days) g.setColor(Color.gray); g.setFont(new Font("Arial", 1, 10)); if (i%2 == 0) { g.drawString(String.valueOf(i), i<10 ? x+i*stepMonth : x+i*stepMonth-5, height+15); } } } private void paintChart(Graphics2D g) { // --- Chart --- // g.setPaint(new GradientPaint(x, y, Color.green, x, height, Color.red)); g.setStroke(new BasicStroke(stroke)); for (Object i : data.keySet()) { // --- Vertical lines (chart values) int day = (Integer) i; int cnt = data.get(i); int x1 = x + day*stepMonth; int x2 = x1; int y2 = height-2; int y1 = y2 - cnt*step; g.draw(new Line2D.Double(x1, y1, x2, y2)); } } public void addNewRecord(String d, String n) { int day = Integer.valueOf(d); int no = Integer.valueOf(n); data.put(day, no); } public void deleteRecord(int day) { data.remove(currDay); } }