Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday, 11 May 2014

Create Charts in website using Chart.js Javascript library

Recently, I am working on a project in which I have to create some charts and graphs in a website.As we know that Graphs are very useful in displaying data visually rather than displaying data in tables. Using Chart.js, we can draw charts and graphs on webpage using HTML5 canvas element. We can create six types of charts using chart.js

Let's Start
Firstly, we have to go http://www.chartjs.org/ for downloading chart.js Javascript plugin. Clicking on download button will take you to GitHub page from where you can download the archive.


After Extracting the files Chart.js-master.rar file, you will be able to find documentation, examples and chart.js file. Add chart.js javascript library into your document in between head tag.

<script src="js/Chart.min.js"></script>

I am using minified version of chart.js. Add <canvas> element with height,width and unique id.

Example 1:
In first Example we are going to create Pie Chart using chart.js. For creating pie chart, a variable array(named pieChartData) declared which contain value and color properties. Set the values and color depanding upon your chart. 



Chart.js provide various option for changing animation and look. You can change these options according to your wish.
For creating chart, we have to initialize chart class and pass our canvas element and "2D" drawing context and call the pie method.
<!Doctype HTML>
<html>
<head>
    <title>Pie Chart</title>
    <script src="js/Chart.min.js"></script>
</head>
<body>
    <canvas id="pieChartLoc" height="300" width="300"></canvas>
    <script>
        var pieChartData = [
        {
            value: 50,
            color: "lightblue"
        },
        {
            value: 10,
            color: "red"
        },
        {
            value: 40,
            color: "green"
        }
        ]
        var myLine = new Chart(document.getElementById("pieChartLoc").getContext("2d")).Pie(pieChartData);
    </script>
</body>
</html>

Example 2: Creating the Doughnut Chart
Doughnut chart looks much similar to pie chart except they have the centre cut out. Everything is similar to above process except we have to call doughnut method this time.
<!Doctype HTML>
<html>
<head>
    <title>Doughnut Chart</title>
    <script src="js/Chart.min.js"></script>
</head>
<body>
    <canvas id="doughNutChartLoc" height="300" width="300"></canvas>
    <script>
        var DoughNutChartData = [
        {
            value: 50,
            color: "lightblue"
        },
        {
            value: 10,
            color: "red"
        },
        {
            value: 40,
            color: "green"
        }
        ]
        var myDoughnut = new Chart(document.getElementById("doughNutChartLoc").getContext("2d")).Doughnut(DoughNutChartData);
    </script>
</body>
</html>

Preview:

Example 3: Creating the Bar Chart
For creating Bar chart, we have to labels( displayed on x-axis) and datasets( contains information about fillColor, strokeColor and data which is basically value on y-axis)

<!Doctype HTML>
<html>
<head>
    <title>Bar Chart</title>
    <script src="js/Chart.min.js"></script>
</head>
<body>
    <canvas id="barChartLoc" height="300" width="300"></canvas>
    <script>
        var barChartLocData = {
            labels: ["January""Feburary""March"],
            datasets: [{ fillColor: "lightblue", strokeColor: "blue", data: [15, 20, 35] }]
        };
        var mybarChartLoc = new Chart(document.getElementById("barChartLoc").getContext("2d")).Bar(barChartLocData);
    </script>
</body>
</html>

Hope you like it. Thanks.
[Download Source Code from Google Drive]


Wednesday, 12 March 2014

Error Provider in C# (Windows Form Application)

The ErrorProvider alerts the user that something is wrong.

In this example I will use a Display Warning icon, Wrong icon (when an incorrect expression is entered ) or a Tick icon depending upon the data entered in the text box so that the user can determine that data entered is correct or incorrect.

Let's Begin as in the following:

1. Create a new Windows Forms Application.
2. Place two text boxes (for getting name and age) and three ErrorProvider controls from the toolbox.


Set text boxes are named txt_name and txt_age. I have also created a button but there is no use for it (in the example).
3. Set the Icon for errorprovider1 (warning), errorprovider2 (wrong or cross) and errorprovider3 as a tick mark (for a correct entry). I will use the warning errorprovider when the user leaves a text box blank, use a cross or wrong when the user enters incorrect informat (for the Age text box) and a tick icon for when all conditions are satisfied.
4.In addition to defaults imports, I am adding Regular Expression library support for the txt_age text box as in the following:

using System.Text.RegularExpressions;

5. Now we need to create validating events for both text boxes. Right-click on the TextBox then click on "Properties".then click on the lightening bolt (Events) icon at the top of the window, then double-click on validating event.

6. Enter these codes  between the generated codes.

void Txt_nameValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
if(txt_name.Text==string.Empty)
{
errorProvider1.SetError(txt_name,"Please Enter Name");
errorProvider2.SetError(txt_name,"");
errorProvider3.SetError(txt_name,"");
}
else
{
errorProvider1.SetError(txt_name,"");
errorProvider2.SetError(txt_name,"");
errorProvider3.SetError(txt_name,"Correct");
}
}
void Txt_ageValidating(object sender, System.ComponentModel.CancelEventArgs e)
{
if(txt_age.Text==string.Empty)
{
errorProvider1.SetError(txt_age,"Please provide age");
errorProvider2.SetError(txt_age,"");
errorProvider3.SetError(txt_age,"");
}
else
{
Regex numberchk=new Regex(@"^([0-9]*|\d*)$");
if(numberchk.IsMatch(txt_age.Text))
{
errorProvider1.SetError(txt_age,"");
errorProvider2.SetError(txt_age,"");
errorProvider3.SetError(txt_age,"Correct");
}
else
{
errorProvider1.SetError(txt_age,"");
errorProvider2.SetError(txt_age,"Wrong format");
errorProvider3.SetError(txt_age,"");
}
}
}

In the "txt_nameValidating()" event, if txt_name is left empty then it shows error. This is done using the "setError()" method. The setError method sets the error description string for the specified control. If the user hovers the mouse over the error icon then it shows an error description string that we have declared in the setError() method.

7. Build and run the code.

Preview:
You can also download the source code of this example.

(Download Source Code)

Hope you like it.
Thanks.


Tuesday, 11 February 2014

XML Documentation in C#

In C#, we use XML Comments for creating documentation for Classes and its member. XML Documentation helps other programmer or developer to use your code i.e. Classes, Functions and its member easily by providing some useful information. XML Documentation starts with three slashes before the class or function going to be documented.

Adding XML Documentation to a class:

Typing three slashes (///) before a class or function will create documentation automatically in Visual Studio. Documentation contain one or more than one documentation elements. Each element starts with start tag (e.g. <summary>) and ends with end tag (e.g. </summary>).
Element
Description
<summary>
Provide Description for Class, member etc.
<param name=”i”>
Describe a parameter of a method
<returns>
Describe return type of a method
<value>
Describe value of a property
For Example:-

In above example, when we type three slashes before result method than it will automatically create three documentation elements that are Summary (used for description of class), param tag for each parameter and return tag for method’s return type.

When I type or use Add class than, IntelliSense in Visual Studio show Screen tip that we have added in xml documentation element (<summary tag>) earlier.
Preview:

Protected by Copyscape Web Plagiarism Scanner



Friday, 7 February 2014

Creating and using DLL (Class library) in C#

A DLL (Dynamic Link library) is a library that contain some functions (code) and data which can be used by more than one program at a same time. Once we have created a DLL file, we can use it in many application. The only thing we have to do is to add the reference/Import the DLL File.


Creating DLL File:-
Step 1: Open Visual Studio -> Click on File->New Project -> Visual C# -> Class library

Step 2:- Change the class name(class1.cs) to calculate.cs

Step 3:- in calculate class write method for addition and subtraction of two integer.

Step 4:- Build the solution (F6). If Build is succeeded than you will see calculation.dll file in bin/debug directory of your project.
We have created our DLL file. Now we are going to use it in another Application.

Using DLL File:-
Step 1:- Open Visual Studio-> Click on File -> New Project ->Visual C# ->Windows Form Application
Step 2:- Design form as in below picture

Step 3:- Add Reference of dll file i.e. calculation.dll that we have created earlier.
Step: 4:- Add namespace (using calculation)




Step 5:-
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Calculation;

namespace MiniCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        calculate cal = new calculate();
        //Addition Button click event
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //storing the result in int i
                int i = cal.Add(int.Parse(txtFirstNo.Text), int.Parse(txtSecNo.Text));
                txtResult.Text = i.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //Subtraction button click event
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //storing the result in int i
                int i = cal.Sub(int.Parse(txtFirstNo.Text), int.Parse(txtSecNo.Text));
                txtResult.Text = i.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Protected by Copyscape Web Plagiarism Scanner







Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook