Click here to Skip to main content
15,885,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code behind for the line chart. It gives output the month in decimal. Example: 4.0 (for month April)
I want it to be April only. How to do it?


Code.aspx.cs
[WebMethod]
   public static List<object> GetChartData2()
   {
       string query = " SELECT MONTH(DateOfPurchase) as Bulan, SUM(TotalPaid) AS JumlahJualan ";
       query += "  FROM tblOrders GROUP BY  MONTH(DateOfPurchase) ORDER BY MONTH(DateOfPurchase)";
       string constr = ConfigurationManager.ConnectionStrings["KalzAgency"].ConnectionString;
       List<object> chartData = new List<object>();
       chartData.Add(new object[]
       {
       "Bulan", "Jumlah Jualan (RM)"
       });
       using (SqlConnection con = new SqlConnection(constr))
       {
           using (SqlCommand cmd = new SqlCommand(query))
           {
               cmd.CommandType = CommandType.Text;
               cmd.Connection = con;
               con.Open();
               using (SqlDataReader sdr = cmd.ExecuteReader())
               {
                   while (sdr.Read())
                   {
                       chartData.Add(new object[]
                       {
                       sdr["Bulan"], sdr["JumlahJualan"]
                       });
                   }
               }
               con.Close();
               return chartData;
           }
       }
   }







Code.aspx
<script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var options = {
                title: 'Jualan Mengikut Bulan Bagi Tahun 2021',
                width: 1700,
                height: 600,
                bar: { groupWidth: "100%" },
                legend: { position: "bottom" },
                isStacked: true
            };
            $.ajax({
                type: "POST",
                url: "Dashboard.aspx/GetChartData2",
                data: '{}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    var data = google.visualization.arrayToDataTable(r.d);
                    var chart = new google.visualization.LineChart($("#chart2")[0]);
                    chart.draw(data, options);
                },
                failure: function (r) {
                    alert(r.d);
                },
                error: function (r) {
                    alert(r.d);
                }
            });
        }
    </script>


What I have tried:

This is the code behind for the line chart. It gives output the month in decimal. Example: 4.0 (for month April)
I want it to be April only. How to do it?
Posted
Updated 11-Jun-21 23:41pm

1 solution

Try:
string query = " SELECT DATENAME(MONTH, DATEADD(MONTH, MONTH(DateOfPurchase) - 1, '2001-01-01')) as Bulan, SUM(TotalPaid) AS JumlahJualan ";
       query += "  FROM tblOrders GROUP BY  MONTH(DateOfPurchase) ORDER BY MONTH(DateOfPurchase)";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900