Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

ASP.NET Web Page Charts:


Creating charts in ASP.NET web applications can be done using various libraries like Chart.js or Telerik UI for ASP.NET AJAX. Let's simplify the process using an example with Chart.js, which is a popular JavaScript library for creating interactive charts.

Example: Creating a Simple Chart in ASP.NET

  1. Set Up Your Project:

    • Open Visual Studio and create a new ASP.NET Web Application project.
    • Choose either Web Forms or MVC based on your preference.
  2. Install Chart.js:

    • Download Chart.js from the official website or use a CDN link.
    • Include Chart.js in your project by adding it to your project's Scripts folder or referencing it from a CDN in your HTML.
  3. Add an HTML Page with a Canvas Element:

    • Create a new HTML page (e.g., ChartPage.aspx for Web Forms or ChartPage.cshtml for MVC).
    • Add a canvas element to the page where the chart will be rendered:
  4. Example

    <canvas id="myChart" width="400" height="400"></canvas>

  5. Write JavaScript to Create the Chart:

    • Add a <script> tag to your HTML page to include JavaScript code for creating the chart.
    • Use the Chart.js API to configure and render the chart. For example:

    Example

    <script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
    label: '# of Votes',
    data: [12, 19, 3, 5, 2, 3],
    backgroundColor: [
    'rgba(255, 99, 132, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(255, 206, 86, 0.2)',
    'rgba(75, 192, 192, 0.2)',
    'rgba(153, 102, 255, 0.2)',
    'rgba(255, 159, 64, 0.2)'
    ],
    borderColor: [
    'rgba(255, 99, 132, 1)',
    'rgba(54, 162, 235, 1)',
    'rgba(255, 206, 86, 1)',
    'rgba(75, 192, 192, 1)',
    'rgba(153, 102, 255, 1)',
    'rgba(255, 159, 64, 1)'
    ],
    borderWidth: 1
    }]
    },
    options: {
    scales: {
    y: {
    beginAtZero: true
    }
    }
    }
    });
    </script>

  6. Run Your Application:

    • Build and run your ASP.NET web application.
    • Navigate to the page where you added the chart (ChartPage.aspx or ChartPage.cshtml).
    • You should see the chart rendered on the page.

That's it! You've created a simple chart in your ASP.NET web application using Chart.js. You can customize the chart further by exploring the Chart.js documentation and adjusting the configuration options to meet your needs.


Advertisement
Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java