This commit is contained in:
parent
7aa5bc2a0e
commit
4256d6e7b2
12 changed files with 401 additions and 26 deletions
156
src/zutil/chart/AbstractChart.java
Normal file
156
src/zutil/chart/AbstractChart.java
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2013 Ziver
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
package zutil.chart;
|
||||
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.geom.Line2D;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
public abstract class AbstractChart extends JPanel{
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** The offset from the borders of the panel in pixels */
|
||||
public static final int PADDING = 20;
|
||||
|
||||
protected ChartData data;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected Rectangle chartBound;
|
||||
|
||||
|
||||
|
||||
protected void paintComponent(Graphics g){
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
Rectangle bound = drawScale( g2 );
|
||||
drawChart( g2, bound );
|
||||
}
|
||||
|
||||
protected Rectangle drawScale(Graphics2D g2){
|
||||
if( data == null )
|
||||
return null;
|
||||
|
||||
// update values
|
||||
width = this.getWidth();
|
||||
height = this.getHeight();
|
||||
Rectangle bound = new Rectangle();
|
||||
|
||||
// Values
|
||||
int stepLength = 7;
|
||||
|
||||
/////// Temp values
|
||||
// Calculate Font sizes
|
||||
FontMetrics metric = g2.getFontMetrics();
|
||||
int fontHeight = metric.getHeight();
|
||||
int fontXWidth = 0;
|
||||
int fontYWidth = 0;
|
||||
for( Point p : data.getPoints() ){
|
||||
int length = 0;
|
||||
String tmp = data.getXString( p.x );
|
||||
if( tmp != null ) length = metric.stringWidth( tmp );
|
||||
else length = metric.stringWidth( ""+p.x );
|
||||
fontXWidth = Math.max(length, fontXWidth);
|
||||
|
||||
tmp = data.getXString( p.y );
|
||||
if( tmp != null ) length = metric.stringWidth( tmp );
|
||||
else length = metric.stringWidth( ""+p.y );
|
||||
fontYWidth = Math.max(length, fontYWidth);
|
||||
}
|
||||
// Calculate origo
|
||||
Point origo = new Point( PADDING+fontYWidth+stepLength, height-PADDING-fontHeight-stepLength );
|
||||
bound.x = (int) (origo.getX()+1);
|
||||
bound.y = PADDING;
|
||||
bound.width = width-bound.x-PADDING;
|
||||
bound.height = (int) (origo.getY()-PADDING-1);
|
||||
// Calculate Axis scales
|
||||
double xScale = (double)(Math.abs(data.getMaxX())+Math.abs(data.getMinX()))/bound.width;
|
||||
double yScale = (double)(Math.abs(data.getMaxY())+Math.abs(data.getMinY()))/bound.height;
|
||||
|
||||
|
||||
/////// Draw
|
||||
// Y Axis
|
||||
g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX(), origo.getY() ));
|
||||
// X Axis
|
||||
g2.draw( new Line2D.Double( origo.getX(), origo.getY(), width-PADDING, origo.getY() ));
|
||||
// Y Axis steps and labels
|
||||
g2.draw( new Line2D.Double( origo.getX(), origo.getY(), origo.getX()-stepLength, origo.getY() ));
|
||||
g2.draw( new Line2D.Double( origo.getX(), PADDING, origo.getX()-stepLength, PADDING ));
|
||||
|
||||
// X Axis steps and labels
|
||||
g2.draw( new Line2D.Double( width-PADDING, origo.getY(), width-PADDING, origo.getY()+stepLength ));
|
||||
|
||||
// DEBUG
|
||||
/*
|
||||
g2.setColor(Color.red);
|
||||
g2.drawRect(bound.x, bound.y, bound.width, bound.height);
|
||||
*/
|
||||
return bound;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called after the chart scale has been drawn.
|
||||
* This method will draw the actual chart
|
||||
*
|
||||
* @param g is the Graphics object that will paint the chart
|
||||
* @param bound is the bounds of the chart, the drawing should not exceed this bound
|
||||
*/
|
||||
protected abstract void drawChart(Graphics2D g2, Rectangle bound);
|
||||
|
||||
|
||||
/**
|
||||
* Sets the data that will be drawn.
|
||||
*
|
||||
* @param data is the data to draw
|
||||
*/
|
||||
public void setChartData(ChartData data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a x value to ax pixel coordinate
|
||||
*
|
||||
* @param x is the x data value
|
||||
* @return pixel coordinate, or 0 if the chart have not been drawn yet.
|
||||
*/
|
||||
protected int getXCoordinate(int x){
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a y value to a y pixel coordinate
|
||||
*
|
||||
* @param y is the y data value
|
||||
* @return pixel coordinate, or 0 if the chart have not been drawn yet.
|
||||
*/
|
||||
protected int getYCoordinate(int y){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
103
src/zutil/chart/ChartData.java
Normal file
103
src/zutil/chart/ChartData.java
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2013 Ziver
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
package zutil.chart;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ChartData {
|
||||
|
||||
private HashMap<Integer,String> xStrings;
|
||||
private HashMap<Integer,String> yStrings;
|
||||
private int maxx;
|
||||
private int minx;
|
||||
private int maxy;
|
||||
private int miny;
|
||||
|
||||
private ArrayList<Point> points;
|
||||
|
||||
|
||||
public ChartData(){
|
||||
xStrings = new HashMap<Integer,String>();
|
||||
yStrings = new HashMap<Integer,String>();
|
||||
|
||||
points = new ArrayList<Point>();
|
||||
}
|
||||
|
||||
public void setXValueString(int x, String name){
|
||||
xStrings.put(x, name);
|
||||
}
|
||||
public void setYValueString(int y, String name){
|
||||
yStrings.put(y, name);
|
||||
}
|
||||
|
||||
|
||||
public void addPoint(int x, int y){
|
||||
points.add( new Point( x, y));
|
||||
setMaxMin(x, y);
|
||||
}
|
||||
public void addPoint(int y){
|
||||
points.add( new Point( maxx, y));
|
||||
maxx++;
|
||||
setMaxMin(maxx, y);
|
||||
}
|
||||
public void addPoint(String x, int y){
|
||||
xStrings.put(maxx, x);
|
||||
points.add( new Point( maxx, y));
|
||||
maxx++;
|
||||
setMaxMin(maxx, y);
|
||||
}
|
||||
|
||||
|
||||
private void setMaxMin(int x, int y){
|
||||
if( x > maxx ) maxx = x;
|
||||
else if( x < minx ) minx = x;
|
||||
|
||||
if( y > maxy ) maxx = y;
|
||||
else if( y < miny ) minx = y;
|
||||
}
|
||||
|
||||
public int getMaxX(){
|
||||
return maxx;
|
||||
}
|
||||
public int getMinX(){
|
||||
return minx;
|
||||
}
|
||||
public int getMaxY(){
|
||||
return maxy;
|
||||
}
|
||||
public int getMinY(){
|
||||
return miny;
|
||||
}
|
||||
public String getXString(int x){
|
||||
return xStrings.get(x);
|
||||
}
|
||||
public String getYString(int y){
|
||||
return yStrings.get(y);
|
||||
}
|
||||
|
||||
protected List<Point> getPoints(){
|
||||
return points;
|
||||
}
|
||||
}
|
||||
62
src/zutil/chart/LineChart.java
Normal file
62
src/zutil/chart/LineChart.java
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2013 Ziver
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
package zutil.chart;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.geom.Line2D;
|
||||
|
||||
public class LineChart extends AbstractChart{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void drawChart(Graphics2D g2, Rectangle bound) {
|
||||
// TODO Auto-generated method stub
|
||||
drawLine(g2, 50, 50,100,150);
|
||||
drawLine(g2, 100,150,150,100);
|
||||
drawLine(g2, 150,100,200,300);
|
||||
drawLine(g2, 200,300,250,40);
|
||||
}
|
||||
|
||||
private void drawLine(Graphics2D g2, float x1, float y1, float x2, float y2){
|
||||
// Shadow
|
||||
g2.setPaint(new Color(220,220,220));
|
||||
g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
||||
g2.draw( new Line2D.Float(x1, y1+2, x2, y2+2));
|
||||
// Smoth shadow
|
||||
g2.setPaint(new Color(230,230,230));
|
||||
g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
||||
g2.draw( new Line2D.Float(x1+1, y1+3, x2-1, y2+3));
|
||||
|
||||
// Line border
|
||||
g2.setPaint(new Color(255,187,187));
|
||||
g2.setStroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
||||
g2.draw( new Line2D.Float(x1, y1, x2, y2));
|
||||
|
||||
// Line
|
||||
g2.setPaint(new Color(237,28,36));
|
||||
g2.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
||||
g2.draw( new Line2D.Float(x1, y1, x2, y2));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue