📄 sdachart.java
字号:
min = (int) (minValue / minUnit) * minUnit; sideInt = (int) ((maxValue > max ? 1 : 0) + (minValue < min ? 1 : 0)); //所有的数量 allInt = (int) ((max - min + sideInt) / minUnit); if (ft.stringWidth(String.valueOf(max)) * allInt >= getWidth() - sint) { minUnit = 5 * getMulValue(i); max = (int) (maxValue / minUnit) * minUnit; min = (int) (minValue / minUnit) * minUnit; sideInt = (int) ((maxValue > max ? 1 : 0) + (minValue < min ? 1 : 0)); //所有的数量 allInt = (int) ((max - min + sideInt) / minUnit); if (ft.stringWidth(String.valueOf(max)) * allInt >= getWidth() - sint) { continue; } else { break; } } else { break; } } else { break; } } //获取到合适的 } else { //分割太稀(稠化) int iInt = 0; int mulInt = 1; for (int i = -1; i > -64; i--) { iInt = i; mulInt = 5; minUnit = 5 * getMulValue(i); max = (int) (maxValue / minUnit) * minUnit; min = (int) (minValue / minUnit) * minUnit; sideInt = (int) ((maxValue > max ? 1 : 0) + (minValue < min ? 1 : 0)); //所有的数量 allInt = (int) ((max - min + sideInt) / minUnit); if (ft.stringWidth(String.valueOf(max)) * allInt < getWidth() - sint) { mulInt = 2; minUnit = 2 * getMulValue(i); max = (int) (maxValue / minUnit) * minUnit; min = (int) (minValue / minUnit) * minUnit; sideInt = (int) ((maxValue > max ? 1 : 0) + (minValue < min ? 1 : 0)); //所有的数量 allInt = (int) ((max - min + sideInt) / minUnit); if (ft.stringWidth(String.valueOf(max)) * allInt < getWidth() - sint) { mulInt = 1; minUnit = getMulValue(i); max = (int) (maxValue / minUnit) * minUnit; min = (int) (minValue / minUnit) * minUnit; sideInt = (int) ((maxValue > max ? 1 : 0) + (minValue < min ? 1 : 0)); //所有的数量 allInt = (int) ((max - min + sideInt) / minUnit); if (ft.stringWidth(String.valueOf(max)) * allInt < getWidth() - sint) { continue; } else { break; } } else { break; } } else { break; } } if (mulInt == 5) { mulInt = 1; } if (mulInt == 2) { mulInt = 5; } minUnit = mulInt * getMulValue(iInt + 1); } maxXValue = maxValue > max ? max + minUnit : max; minXValue = minValue < min ? min - minUnit : min; return minUnit; } //画标尺 private void paintScale(Graphics g, boolean canPaint) { int thisWidth = getWidth(); int thisHeight = getHeight(); Font ft = getFont(); //排序 sortValue(); //Y方向 double yScale = calYScale(); //画y刻度 yLen = thisHeight - (showFootText ? 2 * ft.getHeight() + 4 : ft.getHeight() + 4) - (showTitleText ? ft.getHeight() : 0); int id = 0; double scaleValue = minYValue; //y值最大的宽度 int maxYValueWidth = ft.stringWidth(String.valueOf(maxYValue)) > ft.stringWidth(String.valueOf(minYValue)) ? ft.stringWidth(String.valueOf(maxYValue)) : ft.stringWidth(String.valueOf(minYValue)); int scaleLeft = maxYValueWidth + (showAxisText ? ft.getHeight() : 0); int scaleStart = thisHeight - (showFootText ? 2 * ft.getHeight() + 4 : ft.getHeight() + 4); int scalepos = 0; int selfLen = 0; if (canPaint) { while (scaleValue <= maxYValue) { scalepos = (int) (yLen * id / ((maxYValue - minYValue) / yScale)); if (scaleStart - scalepos - ft.getHeight() / 2 + 1 >= 0) { selfLen = maxYValueWidth - ft.stringWidth(String.valueOf(scaleValue)); drawString(g, String.valueOf(scaleValue), showAxisText ? ft.getHeight() + selfLen : selfLen, scaleStart - scalepos - ft.getHeight() / 2 + 1); } if (showRefLine) { if (scaleValue != minYValue) { g.setColor(refLineColor); g.setStrokeStyle(Graphics.DOTTED); drawLine(g, scaleLeft, scaleStart - scalepos, thisWidth, scaleStart - scalepos); g.setColor(foreColor); g.setStrokeStyle(Graphics.SOLID); } } //画刻度 drawLine(g, scaleLeft, scaleStart - scalepos, scaleLeft + 4, scaleStart - scalepos); scaleValue += yScale; id++; } } //竖线 if (canPaint) { drawLine(g, scaleLeft + 4, showTitleText ? ft.getHeight() : 0, scaleLeft + 4, scaleStart); } yStartPos = scaleStart; //X方向 double xScale = calXScale(); //画x刻度 //ys = (int) ((thisWidth - (scaleLeft + 4)) / ((maxXValue - minXValue) / xScale)); xLen = thisWidth - (scaleLeft + 4); id = 0; scaleValue = minXValue; int scaleTop = showFootText ? 2 * ft.getHeight() + 4 : ft.getHeight() + 4; //x值 if (canPaint) { while (scaleValue <= maxXValue) { scalepos = (int) (xLen * id / ((maxXValue - minXValue) / xScale)); if (scaleValue < maxXValue) { drawString(g, String.valueOf(scaleValue), scaleLeft + 4 + scalepos - (ft.stringWidth(String.valueOf(scaleValue))) / 2, thisHeight - scaleTop + 4); } //画刻度 if (showRefLine) { if (scaleValue != minXValue) { g.setColor(refLineColor); g.setStrokeStyle(Graphics.DOTTED); drawLine(g, scaleLeft + 4 + scalepos, thisHeight - scaleTop, scaleLeft + 4 + scalepos, yStartPos - yLen); g.setColor(foreColor); g.setStrokeStyle(Graphics.SOLID); } } drawLine(g, scaleLeft + 4 + scalepos, thisHeight - scaleTop, scaleLeft + 4 + scalepos, thisHeight - scaleTop + 4); scaleValue += xScale; id++; } } //横线 if (canPaint) { drawLine(g, scaleLeft + 4, getHeight() - scaleTop, getWidth(), getHeight() - scaleTop); } xStartPos = scaleLeft + 4; //xLen = thisWidth - xStartPos; SetClip(g); //外框 if (canPaint) { drawRect(g, xStartPos, yStartPos - yLen, xLen, yLen); } //文字 if (canPaint) { if (showAxisText) { int textWidth = axisText.length() * ft.getHeight(); int fontHeight = ft.getHeight(); int startPos = (getHeight() - textWidth) / 2 + 1; char ch = 0; for (int i = 0; i < axisText.length(); i++) { ch = axisText.charAt(i); drawString(g, String.valueOf(ch), (fontHeight - ft.charWidth(ch)) / 2, startPos + i * fontHeight); } } } if (canPaint) { if (showFootText) { int textWidth = ft.stringWidth(footText); int fontHeight = ft.getHeight(); int startPos = (thisWidth - textWidth) / 2 + 1; drawString(g, footText, startPos, thisHeight - fontHeight); } } if (showTitleText) { int textWidth = ft.stringWidth(titleText); int startPos = (thisWidth - textWidth) / 2 + 1; drawString(g, titleText, startPos, 0); } } //画series线 private void paintLineSeries(Graphics g, SDAChartSeries series) { SDAChartSeries.PointData pd = null; SDAChartSeries.PointData pd1 = null; Font ft = getFont(); int fontHeight = ft.getHeight(); int x1 = 0, y1 = 0, x2 = 0, y2 = 0; int mack = series.getMarksBackColor(); byte type = series.getMarkValueType(); String value = ""; series.calPercent(); for (int i = 0; i < series.getDataCount() - 1; i++) { pd = series.getData(i); x1 = getXPos(pd.xValue); y1 = getYPos(pd.yValue); pd1 = series.getData(i + 1); x2 = getXPos(pd1.xValue); y2 = getYPos(pd1.yValue); //线 g.setColor(series.getLineColor()); drawLine(g, x1, y1, x2, y2); //画点 if (series.isShowPoint()) { g.setColor(pd.color); fillRect(g, x1 - 2, y1 - 2, 4, 4); g.setColor(foreColor); drawRect(g, x1 - 2, y1 - 2, 4, 4); } //画标识 if (series.isShowMarks()) { if (type == SDAChartSeries.svNone) { value = ""; } if (type == SDAChartSeries.svValue) { value = String.valueOf(pd.yValue); } if (type == SDAChartSeries.svXValue) { value = String.valueOf(pd.xValue); } if (type == SDAChartSeries.svPercent) { value = pd.percent; } if (type == SDAChartSeries.svXYValue) { value = pd.xValue + "/" + pd.yValue; } if (type == SDAChartSeries.svXValuePercent) { value = pd.xValue + "/" + pd.percent; } if (type == SDAChartSeries.svLabel) { value = pd.label; } if (type == SDAChartSeries.svLabelValue) { value = pd.label + "/" + pd.yValue; } if (type == SDAChartSeries.svLabelPercent) { value = pd.label + "/" + pd.percent; } if (value.length() > 0) { g.setColor(foreColor); drawLine(g, x1, y1, x1, y1 - 6); g.setColor(mack); fillRect(g, x1 - (ft.stringWidth(value)) / 2 + 1, y1 - 6 - fontHeight, ft.stringWidth(value) + 3, fontHeight); g.setColor(foreColor); drawRect(g, x1 - (ft.stringWidth(value)) / 2 + 1, y1 - 6 - fontHeight, ft.stringWidth(value) + 3, fontHeight); drawString(g, value, x1 - (ft.stringWidth(value)) / 2 + 3, y1 - 6 - fontHeight); } } } if (pd1 != null) { if (series.isShowPoint()) { g.setColor(pd1.color); fillRect(g, x2 - 2, y2 - 2, 4, 4); g.setColor(foreColor); drawRect(g, x2 - 2, y2 - 2, 4, 4); } if (series.isShowMarks()) { if (type == SDAChartSeries.svNone) { value = ""; } if (type == SDAChartSeries.svValue) { value = String.valueOf(pd1.yValue); } if (type == SDAChartSeries.svXValue) { value = String.valueOf(pd1.xValue); } if (type == SDAChartSeries.svPercent) { value = pd1.percent; } if (type == SDAChartSeries.svXYValue) { value = pd1.xValue + "/" + pd1.yValue; } if (type == SDAChartSeries.svXValuePercent) { value = pd1.xValue + "/" + pd1.percent; } if (type == SDAChartSeries.svLabel) { value = pd1.label; } if (type == SDAChartSeries.svLabelValue) { value = pd1.label + "/" + pd1.yValue; } if (type == SDAChartSeries.svLabelPercent) { value = pd1.label + "/" + pd1.percent; } if (value.length() > 0) { g.setColor(foreColor); drawLine(g, x2, y2, x2, y2 - 6); g.setColor(mack); fillRect(g, x2 - (ft.stringWidth(value)) / 2 + 1, y2 - 6 - fontHeight, ft.stringWidth(value) + 3, fontHeight); g.setColor(foreColor); drawRect(g, x2 - (ft.stringWidth(value)) / 2 + 1, y2 - 6 - fontHeight, ft.stringWidth(value) + 3, fontHeight); drawString(g, value, x2 - (ft.stringWidth(value)) / 2 + 3, y2 - 6 - fontHeight); } } } } //散点图 private void paintPointSeries(Graphics g, SDAChartSeries series) { SDAChartSeries.PointData pd = null; Font ft = getFont(); int fontHeight = ft.getHeight(); int x1 = 0, y1 = 0; int mack = series.getMarksBackColor(); byte type = series.getMarkValueType(); String value = ""; series.calPercent(); for (int i = 0; i < series.getDataCount(); i++) { pd = series.getData(i); x1 = getXPos(pd.xValue); y1 = getYPos(pd.yValue); //画点 if (series.isShowPoint()) { g.setColor(pd.color); fillRect(g, x1 - 2, y1 - 2, 4, 4); g.setColor(foreColor); drawRect(g, x1 - 2, y1 - 2, 4, 4); } //画标识
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -