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

ro.nextreports.engine.chart.Chart Maven / Gradle / Ivy

Go to download

NextReports Engine is a lightweight Java platform development library which can be used to run NextReports inside your applications.

There is a newer version: 9.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ro.nextreports.engine.chart;

import java.io.Serializable;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

import ro.nextreports.engine.Report;

/**
 * User: mihai.panaitescu
 * Date: 14-Dec-2009
 * Time: 15:49:26
 */
public class Chart implements Serializable {

    private static final long serialVersionUID = -3455484134680121149L;

    // label orientation
    public static transient final byte HORIZONTAL = 1;
    public static transient final byte VERTICAL = 2;
    public static transient final byte DIAGONAL = 3;
    public static transient final byte HALF_DIAGONAL = 4;

    // transparency
    public static transient final byte NONE_TRANSPARENCY = 1;
    public static transient final byte LOW_TRANSPARENCY = 2;
    public static transient final byte AVG_TRANSPARENCY = 3;
    public static transient final byte HIGH_TRANSPARENCY = 4;

    private String name;
    private String version;
    private ChartTitle title;
    private ChartType type;
    private Report report;
    private Color background;
    private List foregrounds;
    private String xColumn;
    private List yColumns;
    private List yColumnsLegends;
    private String yColumnQuery;
    private byte xorientation;
    private String xPattern;
    private Color xColor;
    private Color yColor;
    private Color xAxisColor;
    private Color yAxisColor;
    private byte transparency;
    private ChartTitle xLegend;
    private ChartTitle yLegend;
    private String yFunction;
    private Color xGridColor;
    private Color yGridColor;
    private Boolean xShowGrid;
    private Boolean yShowGrid;
    private Boolean xShowLabel;
    private Boolean yShowLabel;
    // showYValuesOnChart has meaning for image, not for flash
    private Boolean showYValuesOnChart;
    private String yTooltipPattern;
    private Font font;
    private Font xLabelFont;
    private Font yLabelFont;

    public static transient Color[] COLORS = new Color[] {
            new Color(0, 0, 204),
            Color.RED,
            new Color(0, 204, 102),
            new Color(153, 0, 153),
            new Color(255, 200, 50),
            new Color(0, 200, 200),
            new Color(240, 240, 17),
            new Color(131, 76, 20),
            new Color(120, 128, 13),
            new Color(230, 80, 200)
    };
    
    public Chart() {
        // defaults
        this.title = new ChartTitle("");
        this.xLegend = new ChartTitle("");
        this.yLegend = new ChartTitle("");
        this.background = Color.WHITE;
        this.foregrounds = new ArrayList();
        this.xorientation = HORIZONTAL;
        this.transparency = AVG_TRANSPARENCY;
        this.xColor = Color.BLACK;
        this.yColor= Color.BLACK;
        this.xAxisColor = Color.BLACK;
        this.yAxisColor= Color.BLACK;
        this.xShowGrid = Boolean.TRUE;
        this.yShowGrid = Boolean.TRUE;
        this.xShowLabel = Boolean.TRUE;
        this.yShowLabel = Boolean.TRUE;
        this.showYValuesOnChart = Boolean.FALSE;        
        foregrounds.addAll(Arrays.asList(COLORS));        
        setType(new ChartType(ChartType.BAR));
    }

    public Chart(ChartType type, Report report) {
        this();
        setType(type);
        setReport(report);
    }

    public ChartType getType() {
        return type;
    }

    public void setType(ChartType type) {
        if (type == null) {
            throw new IllegalArgumentException("Chart type cannot be null.");
        }
        this.type = type;
    }

    public Report getReport() {
        return report;
    }

    public void setReport(Report report) {
        if (report == null) {
            throw new IllegalArgumentException("Report cannot be null.");
        }
        this.report = report;
    }

    public ChartTitle getTitle() {
        return title;
    }

    public void setTitle(ChartTitle title) {
        this.title = title;
    }

    public Color getBackground() {
        return background;
    }

    public void setBackground(Color background) {
        this.background = background;
    }

    public List getForegrounds() {
    	if (foregrounds.size() == 6) {
    		// from 4.1 to 4.2 added another 4 properties
    		foregrounds.add(new Color(240, 240, 17));
    		foregrounds.add(new Color(131, 76, 20));
    		foregrounds.add(new Color(120, 128, 13));
    		foregrounds.add(new Color(230, 80, 200));    	
    	}	
        return foregrounds;
    }

    public void setForegrounds(List foregrounds) {
        this.foregrounds = foregrounds;
    }

    public String getXColumn() {
        return xColumn;
    }

    public void setXColumn(String xColumn) {
        this.xColumn = xColumn;
    }

    public List getYColumns() {
        if (yColumns == null) {
            yColumns = new ArrayList();
        }
        return yColumns;
    }

    public void setYColumns(List yColumns) {
        List list = new ArrayList();
        for (String s : yColumns) {
            if (s!=null) {
                list.add(s);
            }
        }
        this.yColumns = list;
    }
        
    public String getYColumnQuery() {
		return yColumnQuery;
	}

	public void setYColumnQuery(String yColumnQuery) {
		this.yColumnQuery = yColumnQuery;
	}

	public List getYColumnsLegends() {
        if (yColumnsLegends == null) {
            yColumnsLegends = new ArrayList();
        }
        return yColumnsLegends;
    }

    public void setYColumnsLegends(List yColumnsLegends) {
        List list = new ArrayList();
        for (String s : yColumnsLegends) {
            if (s!=null) {
                list.add(s);
            }
        }
        this.yColumnsLegends = list;
    }

    public String getName() {
        return name;
    }

    public String getBaseName() {
        if (name.endsWith(".chart")) {
            return name.substring(0, name.length()-6);
        } else {
            return name;
        }
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public byte getXorientation() {
        return xorientation;
    }

    public void setXorientation(byte xorientation) {
        this.xorientation = xorientation;
    }

    public Color getXColor() {        
        return xColor;
    }

    public void setXColor(Color xColor) {
        this.xColor = xColor;
    }

    public Color getYColor() {
        return yColor;
    }

    public void setYColor(Color yColor) {
        this.yColor = yColor;
    }        

    public Color getxAxisColor() {
    	if (xAxisColor == null) {
    		xAxisColor = Color.BLACK;
    	}
		return xAxisColor;
	}

	public void setxAxisColor(Color xAxisColor) {
		this.xAxisColor = xAxisColor;
	}

	public Color getyAxisColor() {
		if (yAxisColor == null) {
			yAxisColor = Color.BLACK;
		}
		return yAxisColor;
	}

	public void setyAxisColor(Color yAxisColor) {
		this.yAxisColor = yAxisColor;
	}

	public byte getTransparency() {
        return transparency;
    }

    public void setTransparency(byte transparency) {
        this.transparency = transparency;
    }

    public ChartTitle getXLegend() {
        return xLegend;
    }

    public void setXLegend(ChartTitle xLegend) {
        this.xLegend = xLegend;
    }

    public ChartTitle getYLegend() {
        return yLegend;
    }

    public void setYLegend(ChartTitle yLegend) {
        this.yLegend = yLegend;
    }

    public String getYFunction() {
        return yFunction;
    }

    public void setYFunction(String yFunction) {
        this.yFunction = yFunction;
    }

    public String getXPattern() {
        return xPattern;
    }

    public void setXPattern(String xPattern) {
        this.xPattern = xPattern;
    }

    public Color getXGridColor() {
        return xGridColor;
    }

    public void setXGridColor(Color xGridColor) {
        this.xGridColor = xGridColor;
    }

    public Color getYGridColor() {
        return yGridColor;
    }

    public void setYGridColor(Color yGridColor) {
        this.yGridColor = yGridColor;
    }

    public Boolean getXShowGrid() {
        return xShowGrid;
    }

    public void setXShowGrid(Boolean xShowGrid) {
        this.xShowGrid = xShowGrid;
    }

    public Boolean getYShowGrid() {
        return yShowGrid;
    }

    public void setYShowGrid(Boolean yShowGrid) {
        this.yShowGrid = yShowGrid;
    }

    public Boolean getXShowLabel() {
        return xShowLabel;
    }

    public void setXShowLabel(Boolean xShowLabel) {
        this.xShowLabel = xShowLabel;
    }

    public Boolean getYShowLabel() {
        return yShowLabel;
    }

    public void setYShowLabel(Boolean yShowLabel) {
        this.yShowLabel = yShowLabel;
    }        

    public Boolean getShowYValuesOnChart() {
		return showYValuesOnChart;
	}

	public void setShowYValuesOnChart(Boolean showYValuesOnChart) {
		this.showYValuesOnChart = showYValuesOnChart;
	}
		
	public String getYTooltipPattern() {
		return yTooltipPattern;
	}

	public void setYTooltipPattern(String yTooltipPattern) {
		this.yTooltipPattern = yTooltipPattern;
	}
	
	public Font getFont() {
        if (font == null) {
            return getDefaultFont();
        }
        return font;
    }

    public void setFont(Font font) {    	
        this.font = font;
    }
    
    public Font getXLabelFont() {
        if (xLabelFont == null) {
            return getDefaultFont();
        }
        return xLabelFont;
    }

    public void setXLabelFont(Font xLabelFont) {    	
        this.xLabelFont = xLabelFont;
    }
    
    public Font getYLabelFont() {
        if (yLabelFont == null) {
            return getDefaultFont();
        }
        return yLabelFont;
    }

    public void setYLabelFont(Font yLabelFont) {    	
        this.yLabelFont = yLabelFont;
    }
	
	private Font getDefaultFont() {
		return new Font("SansSerif", Font.PLAIN, 12);
	}

	public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Chart chart = (Chart) o;

        if (transparency != chart.transparency) return false;
        if (xorientation != chart.xorientation) return false;
        if (background != null ? !background.equals(chart.background) : chart.background != null) return false;
        if (foregrounds != null ? !foregrounds.equals(chart.foregrounds) : chart.foregrounds != null) return false;
        if (name != null ? !name.equals(chart.name) : chart.name != null) return false;
        if (report != null ? !report.equals(chart.report) : chart.report != null) return false;
        if (title != null ? !title.equals(chart.title) : chart.title != null) return false;
        if (type != null ? !type.equals(chart.type) : chart.type != null) return false;
        if (version != null ? !version.equals(chart.version) : chart.version != null) return false;
        if (xColor != null ? !xColor.equals(chart.xColor) : chart.xColor != null) return false;
        if (xColumn != null ? !xColumn.equals(chart.xColumn) : chart.xColumn != null) return false;
        if (xGridColor != null ? !xGridColor.equals(chart.xGridColor) : chart.xGridColor != null) return false;
        if (xLegend != null ? !xLegend.equals(chart.xLegend) : chart.xLegend != null) return false;
        if (xPattern != null ? !xPattern.equals(chart.xPattern) : chart.xPattern != null) return false;
        if (xShowGrid != null ? !xShowGrid.equals(chart.xShowGrid) : chart.xShowGrid != null) return false;
        if (xShowLabel != null ? !xShowLabel.equals(chart.xShowLabel) : chart.xShowLabel != null) return false;
        if (yColor != null ? !yColor.equals(chart.yColor) : chart.yColor != null) return false;
        if (yColumns != null ? !yColumns.equals(chart.yColumns) : chart.yColumns != null) return false;
        if (yColumnsLegends != null ? !yColumnsLegends.equals(chart.yColumnsLegends) : chart.yColumnsLegends != null)
            return false;
        if (yColumnQuery != null ? !yColumnQuery.equals(chart.yColumnQuery) : chart.yColumnQuery != null) return false;
        if (yFunction != null ? !yFunction.equals(chart.yFunction) : chart.yFunction != null) return false;
        if (yGridColor != null ? !yGridColor.equals(chart.yGridColor) : chart.yGridColor != null) return false;
        if (yLegend != null ? !yLegend.equals(chart.yLegend) : chart.yLegend != null) return false;
        if (yShowGrid != null ? !yShowGrid.equals(chart.yShowGrid) : chart.yShowGrid != null) return false;
        if (yShowLabel != null ? !yShowLabel.equals(chart.yShowLabel) : chart.yShowLabel != null) return false;
        if (showYValuesOnChart != null ? !showYValuesOnChart.equals(chart.showYValuesOnChart) : chart.showYValuesOnChart != null) return false;
        if (yTooltipPattern != null ? !yTooltipPattern.equals(chart.yTooltipPattern) : chart.yTooltipPattern != null) return false;
        if (font != null ? !font.equals(chart.font) : chart.font != null) return false;
        if (xLabelFont != null ? !xLabelFont.equals(chart.xLabelFont) : chart.xLabelFont != null) return false;
        if (yLabelFont != null ? !yLabelFont.equals(chart.yLabelFont) : chart.yLabelFont != null) return false;
        if (xAxisColor != null ? !xAxisColor.equals(chart.xAxisColor) : chart.xAxisColor != null) return false;
        if (yAxisColor != null ? !yAxisColor.equals(chart.yAxisColor) : chart.yAxisColor != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (name != null ? name.hashCode() : 0);
        result = 31 * result + (version != null ? version.hashCode() : 0);
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        result = 31 * result + (report != null ? report.hashCode() : 0);
        result = 31 * result + (background != null ? background.hashCode() : 0);
        result = 31 * result + (foregrounds != null ? foregrounds.hashCode() : 0);
        result = 31 * result + (xColumn != null ? xColumn.hashCode() : 0);
        result = 31 * result + (yColumns != null ? yColumns.hashCode() : 0);
        result = 31 * result + (yColumnsLegends != null ? yColumnsLegends.hashCode() : 0);
        result = 31 * result + (yColumnQuery != null ? yColumnQuery.hashCode() : 0);
        result = 31 * result + (int) xorientation;
        result = 31 * result + (xPattern != null ? xPattern.hashCode() : 0);
        result = 31 * result + (xColor != null ? xColor.hashCode() : 0);
        result = 31 * result + (yColor != null ? yColor.hashCode() : 0);
        result = 31 * result + (xAxisColor != null ? xAxisColor.hashCode() : 0);
        result = 31 * result + (yAxisColor != null ? yAxisColor.hashCode() : 0);
        result = 31 * result + (int) transparency;
        result = 31 * result + (xLegend != null ? xLegend.hashCode() : 0);
        result = 31 * result + (yLegend != null ? yLegend.hashCode() : 0);
        result = 31 * result + (yFunction != null ? yFunction.hashCode() : 0);
        result = 31 * result + (xGridColor != null ? xGridColor.hashCode() : 0);
        result = 31 * result + (yGridColor != null ? yGridColor.hashCode() : 0);
        result = 31 * result + (xShowGrid != null ? xShowGrid.hashCode() : 0);
        result = 31 * result + (yShowGrid != null ? yShowGrid.hashCode() : 0);
        result = 31 * result + (xShowLabel != null ? xShowLabel.hashCode() : 0);
        result = 31 * result + (yShowLabel != null ? yShowLabel.hashCode() : 0);
        result = 31 * result + (showYValuesOnChart != null ? showYValuesOnChart.hashCode() : 0);
        result = 31 * result + (yTooltipPattern != null ? yTooltipPattern.hashCode() : 0);
        result = 31 * result + (font != null ? font.hashCode() : 0);
        result = 31 * result + (xLabelFont != null ? xLabelFont.hashCode() : 0);
        result = 31 * result + (yLabelFont != null ? yLabelFont.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "Chart{" +
                "name='" + name + '\'' +
                ", version='" + version + '\'' +
                ", title=" + title +
                ", type=" + type +
                ", report=" + report +
                ", background=" + background +
                ", foregrounds=" + foregrounds +
                ", xColumn='" + xColumn + '\'' +
                ", yColumns='" + yColumns + '\'' +
                ", yColumnsLegends='" + yColumnsLegends + '\'' +
                ", yColumnQuery='" + yColumnQuery + '\'' +
                ", xorientation=" + xorientation +
                ", xPattern='" + xPattern + '\'' +
                ", xColor=" + xColor +
                ", yColor=" + yColor +
                ", xAxisColor=" + xAxisColor +
                ", yAxisColor=" + yAxisColor +
                ", transparency=" + transparency +
                ", xLegend=" + xLegend +
                ", yLegend=" + yLegend +
                ", yFunction='" + yFunction + '\'' +
                ", xGridColor=" + xGridColor +
                ", yGridColor=" + yGridColor +
                ", xShowGrid=" + xShowGrid +
                ", yShowGrid=" + yShowGrid +
                ", xShowLabel=" + xShowLabel +
                ", yShowLabel=" + yShowLabel +
                ", showYValuesOnChart=" + showYValuesOnChart +
                ", yTooltipPattern=" + yTooltipPattern +
                ", font=" + font +
                ", xLabelFont=" + xLabelFont +
                ", yLabelFont=" + yLabelFont +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy