First phase: I've bound the data source
private DataTable GetData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString);
DataSet dsData = new DataSet();
try
{
string sqlString = "select top 5\n" +
" b.name, COUNT(codAmount) as ca\n" +
" from consignment as c, Branches as b,\n" +
" CODConsignmentDetail_New as cn\n" +
" where c.destination = b.BranchCode\n" +
" and c.consignmentNumber = cn.consignmentNumber\n" +
" and c.consignerAccountNo = '" + Session["AccountNo"].ToString() + "'\n" +
" group by b.name\n" +
" order by ca desc";
SqlCommand SQLCmd = new SqlCommand(sqlString, con);
SQLCmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = SQLCmd;
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
catch
{
throw;
}
}
Then I've created the bar chart, but it uses only one color for each column which is blue.
I've to tried to append the color by sending the color through array, but it doesn't work.
private void BindChart1()
{
StringBuilder str = new StringBuilder();
DataTable dt = new DataTable();
try
{
dt = GetData();
str.Append(@"<script type=*text/javascript*> google.load( *visualization*, *1*, {packages:[*corechart*]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Consignment Amount');
data.addRows(" + dt.Rows.Count + ");");
string[] colours={ "green","blue","yellow","brown","red"};
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
str.Append("data.setValue( " + i + "," + 0 + "," + "'" + dt.Rows[i]["name"].ToString() + "');");
str.Append("data.setValue(" + i + "," + 1 + "," + dt.Rows[i]["ca"].ToString() + ") ;");
//str.Append(" chart.draw(colors:['"+colours[i].ToString()+"'],");
}
str.Append(" var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));");
str.Append(" chart.draw(data, {width: 550, height: 300, title: 'COD amount amoungst Cities',");
str.Append("hAxis: {title: 'Cities', titleTextStyle: {color: 'green'}}");
str.Append("}); }");
str.Append("</script>");
lt.Text = str.ToString().Replace('*', '"');
lt.Visible = true;
}
catch
{ }
}
I've also tried to add the color function below like this
str.Append(" chart.draw(data, {width: 550, height: 300, title: 'COD amount amongst Cities',color:['red','blue','yellow','green','black',]");
but it only uses the first color for each row for example each will have red color.