📄 histogram.java
字号:
//Title: Histogram.java
//Version:
//Copyright: Copyright (c) 1999
//Author: Jehan Bing
//Company: SRI International
//Description: Bar chart
/**
* The contents of this file are subject to the OAA Community Research
* 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.ai.sri.com/~oaa/. Software distributed under the License
* is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific language governing
* rights and limitations under the License. Portions of the software are
* Copyright (c) SRI International, 1999. All rights reserved.
* "OAA" is a registered trademark, and "Open Agent Architecture" is a
* trademark, of SRI International, a California nonprofit public benefit
* corporation.
*/
package com.sri.oaa2.agt.monitor;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Displays and manage an histogram graph.
*
*/
public class Histogram extends JPanel {
/**
* List of the bars.
*
*/
private Vector bars = new Vector();
/**
* Maximum value a bar can display.
*
*/
private float maxValue = 100;
/**
* Text to display before the value.
*
*/
private String prefix = "";
/**
* Text to display after the value.
*
*/
private String suffix = "%";
/**
* Determines whether the value of the bars are displayed.
*
*/
private boolean valueDisplayed = true;
/**
* Nnumber of decimal to display.
*
*/
private int precision = 2;
/**
* Determines whether the null value is displayed in a bar.
*
*/
private boolean zeroShown = false;
/**
* Horizontal alignment of the value in the bars.
*
*/
private float textAlignmentX = 0.5f;
/**
* space between two bars.
*
*/
private int vgap = 5;
/**
* Space between a bar and its label.
*
*/
private int hgap = 5;
private boolean shouldRoundValue = false;
private boolean centered = false;
private boolean leftJustified = false;
private boolean showZeroValue = false;
/**
* Creates an histogram.
*
*/
public Histogram() {
setLayout(new GridLayout(0, 1));
// setLayout(new GridBagLayout());
}
/**
* Redo the layout.
*
*/
protected void resetLocation() {
removeAll();
GridBagConstraints c = new GridBagConstraints();
int numBars = bars.size();
for (int i = 0; i < numBars; i++) {
add((JComponent)bars.elementAt(i));
}
revalidate();
}
/**
* Create a new bar in the histogram.
*
* @param color color of the bar
*/
public void addSeries(String legend, Color color) {
HistogramBar bar = new HistogramBar();
bar.setBarColor(color);
bar.setMaxValue(maxValue);
bar.setPrefix(legend);
bar.setSuffix(suffix);
bar.setValueDisplayed(valueDisplayed);
bar.setPrecision(precision);
bar.setTextAlignmentX(textAlignmentX);
bar.setZeroShown(zeroShown);
bar.setCentered(centered);
bar.setLeftJustified(leftJustified);
if (bars.size() >= 1) {
int spaceBefore = Math.round(vgap/2.0f);
bar.setSpaceBefore(spaceBefore);
bar.setSpaceAfter(0);
((HistogramBar)bars.elementAt(bars.size()-1)).setSpaceAfter(vgap-spaceBefore);
}
bars.addElement(bar);
resetLocation();
}
/**
* Returns the number of bars.
*
*/
public int getNumBars() {
return bars.size();
}
/**
* Remove all the bars.
*
*/
public void removeAllBars() {
bars.removeAllElements();
resetLocation(); // will remove the components
}
/**
* Remove a bar.
*
* @param numBar bar number.
*/
public void removeBar(int numBar) {
bars.removeElementAt(numBar);
resetLocation(); // will remove the components
}
/**
* Returns the bar at the specified position.
*
* @param i bar number.
*/
public HistogramBar barAt(int i) {
return (HistogramBar) bars.elementAt(i);
}
public void setRoundsValue(boolean r) {
shouldRoundValue = r;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setRoundsValue(r);
}
}
public boolean roundsValue() {
return shouldRoundValue;
}
/**
* Sets the number of decimal to display.
*
* @param precision number of decimal.
*/
public void setPrecision(int precision) {
this.precision = precision;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setPrecision(precision);
}
}
/**
* Returns the number of decimal to display.
*
*/
public int getPrecision() {
return precision;
}
/**
* Determines whether a null value is displayed.
*
* @param b
*/
public void setZeroShown(boolean b) {
zeroShown = b;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setZeroShown(b);
}
}
public void setShowZeroValue(boolean b) {
setZeroShown(b);
}
/**
* Returns whether a value of 0 is displayed.
*
*/
public boolean isZeroShown() {
return zeroShown;
}
public boolean showsZeroValue() {
return isZeroShown();
}
/**
* Sets the horizontal alignment of a value in a bar. 0=left, 1=right, 0.5=center, ...
*
* @param alignment the alignment.
*/
public void setTextAlignmentX(float alignment) {
textAlignmentX = alignment;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setTextAlignmentX(alignment);
}
}
/**
* Returns the horizontal alignment of the value in a bar.
*
*/
public float getTextAlignmentX() {
return textAlignmentX;
}
public void setCentered(boolean b) {
centered = b;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setCentered(b);
}
}
public boolean isCentered() {
return centered;
}
public void setLeftJustified(boolean b) {
leftJustified = b;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setLeftJustified(b);
}
}
public boolean isLeftJustified() {
return leftJustified;
}
/**
* Sets the maximum value a bar can display.
*
* @param value max value.
*/
public void setMaxValue(float value) {
maxValue = value;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setMaxValue(maxValue);
}
}
/**
* Returns the maximum value a bar can display.
*
*/
public float getMaxValue() {
return maxValue;
}
/**
* Sets the space between the bars.
*
* @param value space.
*/
public void setVgap(int value) {
vgap = value;
if (bars.size() >= 2) {
int spaceBefore = Math.round(value/2.0f);
int spaceAfter = value-spaceBefore;
for (int i = bars.size()-1; i >= 1; i--) {
((HistogramBar)bars.elementAt(i)).setSpaceBefore(spaceBefore);
((HistogramBar)bars.elementAt(i-1)).setSpaceAfter(spaceAfter);
}
}
}
/**
* Returns the space between the bars.
*
*/
public int getVgap() {
return vgap;
}
/**
* Sets the text to displayed after the value (ex: percentage).
*
* @param prefix new text.
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setPrefix(prefix);
}
}
/**
* Returns the text displayed after the value.
*
*/
public String getPrefix() {
return prefix;
}
/**
* Sets the text to display before the value (ex: a dollar sign).
*
* @param suffix new text
*/
public void setSuffix(String suffix) {
this.suffix = suffix;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setSuffix(suffix);
}
}
/**
* Returns the text displayed before the value.
*
*/
public String getSuffix() {
return suffix;
}
/**
* Allows to display the value for each bar.
*
* @param b True to display the value, false otherwise.
*/
public void setValueDisplayed(boolean b) {
this.valueDisplayed = b;
for (int i = bars.size()-1; i >= 0; i--) {
((HistogramBar)bars.elementAt(i)).setValueDisplayed(b);
}
}
/**
* Returns whether the value for each bar is displayed.
*
*/
public boolean getValueDisplayed() {
return valueDisplayed;
}
/**
* Sets the value of a bar.
*
* @exception IllegalArgumentException thrown if the number is invalid.
* @param numBar Bar number.
* @param value New value.
*/
public void setValue(int numBar, float value) throws IllegalArgumentException {
if ((numBar < 0) || (numBar >= bars.size()))
throw new IllegalArgumentException("numBar out of bounds");
((HistogramBar)bars.elementAt(numBar)).setValue(value);
}
/**
* Returns the value of a bar.
*
* @exception IllegalArgumentException thrown if the number is invalid.
* @param numBar Bar number.
*/
public float getValue(int numBar) throws IllegalArgumentException {
if ((numBar < 0) || (numBar >= bars.size()))
throw new IllegalArgumentException("numBar out of bounds");
return ((HistogramBar)bars.elementAt(numBar)).getValue();
}
/**
* Sets the color of a bar.
*
* @exception IllegalArgumentException thrown if the number is invalid.
* @param numBar Bar number.
* @param color New color.
*/
public void setBarColor(int numBar, Color color) throws IllegalArgumentException {
if ((numBar < 0) || (numBar >= bars.size()))
throw new IllegalArgumentException("numBar out of bounds");
((HistogramBar)bars.elementAt(numBar)).setBarColor(color);
}
/**
* Returns the color of a bar.
*
* @exception IllegalArgumentException thrown if the number is invalid.
* @param numBar number of the bar.
*/
public Color getBarColor(int numBar) throws IllegalArgumentException {
if ((numBar < 0) || (numBar >= bars.size()))
throw new IllegalArgumentException("numBar out of bounds");
return ((HistogramBar)bars.elementAt(numBar)).getBarColor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -