2016年4月20日 星期三

[研究][C#][ASP.NET] Chart 繪圖控制項(Control)-長條圖、圓餅圖

[研究][C#][ASP.NET] Chart 繪圖控制項(Control)-長條圖、圓餅圖

2016-04-20
2020-12-02 更新

這是自己參考用的

不用去加入參考 (舊版才要),只要把 Chart 拖入設計畫面即可。


#VALX 會顯示X軸的標籤
#VALY 會顯示Y軸的值
#PERCENT{P1} 會把Y軸的值用百分比的方式表示


            for (int i = 1; i <= 3; i++)
            {
                // add a series to the chart
                string item = "SeriesLayer" + i.ToString();
                Chart1.Series.Add(item);
                Chart1.Series[item].ChartType = SeriesChartType.StackedColumn;
                //Chart1.Series[item].Label = "#ValY";
                //Chart1.Series[item].LegendText = "#ValY";
                Chart1.Series[item].IsValueShownAsLabel = true; 
                for (int a = 1; a <= 5; a++)
                {
                    // add 5 points to the current series 
                    Chart1.Series[item].Points.AddXY(a, 100.0);
                    //Chart1.Series[item].Label = "#VALY{$#.###}";
                    Chart1.Series[item].Label = "#VALY";
                }
            }

********************************************************************************
如果編譯出現錯誤
錯誤 CS0246 找不到類型或命名空間名稱 'Chart' (是否遺漏了 using 指示詞或組件參考?)

解決:
(下圖)在方案的「參考」按下滑鼠右鍵,選「加入參考」。

(下圖) 
( ASP.NET 網站) ASP.NET WebForm 請勾選 System.Web.DataVisualization
( NET 桌面應用程式) ASP.NET WinForm 請勾選 System.Windows.Forms.DataVisualization

( ASP.NET 網站) ASP.NET WebForm 加上
using System.Web.UI.DataVisualization.Charting;

( NET 桌面應用程式) ASP.NET WinForm
using System.Windows.Forms.DataVisualization.Charting;

Web.Config 要加上 ChartImageHandler

ASP.NET 有絕招: 使用 ASP.NET 和 LINQ 圖表 | Microsoft Docs

[ASP.NET] MS Chart簡易範例 | ShunNien 學習筆記 - 點部落


<appSettings>
<add key="ChartImageHandler" value="storage=session;timeout=20;" />
OR
<add key="ChartImageHandler" value="storage=memory;timeout=20;" />
OR
<add key="ChartImageHandler" value="storage=file;timeout=20;" />
OR
<add key="ChartImageHandler" value="storage=file;timeout=20;dir=C:\TempImageFiles;" />
</appSettings>

********************************************************************************
2024-08-20補

Web.Config

<?xml version="1.0"?>
<!--
  如需如何設定 ASP.NET 應用程式的詳細資訊,請前往
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.8">
      <assemblies>
        <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.8"/>
    <!-- 在整合式管線模式下,這裡不需要配置 httpHandlers -->
    <!--<httpHandlers>
      <add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />
    </httpHandlers>-->
    <!-- 在整合式管線模式下,這裡不需要配置 httpHandlers -->
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
  <system.webServer>
    <handlers>
      <!-- 在整合式管線模式下配置處理常式 -->
      <add name="ChartImageHandler" path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" preCondition="integratedMode" />
      <!-- 在整合式管線模式下配置處理常式 -->
    </handlers>
  </system.webServer>
</configuration>

Default.aspx

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

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Chart ID="Chart1" runat="server">
            <Series>
                <asp:Series Name="Series1"></asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
    </form>
</body>
</html>



Default.aspx.cs

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

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Chart1.Series["Series1"].Points.AddXY("January", 100);
            Chart1.Series["Series1"].Points.AddXY("February", 120);
            Chart1.Series["Series1"].Points.AddXY("March", 80);
            Chart1.Series["Series1"].Points.AddXY("April", 140);
        }
    }
}




Default.aspx.cs

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

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            for (int i = 1; i <= 3; i++)
            {
                // add a series to the chart
                string item = "SeriesLayer" + i.ToString();
                Chart1.Series.Add(item);
                Chart1.Series[item].ChartType = SeriesChartType.StackedColumn;
                //Chart1.Series[item].Label = "#ValY";
                //Chart1.Series[item].LegendText = "#ValY";
                Chart1.Series[item].IsValueShownAsLabel = true;
                for (int a = 1; a <= 5; a++)
                {
                    // add 5 points to the current series 
                    Chart1.Series[item].Points.AddXY(a, 100.0);
                    //Chart1.Series[item].Label = "#VALY{$#.###}";
                    Chart1.Series[item].Label = "#VALY";
                }
            }
        }
    }
}



(完)


相關

沒有留言:

張貼留言