Hello,
I am using Aspose word (version:23.3) java, trying to generate the bar chart with one table having some values as shown in the picture attached below.
I tried to create a similar template using Microsoft-word and Aspose LinqEngine, but unable to create as displayed in below attachment. I am also attaching JSON data file for difference table.sampleData.zip (624 Bytes)
chart.png (6.9 KB)
Can you please help to achieve this?
Thank you.
@iampranoti
LINQ Reporting Engine capabilities are restricted by MS Word capabilities. Unfortunately, MS Word does not allow to create charts like this.
However, there is a way to mimic the chart by a table and dynamic image generation. Please check Template.docx (15.3 KB) and the following code to build a report from it:
Document document = new Document("Template.docx");
JsonDataSource dataSource = new JsonDataSource("sampleData.json");
ReportingEngine engine = new ReportingEngine();
engine.getKnownTypes().add(Runner.class);
engine.buildReport(document, dataSource, "data");
document.save("Out.docx");
import java.awt.*;
import java.awt.Font;
import java.awt.image.BufferedImage;
public class Runner
{
public static BufferedImage getPercentRect(double value, double maxValue)
{
int imageHeight = 80;
int imageWidth = 1200;
int labelWidth = 200;
int barWidth = (int)Math.round((imageWidth - labelWidth) * value / maxValue);
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = image.getGraphics();
// Draw the bar.
graphics.setColor(Color.BLUE);
graphics.fillRect(0, 0, barWidth, imageHeight);
// Draw the background.
graphics.setColor(Color.WHITE);
graphics.fillRect(barWidth, 0, imageWidth - barWidth, imageHeight);
Graphics2D graphics2D = (Graphics2D)graphics;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Draw the text label.
graphics2D.setColor(Color.BLACK);
graphics2D.setFont(new Font("Times New Roman", Font.PLAIN, 40));
graphics2D.drawString(new Double(value).toString(), barWidth + 20, 60);
return image;
}
}
The result table looks close to what is expected. You may want to play around with appearance of the template table and parameters in code to make it look as needed.
1 Like
Hello,
I tried using above code and generating the chart as shown in picture above. But that bar image is not generating in the document. Attaching the document here.
Please advise.
Thank you.helloworld_template_out.pdf (38.0 KB)
@iampranoti
Unfortunately, we cannot reproduce the issue. Here are result documents obtained by running the shared code on our end: Out.docx (31.5 KB) and Out.pdf (46.6 KB). Please make sure you use exactly the same code and template.