Browse Source

CHART-11709 代码质量

feature/big-screen
白岳 5 years ago
parent
commit
3f3c674558
  1. 48
      designer-chart/src/main/java/com/fr/design/chartx/component/combobox/ColorSchemeComboBox.java
  2. 74
      designer-chart/src/main/java/com/fr/van/chart/designer/component/VanChartFillStylePane.java

48
designer-chart/src/main/java/com/fr/design/chartx/component/combobox/ColorSchemeComboBox.java

@ -93,6 +93,43 @@ public class ColorSchemeComboBox extends UIComboBox {
return colorInfo; return colorInfo;
} }
public SelectType getSelectType() {
int selectedIndex = this.getSelectedIndex();
int itemCount = this.getItemCount();
if (selectedIndex == itemCount - 1) {
return SelectType.GRADATION_COLOR;
}
if (selectedIndex == itemCount - 2) {
return SelectType.COMBINATION_COLOR;
}
if (selectedIndex == 0) {
return SelectType.DEFAULT;
}
return SelectType.NORMAL;
}
public void setSelectType(SelectType selectType) {
int itemCount = this.getItemCount();
switch (selectType) {
case DEFAULT:
setSelectedIndex(0);
break;
case GRADATION_COLOR:
setSelectedIndex(itemCount - 1);
break;
case COMBINATION_COLOR:
setSelectedIndex(itemCount - 2);
break;
}
}
public enum SelectType {
DEFAULT,
COMBINATION_COLOR,
GRADATION_COLOR,
NORMAL
}
public class ColorInfo { public class ColorInfo {
@ -134,6 +171,8 @@ public class ColorSchemeComboBox extends UIComboBox {
private static final int HEIGHT = 20; private static final int HEIGHT = 20;
private static final int MAX_COUNT = 5;
@Override @Override
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
Dimension preferredSize = super.getPreferredSize(); Dimension preferredSize = super.getPreferredSize();
@ -155,8 +194,9 @@ public class ColorSchemeComboBox extends UIComboBox {
comp.setText(BLANK_SPACE + schemeName); comp.setText(BLANK_SPACE + schemeName);
} else { } else {
FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont()); FontMetrics fontMetrics = comp.getFontMetrics(comp.getFont());
double width = (HEIGHT - 2 * Y) * 5; double width = (HEIGHT - 2 * Y) * MAX_COUNT;
String fill = BLANK_SPACE; String fill = BLANK_SPACE;
//图形和文字之间留的宽度大于3倍的X
while (fontMetrics.stringWidth(fill) < width + 3 * X) { while (fontMetrics.stringWidth(fill) < width + 3 * X) {
fill += BLANK_SPACE; fill += BLANK_SPACE;
} }
@ -184,7 +224,7 @@ public class ColorSchemeComboBox extends UIComboBox {
private void drawGradient(Graphics2D g2d, List<Color> colors) { private void drawGradient(Graphics2D g2d, List<Color> colors) {
//上下留4px,宽度等于5倍高 //上下留4px,宽度等于5倍高
double height = HEIGHT - 2 * Y; double height = HEIGHT - 2 * Y;
double width = height * 5; double width = height * MAX_COUNT;
LinearGradientPaint linearGradientPaint = new LinearGradientPaint((float) X, (float) Y, (float) (X + width), (float) Y, new float[]{0f, 1f}, colors.toArray(new Color[colors.size()])); LinearGradientPaint linearGradientPaint = new LinearGradientPaint((float) X, (float) Y, (float) (X + width), (float) Y, new float[]{0f, 1f}, colors.toArray(new Color[colors.size()]));
g2d.setPaint(linearGradientPaint); g2d.setPaint(linearGradientPaint);
Rectangle2D rec = new Rectangle2D.Double(X, Y, width, height); Rectangle2D rec = new Rectangle2D.Double(X, Y, width, height);
@ -192,9 +232,9 @@ public class ColorSchemeComboBox extends UIComboBox {
} }
private void drawCombineColor(Graphics2D g2d, List<Color> colors) { private void drawCombineColor(Graphics2D g2d, List<Color> colors) {
int size = colors.size() > 5 ? 5 : colors.size(); int size = Math.min(colors.size(), MAX_COUNT);
double height = HEIGHT - 2 * Y; double height = HEIGHT - 2 * Y;
double width = height * 5 / size; double width = height * MAX_COUNT / size;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
g2d.setPaint(colors.get(i)); g2d.setPaint(colors.get(i));
Rectangle2D rec = new Rectangle2D.Double(X + width * i, Y, width, height); Rectangle2D rec = new Rectangle2D.Double(X + width * i, Y, width, height);

74
designer-chart/src/main/java/com/fr/van/chart/designer/component/VanChartFillStylePane.java

@ -70,8 +70,8 @@ public class VanChartFillStylePane extends BasicBeanPane<AttrFillStyle> {
@Override @Override
public void doChange() { public void doChange() {
accColors = colorAdjustPane.getColors(); accColors = colorAdjustPane.getColors();
if (styleSelectBox.getSelectedIndex() != styleSelectBox.getItemCount() - 2) { if (styleSelectBox.getSelectType() != ColorSchemeComboBox.SelectType.COMBINATION_COLOR) {
styleSelectBox.setSelectedIndex(styleSelectBox.getItemCount() - 2); styleSelectBox.setSelectType(ColorSchemeComboBox.SelectType.COMBINATION_COLOR);
} }
VanChartFillStylePane.this.revalidate(); VanChartFillStylePane.this.revalidate();
} }
@ -81,27 +81,29 @@ public class VanChartFillStylePane extends BasicBeanPane<AttrFillStyle> {
public void doChange() { public void doChange() {
gradientColors[0] = colorGradient.getSelectColorPointBtnP1().getColorInner(); gradientColors[0] = colorGradient.getSelectColorPointBtnP1().getColorInner();
gradientColors[1] = colorGradient.getSelectColorPointBtnP2().getColorInner(); gradientColors[1] = colorGradient.getSelectColorPointBtnP2().getColorInner();
if (styleSelectBox.getSelectedIndex() != styleSelectBox.getItemCount() - 1) { if (styleSelectBox.getSelectType() != ColorSchemeComboBox.SelectType.GRADATION_COLOR) {
styleSelectBox.setSelectedIndex(styleSelectBox.getItemCount() - 1); styleSelectBox.setSelectType(ColorSchemeComboBox.SelectType.GRADATION_COLOR);
} }
} }
}); });
styleSelectBox.addActionListener(new ActionListener() { styleSelectBox.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
ColorSchemeComboBox.ColorInfo selectColorInfo = styleSelectBox.getSelectColorInfo(); switch (styleSelectBox.getSelectType()) {
if (styleSelectBox.getSelectedIndex() == styleSelectBox.getItemCount() - 2) { case COMBINATION_COLOR:
colorAdjustPane.updateColor(accColors); colorAdjustPane.updateColor(accColors);
cardLayout.show(changeColorSetPane, "acc"); cardLayout.show(changeColorSetPane, "acc");
gradientSelect = false; gradientSelect = false;
} else if (styleSelectBox.getSelectedIndex() == styleSelectBox.getItemCount() - 1) { break;
case GRADATION_COLOR:
colorGradient.updateColor(gradientColors[0], gradientColors[1]); colorGradient.updateColor(gradientColors[0], gradientColors[1]);
changeColorSetPane.add(colorGradient, "gradient");
cardLayout.show(changeColorSetPane, "gradient"); cardLayout.show(changeColorSetPane, "gradient");
gradientSelect = true; gradientSelect = true;
} else if (selectColorInfo.isGradient()) { break;
default:
ColorSchemeComboBox.ColorInfo selectColorInfo = styleSelectBox.getSelectColorInfo();
if (selectColorInfo.isGradient()) {
colorGradient.updateColor(selectColorInfo.getColors().get(0), selectColorInfo.getColors().get(1)); colorGradient.updateColor(selectColorInfo.getColors().get(0), selectColorInfo.getColors().get(1));
changeColorSetPane.add(colorGradient, "gradient");
cardLayout.show(changeColorSetPane, "gradient"); cardLayout.show(changeColorSetPane, "gradient");
gradientSelect = true; gradientSelect = true;
} else { } else {
@ -109,6 +111,8 @@ public class VanChartFillStylePane extends BasicBeanPane<AttrFillStyle> {
cardLayout.show(changeColorSetPane, "acc"); cardLayout.show(changeColorSetPane, "acc");
gradientSelect = false; gradientSelect = false;
} }
break;
}
VanChartFillStylePane.this.revalidate(); VanChartFillStylePane.this.revalidate();
} }
}); });
@ -153,20 +157,20 @@ public class VanChartFillStylePane extends BasicBeanPane<AttrFillStyle> {
String fillStyleName = condition == null ? "" : condition.getFillStyleName(); String fillStyleName = condition == null ? "" : condition.getFillStyleName();
if (StringUtils.isBlank(fillStyleName)) {//兼容处理 if (StringUtils.isBlank(fillStyleName)) {//兼容处理
if (condition == null || condition.getColorStyle() == ChartConstants.COLOR_DEFAULT) { if (condition == null || condition.getColorStyle() == ChartConstants.COLOR_DEFAULT) {
styleSelectBox.setSelectedIndex(0);//默认 styleSelectBox.setSelectType(ColorSchemeComboBox.SelectType.DEFAULT);//默认
} else { } else {
int colorStyle = condition.getColorStyle(); int colorStyle = condition.getColorStyle();
if (colorStyle == ChartConstants.COLOR_GRADIENT) { if (colorStyle == ChartConstants.COLOR_GRADIENT) {
gradientColors[0] = condition.getColorIndex(0); gradientColors[0] = condition.getColorIndex(0);
gradientColors[1] = condition.getColorIndex(1); gradientColors[1] = condition.getColorIndex(1);
styleSelectBox.setSelectedIndex(styleSelectBox.getItemCount() - 1); styleSelectBox.setSelectType(ColorSchemeComboBox.SelectType.GRADATION_COLOR);
} else { } else {
int colorSize = condition.getColorSize(); int colorSize = condition.getColorSize();
accColors = new Color[colorSize]; accColors = new Color[colorSize];
for (int i = 0; i < colorSize; i++) { for (int i = 0; i < colorSize; i++) {
accColors[i] = condition.getColorIndex(i); accColors[i] = condition.getColorIndex(i);
} }
styleSelectBox.setSelectedIndex(styleSelectBox.getItemCount() - 2); styleSelectBox.setSelectType(ColorSchemeComboBox.SelectType.COMBINATION_COLOR);
} }
} }
} else { } else {
@ -176,25 +180,49 @@ public class VanChartFillStylePane extends BasicBeanPane<AttrFillStyle> {
@Override @Override
public AttrFillStyle updateBean() { public AttrFillStyle updateBean() {
switch (styleSelectBox.getSelectType()) {
case COMBINATION_COLOR:
return updateCombinationColor();
case GRADATION_COLOR:
return updateGradationColor();
case DEFAULT:
return updateDefaultColor();
default:
return updateNormalColor();
}
}
private AttrFillStyle updateCombinationColor() {
AttrFillStyle condition = new AttrFillStyle(); AttrFillStyle condition = new AttrFillStyle();
condition.clearColors(); condition.clearColors();
//自定义组合色
if (styleSelectBox.getSelectedIndex() == styleSelectBox.getItemCount() - 2) {
condition.setColorStyle(ChartConstants.COLOR_ACC); condition.setColorStyle(ChartConstants.COLOR_ACC);
for (int i = 0, length = accColors.length; i < length; i++) { for (int i = 0, length = accColors.length; i < length; i++) {
condition.addFillColor(accColors[i]); condition.addFillColor(accColors[i]);
} }
//自定义渐变色 condition.setCustomFillStyle(true);
} else if (styleSelectBox.getSelectedIndex() == styleSelectBox.getItemCount() - 1) { return condition;
}
private AttrFillStyle updateGradationColor() {
AttrFillStyle condition = new AttrFillStyle();
condition.clearColors();
condition.setColorStyle(ChartConstants.COLOR_GRADIENT); condition.setColorStyle(ChartConstants.COLOR_GRADIENT);
Color start = gradientColors[0]; Color start = gradientColors[0];
Color end = gradientColors[1]; Color end = gradientColors[1];
condition.addFillColor(start); condition.addFillColor(start);
condition.addFillColor(end); condition.addFillColor(end);
} else if (styleSelectBox.getSelectedIndex() == 0) { condition.setCustomFillStyle(true);
return condition;
}
private AttrFillStyle updateDefaultColor() {
AttrFillStyle condition = new AttrFillStyle();
condition.clearColors();
condition.setColorStyle(ChartConstants.COLOR_DEFAULT); condition.setColorStyle(ChartConstants.COLOR_DEFAULT);
} else { return condition;
}
private AttrFillStyle updateNormalColor() {
ChartPreStyleConfig manager = ChartPreStyleConfig.getInstance(); ChartPreStyleConfig manager = ChartPreStyleConfig.getInstance();
Object preStyle = manager.getPreStyle(styleSelectBox.getSelectedItem()); Object preStyle = manager.getPreStyle(styleSelectBox.getSelectedItem());
if (preStyle instanceof ChartColorMatching) { if (preStyle instanceof ChartColorMatching) {
@ -202,11 +230,7 @@ public class VanChartFillStylePane extends BasicBeanPane<AttrFillStyle> {
def.setFillStyleName(Utils.objectToString(styleSelectBox.getSelectedItem())); def.setFillStyleName(Utils.objectToString(styleSelectBox.getSelectedItem()));
return def; return def;
} else { } else {
condition.setColorStyle(ChartConstants.COLOR_DEFAULT); return updateDefaultColor();
} }
condition.setCustomFillStyle(true);
}
return condition;
} }
} }

Loading…
Cancel
Save