Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a chart created in code behind to the rendered html page?

Tags:

c#

asp.net

charts

I'm trying to create a .net charting control completely in the code behind and insert that chart at a specific location on the web page.

Here is my html page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="chart"></div>
    </form>
</body>
</html>

Here is the code behind:

using System;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
      //SET UP THE DATA TO PLOT  
        double[] yVal = { 80, 20 };
        string[] xName = { "Pass", "Fail" };

      //CREATE THE CHART
        Chart Chart1 = new Chart();

      //BIND THE DATA TO THE CHART
        Chart1.Series.Add(new Series());
        Chart1.Series[0].Points.DataBindXY(xName, yVal);

      //SET THE CHART TYPE TO BE PIE
        Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
        Chart1.Series[0]["PieLabelStyle"] = "Outside";
        Chart1.Series[0]["PieStartAngle"] = "-90";

      //SET THE COLOR PALETTE FOR THE CHART TO BE A PRESET OF NONE 
      //DEFINE OUR OWN COLOR PALETTE FOR THE CHART 
        Chart1.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.None;
        Chart1.PaletteCustomColors = new Color[] { Color.Blue, Color.Red };

      //SET THE IMAGE OUTPUT TYPE TO BE JPEG
        Chart1.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageType.Jpeg;

      //ADD A PLACE HOLDER CHART AREA TO THE CHART
      //SET THE CHART AREA TO BE 3D
        Chart1.ChartAreas.Add(new ChartArea());
        Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

      //ADD A PLACE HOLDER LEGEND TO THE CHART
      //DISABLE THE LEGEND
        Chart1.Legends.Add(new Legend());
        Chart1.Legends[0].Enabled = false;
    }
}

I want to render the charting control inside the div with id="chart"

Thanks for the help!

like image 212
Ryan Avatar asked Mar 16 '10 12:03

Ryan


People also ask

How can we add chart type to MVC charts?

You can change the Chart Type by changing “type” property in view to “line”, “area”, “spline”, “bar”, “pie”, etc.

How can we add series to MVC charts syntax?

You can add multiple series to the chart by using Series property. The series are rendered in the order as it is added to the series array.

What is chart in asp net?

The DevExpress ASP.NET Chart Control offers a comprehensive collection of 2D and 3D charts to meet the data visualization needs of your end users. The ASP.NET Chart Control provides flexible data binding options that allow you to bind to a table from a database or a collection created in code.

Which namespace is used for implementation of chart control in asp net?

</asp:ChartArea> </chartareas>


2 Answers

Assuming you installed the charting framework without any hitches:-

View:-

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="chart"></div>
        <asp:Chart id="Chart1" runat="server"/>
    </form>
</body>
</html>

Codebehind:-

using System;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
      //SET UP THE DATA TO PLOT  
        double[] yVal = { 80, 20 };
        string[] xName = { "Pass", "Fail" };

      //CREATE THE CHART
        // Don't need to create the chart because it's a control!

      //BIND THE DATA TO THE CHART
        Chart1.Series.Add(new Series());
        Chart1.Series[0].Points.DataBindXY(xName, yVal);

      //SET THE CHART TYPE TO BE PIE
        Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
        Chart1.Series[0]["PieLabelStyle"] = "Outside";
        Chart1.Series[0]["PieStartAngle"] = "-90";

      //SET THE COLOR PALETTE FOR THE CHART TO BE A PRESET OF NONE 
      //DEFINE OUR OWN COLOR PALETTE FOR THE CHART 
        Chart1.Palette = System.Web.UI.DataVisualization.Charting.ChartColorPalette.None;
        Chart1.PaletteCustomColors = new Color[] { Color.Blue, Color.Red };

      //SET THE IMAGE OUTPUT TYPE TO BE JPEG
        Chart1.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageType.Jpeg;

      //ADD A PLACE HOLDER CHART AREA TO THE CHART
      //SET THE CHART AREA TO BE 3D
        Chart1.ChartAreas.Add(new ChartArea());
        Chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

      //ADD A PLACE HOLDER LEGEND TO THE CHART
      //DISABLE THE LEGEND
        Chart1.Legends.Add(new Legend());
        Chart1.Legends[0].Enabled = false;
    }
}

Check out http://weblogs.asp.net/scottgu/archive/2008/11/24/new-asp-net-charting-control-lt-asp-chart-runat-quot-server-quot-gt.aspx

like image 81
Iain Galloway Avatar answered Oct 21 '22 11:10

Iain Galloway


Why the dynamic rendering approach? Why not just define the tag as:

And set the attributes on the control? Alternatively, you could try putting a and setting the text as the rendered response, which will wrap it with a span. You could try out other controls to do this with if you get an error with that, like LiteralControl.

like image 41
Brian Mains Avatar answered Oct 21 '22 12:10

Brian Mains