获取ChartAreas垂直堆叠

时间:2015-12-07 19:55:48

标签: asp.net vb.net charts

我正在动态地将ChartAreas和Series(线型)添加到图表中。每个区域都有一个系列。我没有找到一种方法来控制ChartAreas在图表中的显示方式。

<asp:Chart EnableViewState="false" ID="SMDAServerCPUChart" runat="server" 
    Height="650px" Width="1150px" ImageLocation="~/TempImageFiles1"
    ImageStorageMode="UseImageLocation" Palette="SemiTransparent"
    BorderSkin-SkinStyle="Emboss" BackColor="#DDDDDD" BorderSkin-PageColor="#efeeef">
    <Legends>
        <asp:Legend Name="ServerLegend" BackColor="Transparent" 
            Alignment="Center" IsDockedInsideChartArea="False" Docking="Top" />
    </Legends>
    <Titles>
        <asp:Title Name="SM/DA CPU Utilization" Text="SM/DA CPU Utilization"
            Font="Verdana, 12pt, style=Bold"></asp:Title>
    </Titles>
</asp:Chart>

如果我添加三个区域/系列,每个区域占据图表的整个宽度,它们会像这样垂直堆叠:

Figure A
--------------------------------------------------------
|                       ChartArea/Series               |
--------------------------------------------------------
--------------------------------------------------------
|                       ChartArea/Series               |
--------------------------------------------------------
--------------------------------------------------------
|                       ChartArea/Series               |
--------------------------------------------------------

但是,如果我添加四个,它会将两个放在顶部,两个放在底部:

Figure B
----------------------------        ----------------------------
|     ChartArea/Series     |        |     ChartArea/Series     |
----------------------------        ----------------------------
----------------------------        ----------------------------
|     ChartArea/Series     |        |     ChartArea/Series     |
----------------------------        ----------------------------

我一直试图找到如何指定系列的宽度,但还是无法弄清楚如何做到这一点。 Series.AlignmentOrientation属性似乎没有任何帮助。我一直在玩PixelPointWidth等,但这似乎也没有做任何事情。

我希望系列是图表的整个宽度并垂直堆叠,如上图A所示。

更新:我发现Chart.ChartAreas(0).Position.Width = 95会将ChartArea宽度控制为图表的百分比。但是,一旦设置了,就必须设置其他位置属性,或者将图表全部叠加在同一位置。在这一点上,我假设我将能够使用它 - 比我预期的编码更多。

2 个答案:

答案 0 :(得分:0)

以下是我的想法。评论内联解释

Sub SizeChartAreas(Ch As System.Web.UI.DataVisualization.Charting.Chart)

    'Get the number of ChartAreas, and add 1 to account for the title
    'and legend at the top
    Dim NumAreas = Ch.ChartAreas.Count + 1

    'Since the position values are percentages of the ChartArea,
    '(100 / [number of series]) is the basic formula for incrementing
    'the value that specifies the top (Y).  Subtract 5 on the first
    'calculation to snug up some empty space that is otherwise left
    'between the title/legend and first series.
    Dim SumOfHeights As Integer = (100 / NumAreas) - 5

    'Iterate through each ChartArea
    For Each Area As System.Web.UI.DataVisualization.Charting.ChartArea In Ch.ChartAreas

        'Set the width to 95% of the chart width
        Area.Position.Width = 95

        'Set the height to 100 / NumAreas
        Area.Position.Height = 100 / NumAreas

        'Set the left edge at 1% of the width
        Area.Position.X = 1

        'Set the top edge to SumOfHeights
        Area.Position.Y = SumOfHeights

        'Add the height of a series to SumOfHeights
        SumOfHeights += 100 / NumAreas

    Next

End Sub

答案 1 :(得分:0)

C#实现使用与我最终使用的相同的一般原理。

var h = 0;
var total = chart1.ChartAreas.Count;
var part = 100 / total;
foreach (var area in chart1.ChartAreas) {
    area.Position.Auto = false;
    area.Position.Width = 95;
    area.Position.Height = part;
    area.Position.X = 1;
    area.Position.Y = h;                
    h += part;                
}