I am trying to use Aspose.Words to create a new Word Document with some chart data. However some things I can’t get working - here is a example of my rough code below:
Document myNewDoc = new Document();
DocumentBuilder myBuilder = new DocumentBuilder(myNewDoc);
Shape myShape = myBuilder.InsertChart();
Chart myChart = myShape.Chart;
ChartSeriesCollection mySeries = myChart.Series;
mySeries.Clear();
string[] cats = new string[] { "Category A", "Category B" };
ChartSeries series1 = mySeries.Add("Something Type 1", categories, new double[] { 1, 2 });
ChartSeries series2 = mySeries.Add("Something Type 2", categories, new double[] { 3, 4 });
ChartSeries series3 = mySeries.Add("Something Type 3", categories, new double[] { 5, 6 });
ChartSeries series4 = mySeries.Add("Something Type 3", categories, new double[] { 5, 6 });
series1.Format.Fill.ForeColor = Color.Blue;
series2.Format.Fill.ForeColor = Color.Yellow;
series3.Format.Fill.ForeColor = Color.Red;
series4.Format.Fill.ForeColor = Color.Black;
I’ve tried lots of ways to get a bar chart with the values to also show for each bar but cannot seem to find a way - can you help and point out where I have went wrong? I’d like the result saved as a JPEG file rather than a Word Document.
Hi
Thank you for your interest in Aspose products.
You were really close for getting the desired result you wanted it. I added some code based on your work.
Document myNewDoc = new Document();
DocumentBuilder myBuilder = new DocumentBuilder(myNewDoc);
Shape myShape = myBuilder.InsertChart(ChartType.Bar, 400, 400); // You need to specify the type of Chart you would like to display, also the size of it in pixels.
Chart myChart = myShape.Chart;
ChartSeriesCollection mySeries = myChart.Series;
mySeries.Clear();
string[] categories = new string[] { "Category A", "Category B" }; // There was a problem with the name of the variable been cats, want matching the following lines.
ChartSeries series1 = mySeries.Add("Something Type 1", categories, new double[] { 1, 2 });
ChartSeries series2 = mySeries.Add("Something Type 2", categories, new double[] { 3, 4 });
ChartSeries series3 = mySeries.Add("Something Type 3", categories, new double[] { 5, 6 });
ChartSeries series4 = mySeries.Add("Something Type 3", categories, new double[] { 5, 6 });
series1.Format.Fill.ForeColor = Color.Blue;
series2.Format.Fill.ForeColor = Color.Yellow;
series3.Format.Fill.ForeColor = Color.Red;
series4.Format.Fill.ForeColor = Color.Black;
myNewDoc.Save("zzChartExample.jpeg"); // instead of specifying the extension as docx, use jpeg instead.
I will be pointing out what were you missing.
- When inserting the chart, there are 2 overloads, the simpler one required for you to specify the type of chart you want to create and also the dimensions of it. This is the method used in the code example.
- There was a typo in the code related to the list of categories. I renamed it so it matches the code.
- In order to change from Word document to an JPEG image is as simple as specify the name of it when you are saving the document. So instead of saving a “Example.docx”, save it as “Example.JPEG”.
Please let me know if you would like to know something else.
Best regards.