abap调用java实现折现图绘制代码
时间: 2023-10-16 07:09:02 浏览: 39
以下是一个使用Java类库JFreeChart绘制折线图并通过RFC接口传递给ABAP程序的示例代码:
Java端代码:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;
public class LineChart {
public static byte[] generateChart() throws Exception {
// 创建数据集
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(212, "S1", "2016");
dataset.addValue(504, "S1", "2017");
dataset.addValue(1520, "S1", "2018");
dataset.addValue(1845, "S1", "2019");
dataset.addValue(2190, "S1", "2020");
// 创建折线图对象
JFreeChart chart = ChartFactory.createLineChart("Sales Volume", // 图表标题
"Year", // x轴标签
"Volume", // y轴标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向
true, // 是否显示图例
true, // 是否显示提示
false // 是否生成URL链接
);
// 设置折线图属性
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(false);
chart.setAntiAlias(true);
chart.setTextAntiAlias(true);
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 16));
chart.setPadding(new RectangleInsets(5, 5, 5, 5));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
plot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));
plot.setOutlineVisible(false);
// x轴属性
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
domainAxis.setLowerMargin(0.02);
domainAxis.setUpperMargin(0.02);
domainAxis.setCategoryMargin(0.1);
// y轴属性
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));
rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setLowerBound(0);
rangeAxis.setUpperBound(2500);
// 折线图渲染器
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setSeriesPaint(0, new Color(179, 54, 35));
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesLinesVisible(0, true);
// 将折线图保存为图片
BufferedImage bi = chart.createBufferedImage(700, 400);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", baos);
return baos.toByteArray();
}
public static void main(String[] args) throws Exception {
generateChart();
}
}
```
将该代码打包成Java库,并将其注册到SAP系统中。
ABAP端代码:
```abap
REPORT ztest_jfreechart.
* 调用Java端的RFC函数
DATA: lv_java_conn TYPE REF TO cl_jco_destination,
lv_function TYPE REF TO if_jco_function,
lv_result TYPE REF TO cx_root.
TRY.
lv_java_conn = cl_jco_destination_manager=>get_destination( 'JAVA_SERVER' ).
lv_function = lv_java_conn->get_function( 'com.example.LineChart.generateChart' ).
lv_function->execute( ).
CATCH cx_jco_exception INTO lv_result.
MESSAGE lv_result->get_text( ) TYPE 'E'.
ENDTRY.
* 获取返回值
DATA: lv_image TYPE xstring.
lv_function->get_export_parameter( 'RETURN' )->get( CHANGING lv_image ).
* 将返回值保存为PNG图片
DATA: lv_file TYPE string.
lv_file = '/tmp/chart.png'.
OPEN DATASET lv_file FOR OUTPUT IN BINARY MODE.
TRANSFER lv_image TO lv_file.
CLOSE DATASET lv_file.
* 显示PNG图片
CALL FUNCTION 'WDY_BITMAP_FROM_FILE'
EXPORTING
filename = lv_file
EXCEPTIONS
cancelled = 1
file_not_fou = 2
wrong_format = 3
others = 4.
```
该ABAP程序通过RFC接口调用Java端的RFC函数,获取绘制好的折线图,并将其保存为PNG图片,并通过SAP内置函数`WDY_BITMAP_FROM_FILE`显示在屏幕上。