Thursday, February 16, 2012

Create a Chart Dynamically in SharePoint 2010














        public void DrawGraph(DataTable dt, string chartTitle)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type='text/javascript' src='https://www.google.com/jsapi'></script>");
            sb.AppendLine("<script type='text/javascript'>");
            sb.AppendLine("google.load('visualization', '1', {packages:['corechart']});");
            sb.AppendLine("google.setOnLoadCallback(drawChart);");
            sb.AppendLine("function drawChart() {");
            sb.AppendLine("var data = new google.visualization.DataTable();");
            sb.AppendLine("data.addColumn('string', 'Title');");
            sb.AppendLine("data.addColumn('number', 'Rating');");
            sb.AppendLine("data.addRows([ ");
            foreach (DataRow drow in dt.Rows)
            {
                sb.AppendLine("['" + drow["Title"] + "'," + drow["Rating"] + "],");
            }
            sb.AppendLine("['',0]");
            sb.AppendLine("]);");
            sb.AppendLine("var options = {");
            sb.AppendLine("width: 800, height: 360,");
            sb.AppendLine("title: '" + chartTitle + "',");
            sb.AppendLine("hAxis: {title: 'Title', titleTextStyle: {color: 'white'}}};");
            sb.AppendLine("var chart = new google.visualization.ColumnChart(document.getElementById('chart_div" + chartTitle.Substring(0, 2) + "'));");
            sb.AppendLine("chart.draw(data, options);      }   ");
            sb.AppendLine("</script>");
            sb.AppendLine("<div id='chart_div" + chartTitle.Substring(0, 2) + "'></div>");
            pnlChart.Controls.Add(new LiteralControl(sb.ToString()));
        }



For More Chart Controls check the below link
Google Charts


No comments:

Post a Comment

How to launch a document by clicking a link to the file?

Word: <a href='ms-word:ofe|u|path/to/web/word/document.docx'>Link to document</a> Excel: <a href='ms-excel:o...