📄 plot2d.java
字号:
*/
private static void drawTriangleDown(Graphics gx, double x,
double y, int size) {
gx.drawLine((int)(x),(int)(y+size),
(int)(x-size),(int)(y-size));
gx.drawLine((int)(x-size),(int)(y-size),
(int)(x+size),(int)(y-size));
gx.drawLine((int)(x+size),(int)(y-size),
(int)(x),(int)(y+size));
}
/**
* Draws a data point at a given set of panel coordinates at a given
* size and connects a line to the previous point.
* @param x the x coord
* @param y the y coord
* @param xprev the x coord of the previous point
* @param yprev the y coord of the previous point
* @param size the size of the point
* @param shape the shape of the data point (square is reserved for nominal
* error data points). Shapes: 0=x, 1=plus, 2=diamond, 3=triangle(up),
* 4 = triangle (down).
* @param gx the graphics context
*/
protected static void drawDataPoint(double x,
double y,
double xprev,
double yprev,
int size,
int shape,
Graphics gx) {
drawDataPoint(x,y,size,shape,gx);
// connect a line to the previous point
gx.drawLine((int)x, (int)y, (int)xprev, (int)yprev);
}
/**
* Draws a data point at a given set of panel coordinates at a given
* size.
* @param x the x coord
* @param y the y coord
* @param size the size of the point
* @param shape the shape of the data point (square is reserved for nominal
* error data points). Shapes: 0=x, 1=plus, 2=diamond, 3=triangle(up),
* 4 = triangle (down).
* @param gx the graphics context
*/
protected static void drawDataPoint(double x,
double y,
int size,
int shape,
Graphics gx) {
Font lf = new Font("Monospaced", Font.PLAIN, 12);
FontMetrics fm = gx.getFontMetrics(lf);
if (size == 0) {
size = 1;
}
if (shape != ERROR_SHAPE && shape != MISSING_SHAPE) {
shape = shape % 5;
}
switch (shape) {
case X_SHAPE:
drawX(gx, x, y, size);
break;
case PLUS_SHAPE:
drawPlus(gx, x, y, size);
break;
case DIAMOND_SHAPE:
drawDiamond(gx, x, y, size);
break;
case TRIANGLEUP_SHAPE:
drawTriangleUp(gx, x, y, size);
break;
case TRIANGLEDOWN_SHAPE:
drawTriangleDown(gx, x, y, size);
break;
case ERROR_SHAPE: // draws the nominal error shape
gx.drawRect((int)(x-size),(int)(y-size),(size*2),(size*2));
break;
case MISSING_SHAPE:
int hf = fm.getAscent();
int width = fm.stringWidth("M");
gx.drawString("M",(int)(x-(width / 2)), (int)(y+(hf / 2)));
break;
}
}
/**
* Updates the perturbed values for the plots when the jitter value is
* changed
*/
private void updatePturb() {
double xj=0;
double yj=0;
for (int j=0;j<m_plots.size();j++) {
PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(j));
for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) {
if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) ||
temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) {
} else {
if (m_JitterVal > 0) {
xj = m_JRand.nextGaussian();
yj = m_JRand.nextGaussian();
}
temp_plot.m_pointLookup[i][2] =
pturbX(temp_plot.m_pointLookup[i][0],xj);
temp_plot.m_pointLookup[i][3] =
pturbY(temp_plot.m_pointLookup[i][1],yj);
}
}
}
}
/**
* Fills the lookup caches for the plots. Also calculates errors for
* numeric predictions (if any) in plots
*/
private void fillLookup() {
for (int j=0;j<m_plots.size();j++) {
PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(j));
if (temp_plot.m_plotInstances.numInstances() > 0 &&
temp_plot.m_plotInstances.numAttributes() > 0) {
for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) {
if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) ||
temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) {
temp_plot.m_pointLookup[i][0] = Double.NEGATIVE_INFINITY;
temp_plot.m_pointLookup[i][1] = Double.NEGATIVE_INFINITY;
} else {
double x = convertToPanelX(temp_plot.m_plotInstances.
instance(i).value(m_xIndex));
double y = convertToPanelY(temp_plot.m_plotInstances.
instance(i).value(m_yIndex));
temp_plot.m_pointLookup[i][0] = x;
temp_plot.m_pointLookup[i][1] = y;
}
}
}
}
}
/**
* Draws the data points and predictions (if provided).
* @param gx the graphics context
*/
private void paintData(Graphics gx) {
for (int j=0;j<m_plots.size();j++) {
PlotData2D temp_plot = (PlotData2D)(m_plots.elementAt(j));
for (int i=0;i<temp_plot.m_plotInstances.numInstances();i++) {
if (temp_plot.m_plotInstances.instance(i).isMissing(m_xIndex) ||
temp_plot.m_plotInstances.instance(i).isMissing(m_yIndex)) {
} else {
double x = (temp_plot.m_pointLookup[i][0] +
temp_plot.m_pointLookup[i][2]);
double y = (temp_plot.m_pointLookup[i][1] +
temp_plot.m_pointLookup[i][3]);
double prevx = 0;
double prevy = 0;
if (i > 0) {
prevx = (temp_plot.m_pointLookup[i - 1][0] +
temp_plot.m_pointLookup[i - 1][2]);
prevy = (temp_plot.m_pointLookup[i - 1][1] +
temp_plot.m_pointLookup[i - 1][3]);
}
int x_range = (int)x - m_XaxisStart;
int y_range = (int)y - m_YaxisStart;
if (x_range >= 0 && y_range >= 0) {
if (m_drawnPoints[x_range][y_range] == i
|| m_drawnPoints[x_range][y_range] == 0
|| temp_plot.m_displayAllPoints == true) {
m_drawnPoints[x_range][y_range] = i;
if (temp_plot.m_plotInstances.attribute(m_cIndex).isNominal()) {
if (temp_plot.m_plotInstances.attribute(m_cIndex).numValues() >
m_colorList.size() &&
!temp_plot.m_useCustomColour) {
extendColourMap(temp_plot.m_plotInstances.
attribute(m_cIndex).numValues());
}
Color ci;
if (temp_plot.m_plotInstances.instance(i).
isMissing(m_cIndex)) {
ci = Color.gray;
} else {
int ind = (int)temp_plot.m_plotInstances.instance(i).
value(m_cIndex);
ci = (Color)m_colorList.elementAt(ind);
}
if (!temp_plot.m_useCustomColour) {
gx.setColor(ci);
} else {
gx.setColor(temp_plot.m_customColour);
}
if (temp_plot.m_plotInstances.instance(i).
isMissing(m_cIndex)) {
if (temp_plot.m_connectPoints[i] == true) {
drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i],
MISSING_SHAPE,gx);
} else {
drawDataPoint(x,y,temp_plot.m_shapeSize[i],
MISSING_SHAPE,gx);
}
} else {
if (temp_plot.m_shapeType[i] == CONST_AUTOMATIC_SHAPE) {
if (temp_plot.m_connectPoints[i] == true) {
drawDataPoint(x,y,prevx,prevy,
temp_plot.m_shapeSize[i],j,gx);
} else {
drawDataPoint(x,y,temp_plot.m_shapeSize[i],j,gx);
}
} else {
if (temp_plot.m_connectPoints[i] == true) {
drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i],
temp_plot.m_shapeType[i],gx);
} else {
drawDataPoint(x,y,temp_plot.m_shapeSize[i],
temp_plot.m_shapeType[i],gx);
}
}
}
} else {
double r;
Color ci = null;
if (!temp_plot.m_plotInstances.instance(i).
isMissing(m_cIndex)) {
r = (temp_plot.m_plotInstances.instance(i).
value(m_cIndex) - m_minC) / (m_maxC - m_minC);
r = (r * 240) + 15;
ci = new Color((int)r,150,(int)(255-r));
} else {
ci = Color.gray;
}
if (!temp_plot.m_useCustomColour) {
gx.setColor(ci);
} else {
gx.setColor(temp_plot.m_customColour);
}
if (temp_plot.m_plotInstances.instance(i).
isMissing(m_cIndex)) {
if (temp_plot.m_connectPoints[i] == true) {
drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i],
MISSING_SHAPE,gx);
} else {
drawDataPoint(x,y,temp_plot.m_shapeSize[i],
MISSING_SHAPE,gx);
}
} else {
if (temp_plot.m_shapeType[i] == CONST_AUTOMATIC_SHAPE) {
if (temp_plot.m_connectPoints[i] == true) {
drawDataPoint(x,y,prevx,prevy,
temp_plot.m_shapeSize[i],j,gx);
} else {
drawDataPoint(x,y,temp_plot.m_shapeSize[i],j,gx);
}
} else {
if (temp_plot.m_connectPoints[i] == true) {
drawDataPoint(x,y,prevx,prevy,temp_plot.m_shapeSize[i],
temp_plot.m_shapeType[i],gx);
} else {
drawDataPoint(x,y,temp_plot.m_shapeSize[i],
temp_plot.m_shapeType[i],gx);
}
}
}
}
}
}
}
}
}
}
/*
public void determineAxisPositions(Graphics gx) {
setFonts(gx);
int mxs = m_XaxisStart;
int mxe = m_XaxisEnd;
int mys = m_YaxisStart;
int mye = m_YaxisEnd;
m_axisChanged = false;
int h = this.getHeight();
int w = this.getWidth();
int hf = m_labelMetrics.getAscent();
int mswx=0;
int mswy=0;
// determineBounds();
int fieldWidthX = (int)((Math.log(m_maxX)/Math.log(10)))+1;
int precisionX = 1;
if ((Math.abs(m_maxX-m_minX) < 1) && ((m_maxY-m_minX) != 0)) {
precisionX = (int)Math.abs(((Math.log(Math.abs(m_maxX-m_minX)) /
Math.log(10))))+1;
}
String maxStringX = Utils.doubleToString(m_maxX,
fieldWidthX+1+precisionX
,precisionX);
mswx = m_labelMetrics.stringWidth(maxStringX);
int fieldWidthY = (int)((Math.log(m_maxY)/Math.log(10)))+1;
int precisionY = 1;
if (Math.abs((m_maxY-m_minY)) < 1 && ((m_maxY-m_minY) != 0)) {
precisionY = (int)Math.abs(((Math.log(Math.abs(m_maxY-m_minY)) /
Math.log(10))))+1;
}
String maxStringY = Utils.doubleToString(m_maxY,
fieldWidthY+1+precisionY
,precisionY);
String minStringY = Utils.doubleToString(m_minY,
fieldWidthY+1+precisionY
,precisionY);
if (m_plotInstances.attribute(m_yIndex).isNumeric()) {
mswy = (m_labelMetrics.stringWidth(maxStringY) >
m_labelMetrics.stringWidth(minStringY))
? m_labelMetrics.stringWidth(maxStringY)
: m_labelMetrics.stringWidth(minStringY);
} else {
mswy = m_labelMetrics.stringWidth("MM");
}
m_YaxisStart = m_axisPad;
m_XaxisStart = 0+m_axisPad+m_tickSize+mswy;
m_XaxisEnd = w-m_axisPad-(mswx/2);
m_YaxisEnd = h-m_axisPad-(2 * hf)-m_tickSize;
} */
/**
* Draws the axis and a spectrum if the colouring attribute is numeric
* @param gx the graphics context
*/
private void paintAxis(Graphics gx) {
setFonts(gx);
int mxs = m_XaxisStart;
int mxe = m_XaxisEnd;
int mys = m_YaxisStart;
int mye = m_YaxisEnd;
m_plotResize = false;
int h = this.getHeight();
int w = this.getWidth();
int hf = m_labelMetrics.getAscent();
int mswx=0;
int mswy=0;
// determineBounds();
int precisionXmax = 1;
int precisionXmin = 1;
int precisionXmid = 1;
/*if ((Math.abs(m_maxX-m_minX) < 1) && ((m_maxY-m_minX) != 0)) {
precisionX = (int)Math.abs(((Math.log(Math.abs(m_maxX-m_minX)) /
Math.log(10))))+1;
} */
int whole = (int)Math.abs(m_maxX);
double decimal = Math.abs(m_maxX) - whole;
int nondecimal;
nondecimal = (whole > 0)
? (int)(Math.log(whole) / Math.log(10))
: 1;
precisionXmax = (decimal > 0)
? (int)Math.abs(((Math.log(Math.abs(m_maxX)) /
Math.log(10))))+2
: 1;
if (precisionXmax > VisualizeUtils.MAX_PRECISION) {
precisionXmax = 1;
}
String maxStringX = Utils.doubleToString(m_maxX,
nondecimal+1+precisionXmax
,precisionXmax);
whole = (int)Math.abs(m_minX);
decimal = Math.abs(m_minX) - whole;
nondecimal = (whole > 0)
? (int)(Math.log(whole) / Math.log(10))
: 1;
precisionXmin = (decimal > 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -