Google Combo Chart example with database in ASP.NET

Hide demoDownload
                

In this article I’m going to explain how to create Google combo chart example with database in ASP.NET.  

        Google charts allows you to create a chart that lets you render each series as a different marker type from the following list:

<!--[if !supportLists]-->·         <!--[endif]-->line,

<!--[if !supportLists]-->·         <!--[endif]-->area,

<!--[if !supportLists]-->·         <!--[endif]-->bars,

<!--[if !supportLists]-->·         <!--[endif]-->candlesticks

<!--[if !supportLists]-->·         <!--[endif]-->stepped area.

To assign a default marker type for series, specify the seriesType property. Use the series property to specify properties of each series individually.

 

          Following screen shots are demonstrate various marker type used to create different combo charts.

 1.  seriesType: line – series: area

 

  1. 2. seriesType: area – series: line

 

3. seriesType: bars – series: area

 

Sample data:

 

id

Month

Bolivia

Ecuador

Madagascar

Average

1

2004/05

165

938

522

614.6

2

2005/06

135

1120

599

682

3

2006/07

157

1167

587

623

4

2007/08

139

1110

615

609.4

5

2008/09

136

691

629

569.6

 

 

Designer source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 <head>

 <title>Google Combo chart</title>

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>   

  </head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Literal ID="lt" runat="server" ></asp:Literal>

   <div id="chart_div" style="width: 660px; height: 400px;"></div>

    </div>   

    </form>

</body>

</html>

 

C# code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Text;

public partial class _Default : System.Web.UI.Page

{

    SqlConnection conn = new SqlConnection("Data source=localhost; Initial catalog=Demo; Integrated security=true");

    protected void Page_Load(object sender, EventArgs e)

    {

        BindChart();

    }

    private DataTable GetData()

    {

        DataTable dt = new DataTable();

        string cmd = "select * from Production";

        SqlDataAdapter adp = new SqlDataAdapter(cmd, conn);

        adp.Fill(dt);

        return dt;

    }

    private void BindChart()

    {

        DataTable dt = new DataTable();

        StringBuilder str = new StringBuilder();

        try

        {

            dt = GetData();

 

            str.Append(@"< script type='text/javascript'>

                      google.load('visualization', '1', {packages: ['corechart']});

                   < /script>

                    <script type='text/javascript'>

                    function drawVisualization() {       

                    var data = google.visualization.arrayToDataTable([

                    ['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Average'],");

 

            int count=dt.Rows.Count - 1;

            for (int i = 0; i <= count; i++)

            {

                if (count == i)

                {

                    str.Append("['" + dt.Rows[i]["Month"].ToString() + "'," + dt.Rows[i]["Bolivia"].ToString() + "," + dt.Rows[i]["Ecuador"].ToString() + "," + dt.Rows[i]["Madagascar"].ToString() + "," + dt.Rows[i]["Average"].ToString() + "]]);");

                }

                else

                {

                    str.Append("['" + dt.Rows[i]["Month"].ToString() + "'," + dt.Rows[i]["Bolivia"].ToString() + "," + dt.Rows[i]["Ecuador"].ToString() + "," + dt.Rows[i]["Madagascar"].ToString() + "," + dt.Rows[i]["Average"].ToString() + "],");

                }

            }

           

            str.Append("var options = { title : 'Monthly Coffee Production by Country', vAxis: {title: 'Cups'},  hAxis: {title: 'Month'}, seriesType: 'bars', series: {3: {type: 'area'}} };");

            str.Append(" var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));  chart.draw(data, options); } google.setOnLoadCallback(drawVisualization);");

            str.Append("< /script>");

 

            lt.Text = str.ToString();

        }

        catch (Exception ex)

        { }

    }

}

 

Please refer this link for further reference

https://developers.google.com/chart/interactive/docs/gallery/combochart

 

View demoDownload

- See more at: http://www.dotnetfox.com/articles/google-combo-chart-example-with-database-in-Asp-Net-1099.aspx#sthash.l7cMEOek.dpuf

转载于:https://www.cnblogs.com/happy-Chen/p/3695871.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值