Tutorial com DataTable

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > ASP Controls > Chart >

Tutorial com DataTable

Previous pageReturn to chapter overviewNext page

Página

 

chart

 

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication11.WebForm1" %>

 

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

 

<!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 runat="server">

  <title></title>

</head>

<body>

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

  <div>

  <asp:Chart ID="Grafico" runat="server" Width="545px" ImageStorageMode="UseImageLocation">

      <Series>

          <asp:Series Name="srConcluidas" BorderWidth="2" ChartType="Spline" Color="Green" LegendText="Concluídas"></asp:Series>

          <asp:Series Name="srAbertas" BorderWidth="2" ChartArea="ChartArea1" ChartType="Spline" Color="Blue" LegendText="Abertas"></asp:Series>

          <asp:Series Name="srCanceladas"BorderWidth="2" ChartArea="ChartArea1" ChartType="Spline" Color="Red" LegendText="Canceladas"></asp:Series>

      </Series>

      <ChartAreas>

          <asp:ChartArea Name="ChartArea1"></asp:ChartArea>

      </ChartAreas>

      <Legends>

          <asp:Legend Name="Legend1">

          </asp:Legend>

        </Legends>

  </asp:Chart>

  </form>

</body>

</html>

 

Default.aspx.cs

 

using System;

using System.Data;

using System.Web;

using Npgsql;

 

namespace Programa

{

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

   {

      private void Novo(DataTable tab, int valor1, int valor2, int valor3, string nome)

       {

          DataRow dr = tab.NewRow();

           dr["valor1"] = valor1;

           dr["valor2"] = valor2;

           dr["valor3"] = valor3;

           dr["nome"] = nome;

           tab.Rows.Add(dr);

       }

 

      private DataTable GetTabela()

       {

          DataTable tabela = new DataTable();

           tabela.Columns.Add(new DataColumn("valor1", Type.GetType("System.Int32")));

           tabela.Columns.Add(new DataColumn("valor2", Type.GetType("System.Int32")));

           tabela.Columns.Add(new DataColumn("valor3", Type.GetType("System.Int32")));

           tabela.Columns.Add(new DataColumn("nome", Type.GetType("System.String")));

          return tabela;

       }

 

      protected void Page_Load(object sender, EventArgs e)

       {

          DataTable tabela = GetTabela();

           Novo(tabela, 10, 30, 10, "03/11");

           Novo(tabela, 15, 15, 15, "04/11");

           Novo(tabela, 20, 7, 25, "05/11");

           Novo(tabela, 12, 3, 2, "06/11");

           Novo(tabela, 5, 15, 35, "07/11");

 

           // associar campos

           Grafico.Series[0].XValueMember = "nome";

           Grafico.Series[0].YValueMembers = "valor1";

           

           Grafico.Series[1].XValueMember = "nome";

           Grafico.Series[1].YValueMembers = "valor2";

           

           Grafico.Series[2].XValueMember = "nome";

           Grafico.Series[2].YValueMembers = "valor3";

 

           // máximos e mínimos

           Grafico.ChartAreas[0].AxisY.Maximum = 40;

           Grafico.ChartAreas[0].AxisY.Minimum = 0;

 

           Grafico.ChartAreas[0].AxisX.Minimum = 1;

           Grafico.ChartAreas[0].AxisX.Maximum = 5;

 

           // mostrar as legendas abaixo

           Grafico.Legends[0].LegendStyle = LegendStyle.Table;

           Grafico.Legends[0].TableStyle = LegendTableStyle.Auto;

           Grafico.Legends[0].Docking = Docking.Bottom;

 

           Grafico.DataSource = tabela;

           Grafico.DataBind();

  }

}