技术背景:Aspose.Words For JAVA 20.7.Jar JDK1.8。
技术问题:我使用下面代码创建了一个柱状图,并根据参数向当中添加了系列,可是我的系列是百分比,所以要加上百分号。我的代码当中设置了numberFormat.setFormatCode(“0%”);未生效,第二个问题就是如何将每个系列设置成指定的颜色。
代码如下:
/**
* 柱状图
* */
public static void columnChart( DocumentBuilder builder, String title,List<GxyItemData> entity) throws Exception {
Shape shape = builder.insertChart(ChartType.COLUMN , 432, 252);
Chart chart = shape.getChart();
String[] categories = new String[]{" "};
chart.getTitle().setText(title);//标题
ChartSeriesCollection seriesColl = chart.getSeries();
seriesColl.clear();
entity.forEach(c->{
seriesColl.add(c.getName(), categories,c.getValue4());
});
// 设置数据标签显示百分比
Iterator<ChartSeries> iterator = seriesColl.iterator();
while (iterator.hasNext()) {
ChartSeries series = iterator.next();
ChartDataLabelCollection dataLabelCollection = series.getDataLabels();
ChartDataLabel lb = dataLabelCollection.add(0);
// 设置数据标签显示值和百分比
lb.setShowValue(true);
lb.setShowPercentage(true);
// 设置百分比格式
ChartNumberFormat numberFormat = lb.getNumberFormat();
numberFormat.setFormatCode("0%");
}
double maxY = entity.stream().mapToDouble(s -> {return Arrays.stream(s.getValue4()).max().getAsDouble();}).max().getAsDouble();
ChartAxis axisY = chart.getAxisY();
if (maxY <= 100d){
axisY.getScaling().setMaximum(new AxisBound(100));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if (maxY <= 100d){
axisY.getScaling().setMaximum(new AxisBound(100.0));
axisY.getScaling().setMinimum(new AxisBound(10));
}else if (maxY <= 200d){
axisY.getScaling().setMaximum(new AxisBound(200.0));
}else if (maxY <= 300d){
axisY.getScaling().setMaximum(new AxisBound(300.0));
}else if (maxY <= 400d){
axisY.getScaling().setMaximum(new AxisBound(400.0));
}else if (maxY <= 500d){
axisY.getScaling().setMaximum(new AxisBound(500.0));
}
}
运行效果如下:
image.png (94.0 KB)
原型效果如下:
image.png (29.3 KB)
请问如何在系列当中设置百分号,并设置同意颜色,将系列名称放在柱形下方
@lzx9527, 请参阅下面显示的示例代码,了解如何将百分号添加到数据标签:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252);
Chart chart = shape.getChart();
chart.getAxisY().getNumberFormat().setFormatCode("0.00%");
ChartSeriesCollection seriesColl = chart.getSeries();
seriesColl.clear();
String[] categories = new String[]{"Category 1", "Category 2", "Category 3", "Category 4",
"Category 5", "Category 6", "Category 7", "Category 8", "Category 9"};
seriesColl.add("Series 1",
categories,
new double[]{ 0.999, 0.9836, 0.9421, 0.8792, 0.7588, 0.7241, 0.567, 0.4092, 0.3282 });
seriesColl.get(0).hasDataLabels(true);
ChartDataLabelCollection dataLabelCollection = seriesColl.get(0).getDataLabels();
dataLabelCollection.setShowValue(true);
dataLabelCollection.getNumberFormat().setFormatCode("0.00%");
doc.save("out.docx");
out.20.7.docx (8.9 KB)
要将每个系列设置为指定颜色,可以使用以下代码:
seriesColl.get(0).getFormat().getFill().setColor(Color.RED);
out.23.11.docx (8.8 KB)
请注意,该 API 在 20.7 中不可用,但在最新版本的 Aspose.Words 中可用。
要将系列名称放在列下,必须将系列名称的文本方向更改为垂直或自定义角度。 尚不支持此 API。
/**
* 柱状图
* */
public static void columnChart( DocumentBuilder builder, String title,List<GxyItemData> entity) throws Exception {
Shape shape = builder.insertChart(ChartType.COLUMN , 432, 252);
Chart chart = shape.getChart();
chart.getAxisY().getNumberFormat().setFormatCode("0.00%");
ChartSeriesCollection seriesColl = chart.getSeries();
seriesColl.clear();
String[] categories = new String[]{" "};
chart.getTitle().setText(title);//标题
entity.forEach(c->{
seriesColl.add(c.getName(), categories,c.getValue4());
});
for (int i = 0; i < seriesColl.getCount(); i++) {
ChartDataLabelCollection dataLabelCollection = seriesColl.get(i).getDataLabels();
dataLabelCollection.setShowPercentage(true);// 则显示百分比
dataLabelCollection.setShowValue(true);// 显示Y轴值。
dataLabelCollection.getNumberFormat().setFormatCode("0.00%");
seriesColl.get(i).hasDataLabels(true);
dataLabelCollection.setSeparator("\r\n");
}
double maxY = entity.stream().mapToDouble(s -> {return Arrays.stream(s.getValue4()).max().getAsDouble();}).max().getAsDouble();
ChartAxis axisY = chart.getAxisY();
if (maxY <= 100d){
axisY.getScaling().setMaximum(new AxisBound(100));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if (maxY <= 100d){
axisY.getScaling().setMaximum(new AxisBound(100.0));
axisY.getScaling().setMinimum(new AxisBound(10));
}else if (maxY <= 200d){
axisY.getScaling().setMaximum(new AxisBound(200.0));
}else if (maxY <= 300d){
axisY.getScaling().setMaximum(new AxisBound(300.0));
}else if (maxY <= 400d){
axisY.getScaling().setMaximum(new AxisBound(400.0));
}else if (maxY <= 500d){
axisY.getScaling().setMaximum(new AxisBound(500.0));
}
}
请问大佬我是这样写的 但是这样还是没有正确将百分号加到数据标签值的后面 请问是为什么?如何解决?麻烦大佬了 谢谢
这是我生成的结果:
image.png (40.9 KB)
大佬 我已经讲版本换为了23.5@ denis.shvydkiy
@lzx9527, 我将图表的所有数字除以 100 并删除了 dataLabelCollection.setShowPercentage(true)。 请查看更新后的代码和生成的带有图表的文档:
java.util.List<Column> entity = Arrays.asList(
new Column("Col1", 99.9),
new Column("Col2", 98.36),
new Column("Col3", 94.21),
new Column("Col4", 87.92),
new Column("Col5", 75.88),
new Column("Col6", 72.41),
new Column("Col7", 156.7),
new Column("Col8", 40.92),
new Column("Col9", 32.82));
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertChart(ChartType.COLUMN , 432, 252);
Chart chart = shape.getChart();
chart.getAxisY().getNumberFormat().setFormatCode("0.00%");
ChartSeriesCollection seriesColl = chart.getSeries();
seriesColl.clear();
String[] categories = new String[]{" "};
chart.getTitle().setText("Title");//标题
entity.forEach(c->{
// 所有数字都除以 100
var values = Arrays.stream(c.getValue4()).map(x -> x / 100.0 );
seriesColl.add(c.getName(), categories, values.toArray());
});
for (int i = 0; i < seriesColl.getCount(); i++) {
ChartSeries series = seriesColl.get(i);
series.hasDataLabels(true);
ChartDataLabelCollection dataLabelCollection = series.getDataLabels();
dataLabelCollection.setShowValue(true);// 显示Y轴值。
dataLabelCollection.getNumberFormat().setFormatCode("0.00%");
dataLabelCollection.setSeparator("\r\n");
}
double maxY = entity.stream().mapToDouble(s -> {return Arrays.stream(s.getValue4()).max().getAsDouble();}).max().getAsDouble();
System.out.println(maxY);
ChartAxis axisY = chart.getAxisY();
// 所有数字都除以 100
if (maxY <= 100d){
axisY.getScaling().setMaximum(new AxisBound(1));
axisY.getScaling().setMinimum(new AxisBound(0));
}else if (maxY <= 100d){
axisY.getScaling().setMaximum(new AxisBound(1));
axisY.getScaling().setMinimum(new AxisBound(0.1));
}else if (maxY <= 200d){
axisY.getScaling().setMaximum(new AxisBound(2));
}else if (maxY <= 300d){
axisY.getScaling().setMaximum(new AxisBound(3));
}else if (maxY <= 400d){
axisY.getScaling().setMaximum(new AxisBound(4));
}else if (maxY <= 500d){
axisY.getScaling().setMaximum(new AxisBound(5));
}
doc.save("out.23.5.docx");
out.23.5.docx (8.9 KB)