动态创建箱形图 [英] Create Box Plot Chart Dynamically
本文介绍了动态创建箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据数据库中的表格动态创建箱形图。
这是我的代码:< br $> b $ b
cs文件:
Hi, I would like to create box plot chart dynamically based on values I have from a table in the database.
This is the codes I have:
cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Web.Configuration;
using System.Collections;
using System.Globalization;
using System.Web.Security;
using System.Web.UI.DataVisualization.Charting;
namespace CREATECHART
{
public partial class CREATECHART: System.Web.UI.Page
{
string sp = ConfigurationManager.AppSettings["SP_Testing"];
string dbConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
Bindchart();
}
public void Bindchart()
{
using (SqlConnection conn = new SqlConnection(dbConn))
{
using (SqlCommand cmd = new SqlCommand(sp, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable ChartData = ds.Tables[0];
string[] XPointMember = new string[ChartData.Rows.Count];
int[] YPointMember = new int[ChartData.Rows.Count];
for (int count = 0; count < ChartData.Rows.Count; count++)
{
XPointMember[count] = ChartData.Rows[count]["Month"].ToString();
YPointMember[count] = Convert.ToInt32(ChartData.Rows[count]["Orders"]);
}
Chart1.Series[0].Points.DataBindXY(XPointMember, YPointMember);
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
Chart1.Series[0].BorderWidth = 10;
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
conn.Close();
}
}
}
}
}
Aspx文件:
Aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CREATECHART.aspx.cs" Inherits="CREATECHART.CREATECHART" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Article by</title>
</head>
<body>
<form id="form1" runat="server">
<h4 style ="color: white;">
Article for
</h4>
<asp:Chart
ID="Chart1"
runat="server"
BackColor =" "192, 64, 0"
BackGradientStyle = "LeftRight"
Height ="360px"
BorderlineDashStyle="DashDotDot"
BorderlineWidth="0"
BorderlineColor="255, 128, 0"
Palette="None"
PaletteCustomColors="128, 64, 0"
Width="380px"
>
<Series>
<asp:Series
Name="Series1"
ChartType="BoxPlot"
YValueType="Double"
ChartArea="ChartArea1"
Color="Snow"
>
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea
Name="ChartArea1"
>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</form>
</body>
</html>
执行代码时出现的错误:名称'Chart1'不具备存在于当前背景下。
有人可以帮我解决这个问题。非常感谢。
This is the error I have when execute the codes: The name 'Chart1' does not exist in the current context.
Can someone please help me on this. Thanks a lot.
推荐答案
您好,
我已经更正了您的aspx页面上的次要代码错误。您可以尝试以下代码。
Hi,
I have corrected minor code mistake on your aspx page. The following code you can try it now.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CREATECHART.aspx.cs" Inherits="CREATECHART.CREATECHART" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Article by</title>
</head>
<body>
<form id="form1" runat="server">
<h4 style="color: white;">Article for
</h4>
<asp:Chart
ID="Chart1"
runat="server"
BackColor="192, 64, 0"
BackGradientStyle="LeftRight"
Height="360px"
BorderlineDashStyle="DashDotDot"
BorderlineWidth="0"
BorderlineColor="255, 128, 0"
Palette="None"
PaletteCustomColors="128, 64, 0"
Width="380px">
<Series>
<asp:Series
Name="Series1"
ChartType="BoxPlot"
YValueType="Double"
ChartArea="ChartArea1"
Color="Snow">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea
Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</form>
</body>
</html>
我希望它可以帮到你。
I hope it can help you.
这篇关于动态创建箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文