All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.chaostocosmos.chaosgraph.awt2d.BarRatioGraph Maven / Gradle / Ivy

Go to download

This library contributes to making charts on AWT/Swing/SWT components. It can be used for AWT/Swing applications and Eclipse SWT (Standard Widget Toolkit) applications. If you would try to build a service of RAP dashboard web application, this library will provide great UX experience and excellent interactive functionality to the service for users.

There is a newer version: 2.1.2
Show newest version
package org.chaostocosmos.chaosgraph.awt2d;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.chaostocosmos.chaosgraph.AbstractGraph;
import org.chaostocosmos.chaosgraph.GraphElement;
import org.chaostocosmos.chaosgraph.GraphElements;
import org.chaostocosmos.chaosgraph.NotMatchGraphTypeException;

/**
 * 
 * BarRatioGraph
 *
 * @author Kooin-Shin
 * 2020. 9. 23.
 */
public class BarRatioGraph extends AbstractGraph {
	
    /**
     * Constructor
     * @param ge GraphElements 
     * @throws NotMatchGraphTypeException 
     * @since JDK1.4.1
     */
    public BarRatioGraph(GraphElements ge)  {
        this(ge, 600, 300);
    }
    
    /**
     * Constructor
     * @param width int
     * @param height int
     * @param ge GraphElements
     * @since JDK1.4.1
     */
    public BarRatioGraph(GraphElements ge, int width, int height)  {
        this(ge, "", width, height);
    }
    
    /**
     * Constructor
     * @param ge GraphElements 
     * @param title 
     * @param width int 
     * @param height int 
     * @since JDK1.4.1
     */
    public BarRatioGraph(GraphElements ge, String title, int width, int height)  {
        super(ge, title, width, height);
        if (ge.getGraphType() != GRAPH.BAR_RATIO) {
        	throw new NotMatchGraphTypeException("Can't draw graph with given graph elements type: "+ge.getGraphType().name());
        }
    }

    /**
     * Draw bar ratio graph
     * This method paint bar graph elements
     * @since JDK1.4.1
     */
	@Override
	public void drawGraph(Graphics2D g2d) {
        super.drawGraph(g2d);

        double maximum = 0;
        for(int i=0; i {
        		if(ge.getValues().size() > idx) {
        			return ge.getValues().get(idx);
        		}
        		return 0;
        	}).sum();        	
        	maximum = maximum > sum ? maximum : sum;
        }
        //maximum += maximum/20;
        if(maximum > GRAPH_ELEMENTS.getMaximum()) {
        	setLimit(maximum);
        }

    	super.setShowShadow(false);
    	
