【问题标题】:Chart series label as a percent图表系列标签为百分比
【发布时间】:2015-07-20 13:21:48
【问题描述】:

我通过 System.Web.UI.DataVisualization.Charting 在 MS Visual Studio 2010 中使用 Chart 组件。我在使用柱形图时遇到问题,我想将标签显示为百分比。该图显示了全年每个月的决策数量(正面 - 绿色,负面 - 红色,中性 - 蓝色)。麻烦的是,如果我使用以下命令...

ChartDecisionDyn.Series["Positive"].IsValueShownAsLabel = true;
ChartDecisionDyn.Series["Positive"].Label = "#PERCENT";

...我没有得到假定的百分比结果。显示的结果说明了某个月份的积极决策数量/全年积极决策的数量,但我想要的结果是某个月份的积极决策数量/某个月份的总决策数量。有人有什么建议吗?提前感谢您的帮助。

你可以看到我的图表的详细信息here

【问题讨论】:

  • 不是一个放置图片的好地方,imo!
  • 对于图片,我很抱歉,我想出了一个更好的方法来存储图片。您可以查看此处描述的图表link。我希望绿色列上的标签显示的是 5 月的 40%、6 月的 67% 和 7 月的 100%

标签: c# asp.net .net charts mschart


【解决方案1】:

看不到您图表的图像,但我这样做了:

<asp:Chart ID="Chart1" runat="server" DataSourceID="ObjectDataSource1" Width="451px">
    <Series>
        <asp:Series Name="Series1" XValueMember="Month" YValueMembers="Percentage"></asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
            <AxisY>
                <LabelStyle Format="P0" />
            </AxisY>
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

或者这个:

    Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "P0";

得到了这个:

编辑:这个怎么样:

<asp:Chart ID="Chart1" runat="server" DataSourceID="ObjectDataSource1" Width="451px">
    <Series>
        <asp:Series Name="Series1" XValueMember="Month" YValueMembers="Percentage" IsValueShownAsLabel="True" LabelFormat="F2"></asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
            <AxisY>
                <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
                <LabelStyle Format="P0" />
            </AxisY>
            <AxisX>
                <MajorGrid Enabled="False" />
            </AxisX>
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="WebApplication9.DataPoint" DeleteMethod="Remove" InsertMethod="Add" SelectMethod="ToArray" TypeName="WebApplication9.DataPointList" UpdateMethod="Add"></asp:ObjectDataSource>

编辑 2:添加多个系列。

<asp:Chart ID="Chart1" runat="server" DataSourceID="ObjectDataSource1" Width="499px">
    <Series>
        <asp:Series Name="Percent" XValueMember="Month" YValueMembers="Percent" IsValueShownAsLabel="True" LabelFormat="P0" Legend="Legend1" YAxisType="Secondary"></asp:Series>
        <asp:Series ChartArea="ChartArea1" IsValueShownAsLabel="True" LabelFormat="N0" Legend="Legend1" Name="Positive" XValueMember="Month" YValueMembers="Positive">
        </asp:Series>
        <asp:Series ChartArea="ChartArea1" IsValueShownAsLabel="True" LabelFormat="N0" Legend="Legend1" Name="Neutral" XValueMember="Month" YValueMembers="Neutral">
        </asp:Series>
        <asp:Series ChartArea="ChartArea1" IsValueShownAsLabel="True" LabelFormat="F0" Legend="Legend1" Name="Negative" XValueMember="Month" YValueMembers="Negative">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
            <AxisY>
                <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
            </AxisY>
            <AxisX>
                <MajorGrid Enabled="False" />
            </AxisX>
            <AxisY2>
                <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
                <LabelStyle Format="P0" />
            </AxisY2>
        </asp:ChartArea>
    </ChartAreas>
    <Legends>
        <asp:Legend Alignment="Center" Docking="Top" Name="Legend1">
        </asp:Legend>
    </Legends>
</asp:Chart>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="WebApplication11.DecisionPoint" DeleteMethod="Remove" InsertMethod="Add" SelectMethod="ToArray" TypeName="WebApplication11.DecisionPointList"></asp:ObjectDataSource>

【讨论】:

  • 也谢谢你,jstreet,但是这只会改变 Y 轴以显示百分比......我仍然想将它作为一个值,但将标签显示为总决策的百分比在某月
  • @duwen_blade 不确定哪个“标签”应该是百分比或数字,但现在看看。
  • @duwen_blade 基本上我使用“P0”表示百分比,“F2”表示普通数字。您可以轻松地在 Y 轴和系列标签之间切换它们,以获得您想要的结果。
  • 谢谢,这有点帮助,但是,麻烦的是,我在我的图表中使用了多个系列。如果我将标签显示为百分比(例如 P0),则百分比是从整个图表(所有系列)中计算的。我只需要从 1 serie 算起。
  • 我找到了一种更好的存储图像的方法,所以this 可能会帮助您了解我的意思
【解决方案2】:

ChartDecisionDyn.Series["Positive"].LabelFormat="#.00′ %'";一样使用ChartDecisionDyn.Series["Positive"].LabelFormat

【讨论】:

  • 感谢 rt2800 的建议,我试过了,不过现在看起来像 this
【解决方案3】:

我的理解是这些选项是相互排斥的。第二个将覆盖第一个。设置IsValueShownAsLabel=true如何设置正点的值=positive/(positive+negative+neutral)*100

或设置系列Label="#LABEL" 并在添加点的值时也将等于positive/(positive+negative+neutral)*100 的点标签添加为字符串

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 2018-05-11
    相关资源
    最近更新 更多