如何自定义ASP.NET图表数据绑定到SqlDataSource

时间:2016-08-06 17:43:49

标签: asp.net charts mschart asp.net-charts

我有一个像这样的ASP图表:

我不确定如何使值的标签显示为垂直而不是水平。我不确定这是否可以通过CSS实现,因为结果输出是图像。

Chart appearing as image

我所拥有的只是代码,其中包含以下内容: 在这里看到一些类似的问题:C# chart rotate labels,但它适用于C#,并且给定的解决方案具有C#代码。我正在使用ASP.NET VBScript,而且我没有使用除上述标记之外的任何代码。

另外,请告诉我如何将条形颜色更改为其他颜色。

还有什么我应该检查的吗?

1 个答案:

答案 0 :(得分:2)

LabelAngleColor定义为您想要的值,并停用SmartLabelStyle,如下面的标记所示:

    <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1" Height="400px" Width="600px">
        <Series>
            <asp:Series Name="Series1" IsValueShownAsLabel="True" LabelAngle="-90" LabelFormat="0,0" XValueMember="salesordernumber" YValueMembers="subtotal" Color="Red" Font="Microsoft Sans Serif, 12pt">
                <SmartLabelStyle Enabled="False" />
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
                <AxisY Maximum="3000">
                    <MajorGrid Enabled="False" />
                    <LabelStyle Format="0,0" />
                </AxisY>
                <AxisX>
                    <MajorGrid Enabled="False" />
                </AxisX>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

enter image description here

编辑:使用图表的PreRender事件为系列中的每个数据点指定不同的颜色。下面的示例指定随机颜色,但您可以修改它以指定您想要的任何颜色。

    protected void Chart1_PreRender(object sender, EventArgs e)
    {
        Random r = new Random();

        foreach (DataPoint dp in Chart1.Series[0].Points)
            dp.Color = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
    }

enter image description here

编辑:添加完整的VB代码。

<强> WebForm.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.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>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Chart ID="Chart1" runat="server" Height="400px" Width="600px" DataSourceID="SqlDataSource1">
            <Series>
                <asp:Series Name="Series1" IsValueShownAsLabel="True" LabelAngle="-90" LabelFormat="0,0" XValueMember="salesordernumber" YValueMembers="subtotal" Color="Red" Font="Microsoft Sans Serif, 12pt">
                    <SmartLabelStyle Enabled="False" />
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea Name="ChartArea1">
                    <AxisY Maximum="3000">
                        <MajorGrid Enabled="False" />
                        <LabelStyle Format="0,0" />
                    </AxisY>
                    <AxisX>
                        <MajorGrid Enabled="False" />
                    </AxisX>
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select * from table;"></asp:SqlDataSource>

    </div>
    </form>
</body>
</html>

<强> WebForms.aspx.vb:

Imports System.Web.UI.DataVisualization.Charting
Imports System.Drawing

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Chart1_PreRender(sender As Object, e As EventArgs) Handles Chart1.PreRender
        Dim r As New Random
        For Each dp As DataPoint In Chart1.Series(0).Points
            dp.Color = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))
        Next
    End Sub
End Class