    	List xIndex = GRAPH_ELEMENTS.getXIndex();
        int minXIndex = GRAPH_ELEMENTS.getMinimumXIndexSize();
        if(minXIndex > xIndex.size()) {
            for(int i=0; i elementMap = GRAPH_ELEMENTS.getGraphElementMap();
        List elements = new ArrayList(elementMap.keySet());
        Collections.reverse(elements);

        double indent = 10;
        double unit = GRAPH_WIDTH / xIndex.size();
        double width = unit - indent * 2;
        int xIndexCount = GRAPH_ELEMENTS.getXIndexCount();
        
        g2d.setStroke(new BasicStroke(BORDER_SIZE, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2d.setClip((int)GRAPH_X, (int)(GRAPH_Y-GRAPH_HEIGHT), (int)GRAPH_WIDTH, (int)GRAPH_HEIGHT);

        for(int i=0; i ge.getValues().size() -1) {
        			continue;
        		}
        		double value = ge.getValues().get(i);
        		double height = (LIMIT > maximum) ? value : (value * GRAPH_HEIGHT / maximum);
        		y -= height;
        		//System.out.println(ge.getElementName()+"  x: "+x+"   y: "+y+"   height: "+height+"   maximum: "+maximum+"   value: "+value+"   limit: "+LIMIT);
        		
                if (IS_SHOW_SHADOW) {
                    color(SHADOW_COLOR, g2d);
                    setComposite(SHADOW_ALPHA, g2d);
                    double x1 = Math.cos(Math.toRadians(-(double)SHADOW_ANGLE))*SHADOW_DIST + x;
                    double y1 = Math.sin(Math.toRadians(-(double)SHADOW_ANGLE))*SHADOW_DIST + y;
                    g2d.fill(new Rectangle2D.Double(x1, y1, width, height-SHADOW_DIST));
                    color(GRAPH_BG_COLOR, g2d);
                    g2d.fill(new Rectangle2D.Double(x, y, width, height-SHADOW_DIST));
                }
                
                boolean isSelected = false;
                if(IS_SELECTION_ENABLE && GRAPH_ELEMENTS.getSelectedElement() != null && ge.getElementName().equals(GRAPH_ELEMENTS.getSelectedElement().getElementName())) {
                	if(SEL_BORDER == SELECTION_BORDER.LINE) {
                    	g2d.setStroke(new BasicStroke(BORDER_SIZE, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                	} else if(SEL_BORDER == SELECTION_BORDER.DOT) {
                    	g2d.setStroke(new BasicStroke(BORDER_SIZE*1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0, new float[]{BORDER_SIZE*2}, 0));
                	}
                	setComposite(GRAPH_ALPHA, g2d);
	                color(ge.getElementColor(), SELECTED_COLOR_DENSITY, g2d);
	                isSelected = true;
                } else {
                	if(IS_SELECTION_ENABLE && GRAPH_ELEMENTS.getSelectedElement() != null && !ge.getElementName().equals(GRAPH_ELEMENTS.getSelectedElement().getElementName())) {
                    	setComposite(0.05f, g2d);
                	} else {
                		setComposite(GRAPH_ALPHA, g2d);
                	}
                	g2d.setStroke(new BasicStroke(BORDER_SIZE, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
	                color(ge.getElementColor(), g2d);
                }
                g2d.fill(new Rectangle2D.Double(x, y, width, height));
                if (IS_SHOW_BORDER) {
                	if(isSelected) {
                		color(ge.getElementColor(), SELECTED_COLOR_DENSITY, g2d);
                	} else {
                		color(BORDER_COLOR, g2d);
                	}
                    g2d.draw(new Rectangle2D.Double(x, y, width, height));
                }
                List shapes = ge.getShapes();
                if(shapes == null) {
                	shapes = new ArrayList();
                }
                shapes.add(new Point((int)x, (int)y));
                shapes.add(new Point((int)x, (int)(y+height)));
                shapes.add(new Point((int)(x+width), (int)(y+height)));
                shapes.add(new Point((int)(x+width), (int)y));
                while(shapes.size() > ge.getValues().size() * 4) {
                	shapes.remove(0);
                }
                j++;
        	}
        }
        
        g2d.setClip(0, 0, IMG_WIDTH, IMG_HEIGHT);
        
        if(IS_SHOW_POPUP && GRAPH_ELEMENTS.getSelectedElement() != null && GRAPH_ELEMENTS.getSelectedElement().getSelectedPoint() != null) {
        	drawPopup(GRAPH_ELEMENTS.getSelectedElement().getSelectedPoint(),
            		POPUP_BG_COLOR, 
            		GRAPH_ELEMENTS.getSelectedElement(),
            		g2d);
        }
        
        if (IS_SHOW_LABEL) {
        	List names = new ArrayList();
        	for(int i = GRAPH_ELEMENTS.getElementOrder().size() -1; i >= 0; i--) {
        		names.add(GRAPH_ELEMENTS.getElementOrder().get(i));
        	}
            drawLabel(FONT_NAME, 
            		LABEL_FONT_SIZE, 
            		Font.BOLD, 
            		LABEL_BG_COLOR, 
            		names.stream().map(n -> GRAPH_ELEMENTS.getGraphElementMap().get(n)).collect(Collectors.toList()),
            		g2d); 
        }
	}

	@Override
	public GraphElement isPointOnShapes(int x, int y) {
		List list = new ArrayList(this.getGraphElements().getGraphElementMap().values());
		for(int i=0; i