Tuesday, 25 September 2012

How to add chart control in sharepoint

Step1:
Download chart control  from the following link (Click here)
download another exe file (Click here to download)

step2:
Make changes as follows in web.config(so that only chart control will appear)

<pages>
<controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.Datavisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </controls>
</pages>
 
<httpHandlers>
 <add path="ChartImg.axd" verb="GET, HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
</httpHandlers>
 
<appSettings>
 <add key="ChartImageHandler" value="Storage=memory;Timeout=180;Url=~/tempImages/;" />
</appSettings>
 
<handlers>
 <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </handlers>


Step3:
go to "C:\Program Files (x86)\Microsoft Chart Controls\Assemblies\System.Web.DataVisualization.Design.dll"
add this dll reference to our visual studio solution.
and also add"System.Web.DataVisualization.Design.Dll"


Step4:
go to toolbox add new tab->chooseitems->click browse->Add dll which is mensioned above-->add Chart control

Step5:
if any error comes assembly not found means " in your solution find packages click it -->Advanced-->Add->Choose dll from path where we have stored that dll(incase problem comes)

Step 6:
drag and drop the chart control in visual webpart (now it will show "object reference not set to an instatnce of variable" no problem with this.
in code behind paste the following code


protected void Page_Load(object sender, EventArgs e)
        {



 string[] x = new string[4] { "Mango", "Apple", "Orange", "Banana" };
            int[] y = new int[4] { 200, 112, 55, 96 };
            Chart1.Series[0].Points.DataBindXY(x, y);
            Chart1.Series[0].ChartType = SeriesChartType.Bar;
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
}

this will show chart control in visual web part for asp.net we no need to change the web.config file







Step6:Export Chart control to pdf
before that add these two dll as reference
itextsharp.dll
itextsharp.pdfa.dll

paste this code in button click event


protected void btnsubmit_Click(object sender, EventArgs e)
        {
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            using (MemoryStream stream = new MemoryStream())
            {
                Chart1.SaveImage(stream, ChartImageFormat.Png);
                iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
                chartImage.ScalePercent(75f);
                pdfDoc.Add(chartImage);
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
        }


















No comments:

Post a Comment