Search code examples
c#asp.net.netchartsmschart

Chart series label as a percent


I am using the Chart component in MS Visual Studio 2010 via System.Web.UI.DataVisualization.Charting. I am having trouble with the column graph, where I want to show label as percentage. The graph shows number of decisions (positive - green, negative - red, neutral - blue) in each month throughtout the whole year. Trouble is that if I use the following commands...

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

...I do not get supposed percentage result. The result shown states number of positive decisions in certain month / number of positive decisions throughout the year, but my desired result is number of positive decisions in certain month / number of total decisions on the certain month. Does anyone have any suggestion? Thanks in advance for any help.

You can see the details of my graph here


Solution

  • Could not see the image for your chart but I did this:

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

    OR this:

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

    and got this:

    enter image description here

    EDIT: What about this:

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

    enter image description here

    EDIT 2: Adding multiple series.

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

    enter image description here