gpt4 book ai didi

c# - 如何将在代码隐藏中创建的图表添加到呈现的 html 页面?

转载 作者:太空狗 更新时间:2023-10-29 21:25:32 25 4
gpt4 key购买 nike

我正在尝试完全在代码隐藏中创建一个 .net 图表控件,并将该图表插入网页上的特定位置。

这是我的 html 页面:

<%@ 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>

下面是代码:

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;
}
}

我想在 id="chart"的 div 中呈现图表控件

感谢您的帮助!

最佳答案

假设您顺利安装了图表框架:-

查看:-

<%@ 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>

代码隐藏:-

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;
}
}

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

关于c# - 如何将在代码隐藏中创建的图表添加到呈现的 html 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2454293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com