addlabels function of GraphicCollection support String array as labels

This commit is contained in:
wyq 2025-09-05 23:15:40 +08:00
parent eef4d36320
commit d3a1b71637
4 changed files with 83 additions and 5 deletions

View File

@ -69,7 +69,7 @@ import java.util.zip.ZipInputStream;
public static String getVersion() {
String version = GlobalUtil.class.getPackage().getImplementationVersion();
if (version == null || version.equals("")) {
version = "4.1.1";
version = "4.1.2";
}
return version;
}

View File

@ -21,6 +21,8 @@ import org.meteoinfo.common.PointD;
import org.meteoinfo.geometry.shape.*;
import org.meteoinfo.geometry.geoprocess.GeoComputation;
import org.meteoinfo.geometry.shape.*;
import org.meteoinfo.ndarray.Array;
import org.meteoinfo.ndarray.IndexIterator;
import java.util.ArrayList;
import java.util.Iterator;
@ -581,7 +583,16 @@ public class GraphicCollection extends Graphic implements Iterator {
*/
public void addLabels() {
addLabelsByColor();
labelSet.setDrawLabels(true);
}
/**
* Add labels by texts array
*
* @param texts Texts array
*/
public void addLabels(Array texts) {
addLabelsByColor(texts);
labelSet.setDrawLabels(true);
}
@ -676,6 +687,67 @@ public class GraphicCollection extends Graphic implements Iterator {
}
}
/**
* Add labels
*
* @param texts Texts array
*/
protected void addLabelsByColor(Array texts) {
texts = texts.copyIfView();
if (labelSet.isAutoDecimal()) {
double min = getMinValue();
labelSet.setDecimalDigits(MIMath.getDecimalNum(min));
}
String dFormat = "%1$." + String.valueOf(labelSet.getDecimalDigits()) + "f";
PointD aPoint;
IndexIterator iter = texts.getIndexIterator();
for (Graphic graphic : this.graphics) {
if (!iter.hasNext()) {
break;
}
ColorBreak cb = graphic.getLegend();
Shape shape = graphic.getShape();
PointShape aPS = new PointShape();
switch (shape.getShapeType()) {
case POINT:
case POINT_M:
case POINT_Z:
aPS.setPoint((PointD) ((PointShape) shape).getPoint().clone());
break;
case POLYLINE:
case POLYLINE_M:
case POLYLINE_Z:
int pIdx = ((PolylineShape) shape).getPoints().size() / 2;
aPS.setPoint((PointD) ((PolylineShape) shape).getPoints().get(pIdx - 1).clone());
break;
case POLYGON:
case POLYGON_M:
Extent aExtent = shape.getExtent();
aPoint = new PointD();
aPoint.X = ((aExtent.minX + aExtent.maxX) / 2);
aPoint.Y = ((aExtent.minY + aExtent.maxY) / 2);
aPS.setPoint(aPoint);
break;
}
LabelBreak aLP = new LabelBreak();
aLP.setText(iter.getStringNext());
if (labelSet.isColorByLegend()) {
aLP.setColor(cb.getColor());
} else {
aLP.setColor(labelSet.getLabelColor());
}
aLP.setFont(labelSet.getLabelFont());
aLP.setAlignType(labelSet.getLabelAlignType());
aLP.setYShift(labelSet.getYOffset());
aLP.setXShift(labelSet.getXOffset());
Graphic aGraphic = new Graphic(aPS, aLP);
addLabel(aGraphic);
}
}
/**
* Add label point
*
@ -686,7 +758,7 @@ public class GraphicCollection extends Graphic implements Iterator {
}
/**
* Add labels of contour layer dynamicly
* Add labels of contour layer dynamically
*
* @param sExtent View extent of MapView
*/

View File

@ -20,10 +20,11 @@ class Collection(Artist):
"""
Artist.__init__(self)
def addlabels(self, **kwargs):
def addlabels(self, texts=None, **kwargs):
"""
Add labels
:param texts: (*array of string*) Label texts. Default is ``None``, means using value of the graphics.
:param fontname: (*string*) Font name. Default is ``Arial``.
:param fontsize: (*string*) Font size. Default is ``14``.
:param bold: (*boolean*) Font bold or not. Default is ``False``.
@ -56,7 +57,12 @@ class Collection(Artist):
if not decimals is None:
labelset.setAutoDecimal(False)
labelset.setDecimalDigits(decimals)
self.addLabels()
if texts is None:
self.addLabels()
else:
texts = np.asarray(texts)
self.addLabels(texts._array)
class Point2DCollection(Collection, Point2DGraphicCollection):

View File

@ -39,7 +39,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<revision>4.1.1</revision>
<revision>4.1.2</revision>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.release>8</maven.compiler.release>