Tag-Archive for ◊ chart ◊

Author:
Thursday, October 01st, 2009

With Office 2007, Microsoft introduced a new document format called OpenXml. Its an xml based format for storing Mirosoft office documents. Technically, an Office 2007 file (pptx, docx etc) is simply a zip file containing a set of xml files and related content. You can actually, rename it to .zip and extract its contents! Although the idea is novel, the xml format itself is a little complex to work with. Microsoft does provide a OpenXML SDK to make life a little easier for programmers.

However, you’ll still need to learn a lot of tricks to get things done in OpenXml. I’ll demonstrate one such common task here. If you’re working with an OpenXml PowerPoint (pptx) file, one of the first things you may want to learn is how to clone a slide. i.e. make a copy of a slide along with all its contents. There are several sites that’ll give you code to Clone a slide. Here’s a good one which I’m using as a reference for my code: http://blogs.msdn.com/brian_jones/archive/2009/08/13/adding-repeating-data-to-powerpoint.aspx

However, I didn’t find any site that has a code which works with charts. When you clone a slide, all its parts should also be cloned for it to work properly. For eg. images are stored in a property called ImagePart. If you want images from source slide to appear in the cloned slide, you should copy the ImagePart too.

Similary charts are stored in a ChartPart. However, charts are more tricky as they refer to an embedded excel sheet that contains chart data. You’ll need to copy this excel sheet too. The following function has the sample code for charts and the embedded excel. It also has code to clone images to help you compare the differences:

public static SlidePart CloneSlidePartWithImagesAndCharts(PresentationPart presentationPart, SlidePart slideTemplate)
{
int i = presentationPart.SlideParts.Count();
//Create a new slide part in the presentation.
SlidePart newSlidePart = presentationPart.AddNewPart<SlidePart>(“newSlide” + i);
i++;
//Add the source slide content into the new slide.
newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open));
//Make sure the new slide references the proper slide layout.
newSlidePart.AddPart(slideTemplate.SlideLayoutPart, slideTemplate.GetIdOfPart(slideTemplate.SlideLayoutPart));

// copy the image parts
foreach (ImagePart ipart in slideTemplate.ImageParts)
{
ImagePart newipart = newSlidePart.AddImagePart(ipart.ContentType, slideTemplate.GetIdOfPart(ipart));
newipart.FeedData(ipart.GetStream());
}

// copy the chart parts
foreach (ChartPart cpart in slideTemplate.ChartParts)
{
ChartPart newcpart = newSlidePart.AddNewPart<ChartPart>(slideTemplate.GetIdOfPart(cpart));
newcpart.FeedData(cpart.GetStream());
// copy the embedded excel file
EmbeddedPackagePart epart = newcpart.AddEmbeddedPackagePart(cpart.EmbeddedPackagePart.ContentType);
epart.FeedData(cpart.EmbeddedPackagePart.GetStream());
// link the excel to the chart
(((newcpart.ChartSpace)).ExternalData).Id = newcpart.GetIdOfPart(epart);
newcpart.ChartSpace.Save();
}

//Get the list of slide ids.
SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;

//Deternmine where to add the next slide (find max number of slides).
uint maxSlideId = 1;
SlideId prevSlideId = null;
foreach (SlideId slideId in slideIdList.ChildElements)
{
if (slideId.Id > maxSlideId)
{
maxSlideId = slideId.Id;
prevSlideId = slideId;
}
}
maxSlideId++;
//Add the new slide at the end of the deck.
SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
//Make sure the id and relid are set appropriately.
newSlideId.Id = maxSlideId;
newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart);
newSlidePart.Slide.Save();
return newSlidePart;
}

Category: .Net | Tags: , , , , , ,  | 7 Comments
Author:
Tuesday, September 01st, 2009

I have developed a graphical solution for our prestigious client. Graphical solution includes generating Bar chart, Pie chart, line chart, Stacked bar chart, Multiple bar chart. I have done a simple search to get the list of open source applications which is available in market to provide the Chart solution.

I have got a list of opensource PHP Charts

1. JPGraph
2. GraPHPite
3. Open Flash Chart
etc…

We had chosen JPGraph since it has most of the chart types. But on the next day, I got to know its not completely open source, for commercial purpose we need to get license.

I started searching for some solution which is provided by PHP.

I got a fantastic PEAR package, “Image_Graph”.

PHP professionals knows what is PEAR (PHP Extension and Application Repository). Its is a framework and distribution system for reusable PHP components.

We can handle most of the charts using Image_Graph. Some of them are

Bar Chart, Pie Chart, Line Chart, Donut Chart, Step Chart, Logarithmic Charts, Area Chart, CandleStick Chart, rador Chart, Impulse Chart, Dot Chart, Band Chart and BoxWhisker charts with stacked and gradient effects. Its a cool package.

Click here to have a look at the list of graphs

Requirements need to check before starting with Image_Graph

1. Your PHP should support Graphics, for that you need to install and enable GD Library.

Click here to get more details about GD Library

2. Your PHP version on server should me more than 4.3.0, suggested to keep PHP 5

3. You need to have the following PEAR packages in your server.

i. Image_Canvas-0.3.1
[Linux Command to install : pear install Image_Canvas-0.3.1 ]

ii. Image_Color-1.0.3
[Linux Command to install: sudo pear install Image_Color-1.0.3]

If you have done with all the above stuffs, you are ready to install Image_Graph PEAR Package.

Linux Command: pear install Image_Graph-0.7.2

For Manual installation Download the Image_Graph and move it to the PEAR path.

Try Examples in Local:

1. Download Image_Graph Package
2. Unzip the package
3. Move the unzipped folder to the web root of your Apache [either www / htdocs].
4. You can access the examples using url : http://localhost/Image_Graph/docs/examples/

By exploring the examples, you will get to know the various way of implementation. Its very simple if you know the basics of PHP.

Issues Which I faced during production upload:

The problems which I faced are

1. Missing of Fonts:

I got a warning like “Warning: imagettfbbox(): Could not find/open font in /usr/share/php/Image/Canvas/GD.php on line 1245

Solution:
This occurs if you don’t have the correct (or any) fonts in your Fonts directory, found at php/Image/Canvas/Fonts/. Make sure that you have the corresponding .ttf file located in your Fonts directory

2. Issues with Legends for Pie/Donut charts

I got one more issue, that legends are not shown for Pie/Donut Charts. Refer: http://pear.veggerby.dk/samples/show/id/gradient_pie/

I searched for solutions. Finally the issue is with Image_Graph PEAR Package. They have included 2 lines which caused the problem. I didn’t analyse the functioanlity of the 2 lines. But the solution is simple, comment that two lines in the installed package.

Solution:

1. You need to find the PEAR package’s include path.

2. By checking the phpinfo using method [phpinfo( )], you can get the path “include_pathPHPinfo

3. In file /usr/share/php/Image/Graph/Plot/Pie.php, comment the following lines
Line 502: // $this->_clip(true);
Line 616: // $this->_clip(false);

I hope this will defenitely help peoples who is going for “Image_Graph” PEAR Package. Enjoy coding :)