Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Wednesday, April 4, 2018

Comparing date objects in Javascript

The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.
The ==, !=, ===, and !== operators require you to use date.getTime() as in
var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();
to be clear just checking for equality directly with the data objects won't work
var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

Wednesday, March 28, 2018

Javascript function to read URL query string values

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
Usage:
// query string: ?foo=lorem&bar=&baz
var foo = getParameterByName('foo'); // "lorem"
var bar = getParameterByName('bar'); // "" (present with empty value)
var baz = getParameterByName('baz'); // "" (present with no value)
var qux = getParameterByName('qux'); // null (absent)

Wednesday, February 21, 2018

Chart.js example in ASP.NET Webforms


First, we have to generate a compatible data source for the chart on the server side. 

[WebMethod]
public static string LoadBarChart()
{

    var data = new
    {
        labels = new[] {"South Western","Western","Northern","North Eastern","Eastern" },
        datasets = new[] {
            new  { label = "2016 - 2017", backgroundColor = new[] { "rgba(220, 220, 220, 0.5)" }, pointBorderColor = "#fff", data = new[] {"100","200","150","375","500"} }
            ,new  { label = "2017 - 2018", backgroundColor = new[] { "#018AC8", "#058EAA", "#DEB816", "#E37F1C", "#4A5D0E" }, pointBorderColor = "#fff", data = new[] {"122","210","175","375","525"}  }

        }
    };

    return data.ToJSON();
}


public static class JSONHelper
{
    public static string ToJSON(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

    public static string ToJSON(this object obj, int recursionDepth)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.RecursionLimit = recursionDepth;
        return serializer.Serialize(obj);
    }
}
Add the following in your .aspx page.

    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div id="chart_div">
                            <canvas id="barChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
Then goes your javascript / jquery code

$(function () {

    var ctx2 = document.getElementById("barChart").getContext("2d");

    var fetch_url;
    fetch_url = '../mymap.aspx/LoadBarChart';
    return $.ajax({
        url: fetch_url,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (res, textStatus) {

            var parsedJson = JSON.parse(res.d);
            var barData = parsedJson;
            var barOptions = {
                responsive: true,
                maintainAspectRatio: true,
                scales: {
                    yAxes: [{

                        gridLines: {
                            display: true
                        },
                        scaleLabel: {
                            display: true,
                            labelString: 'Millions'
                        }
                    }],
                    xAxes: [{
                        gridLines: {
                            display: true
                        }
                    }]
                }

            };

            var myChart = new Chart(ctx2, { type: 'bar', data: barData, options: barOptions }); 
        },
        error: function (res, textStatus) {

        }
    });

});