- Code: Select all
private static void addBar(WordprocessingMLPackage wordPackage) throws Exception {
Chart chartPart = new Chart(new PartName("/word/charts/chart1.xml"));
CTChartSpace ch = new CTChartSpace();
CTBoolean bool = new CTBoolean();
bool.setVal(Boolean.FALSE);
ch.setDate1904(bool);
CTTextLanguageID lang = new CTTextLanguageID();
lang.setVal("en-US");
ch.setLang(lang);
CTChart chart = new CTChart();
CTPlotArea ctp = new CTPlotArea();
CTBarChart objBar = new CTBarChart();
CTBarDir barDir = new CTBarDir();
barDir.setVal(STBarDir.BAR);
objBar.setBarDir(barDir);
CTBarGrouping ctbarGr = new CTBarGrouping();
ctbarGr.setVal(STBarGrouping.CLUSTERED);
objBar.setGrouping(ctbarGr);
CTBarSer barSer = new CTBarSer();
// 配置系列名称
CTSerTx ctSerTx = new CTSerTx();
CTStrRef ctSerRef = new CTStrRef();
ctSerRef.setF("Sheet1!$B$1");
CTStrData ctStrData = new CTStrData();
CTUnsignedInt unsign1 = new CTUnsignedInt();
unsign1.setVal(1);
ctStrData.setPtCount(unsign1);
CTStrVal ctStrVal = new CTStrVal();
ctStrVal.setIdx(0);
ctStrVal.setV("Sales");
ctStrData.getPt().add(ctStrVal);
ctSerRef.setStrCache(ctStrData);
ctSerTx.setStrRef(ctSerRef);
barSer.setTx(ctSerTx);
// 配置分类轴数据
CTAxDataSource cat = new CTAxDataSource();
CTStrRef strRef = new CTStrRef();
strRef.setF("Sheet1!$A$2:$A$6");
CTStrData strCache = new CTStrData();
CTUnsignedInt unsign4 = new CTUnsignedInt();
unsign4.setVal(5);
strCache.setPtCount(unsign4);
CTStrVal pt1 = new CTStrVal();
pt1.setIdx(0);
pt1.setV("1st Qtr");
CTStrVal pt2 = new CTStrVal();
pt2.setIdx(1);
pt2.setV("2nd Qtr");
CTStrVal pt3 = new CTStrVal();
pt3.setIdx(2);
pt3.setV("3rd Qtr");
CTStrVal pt4 = new CTStrVal();
pt4.setIdx(3);
pt4.setV("4th");
CTStrVal pt5 = new CTStrVal();
pt5.setIdx(4);
pt5.setV("5th Qtr");
strCache.setPtCount(unsign4);
strCache.getPt().add(pt1);
strCache.getPt().add(pt2);
strCache.getPt().add(pt3);
strCache.getPt().add(pt4);
strCache.getPt().add(pt5);
strRef.setStrCache(strCache);
cat.setStrRef(strRef);
barSer.setCat(cat);
// 配置值轴数据
CTNumDataSource vals = new CTNumDataSource();
CTNumRef numRef = new CTNumRef();
CTNumData numData = new CTNumData();
CTNumVal val = new CTNumVal();
val.setIdx(0);
val.setV("8.2");
CTNumVal val1 = new CTNumVal();
val1.setIdx(1);
val1.setV("3.2");
CTNumVal val2 = new CTNumVal();
val2.setIdx(2);
val2.setV("1.4");
CTNumVal val3 = new CTNumVal();
val3.setIdx(3);
val3.setV("1.2");
CTNumVal val4 = new CTNumVal();
val4.setIdx(4);
val4.setV("3.4");
numData.setPtCount(unsign4);
numData.getPt().add(val);
numData.getPt().add(val1);
numData.getPt().add(val2);
numData.getPt().add(val3);
numData.getPt().add(val4);
numData.setFormatCode("General");
numRef.setNumCache(numData);
numRef.setF("Sheet1!$B$2:$B$6");
vals.setNumRef(numRef);
barSer.setVal(vals);
objBar.getSer().add(barSer);
ctp.getAreaChartOrArea3DChartOrLineChart().add(objBar);
chart.setPlotArea(ctp);
ch.setChart(chart);
chartPart.setJaxbElement(ch);
P paragraph = factory.createP();
PPr paraProp = factory.createPPr();
PPrBase.Spacing spacing = new PPrBase.Spacing();
spacing.setAfter(BigInteger.ZERO);
R run = factory.createR();
Drawing drawing = factory.createDrawing();
GraphicData gData = new GraphicData();
gData.setUri("http://schemas.openxmlformats.org/drawingml/2006/chart");
gData.getAny().add(chartPart);
Inline inline = new Inline();
Graphic g = new Graphic();
g.setGraphicData(gData);
inline.setGraphic(g);
drawing.getAnchorOrInline().add(inline);
run.getContent().add(drawing);
paragraph.getContent().add(run);
paraProp.setSpacing(spacing);
paragraph.setPPr(paraProp);
wordPackage.getMainDocumentPart().getContent().add(paragraph);
}
Program error:Caused by: javax.xml.bind.JAXBException: class org.docx4j.openpackaging.parts.DrawingML.Chart以及其任何超类对此上下文都是未知的。
So what should I do to correctly add a bar chart?