我試圖創建一個從以下數據表中的圖表: ASP.NET圖表 - 動態圖表錯誤地建
在我的aspx頁面,我有一個簡單的圖表,代碼如下:
<asp:Chart ID="chartTipo1" runat="server" BackColor="Transparent" Palette="Pastel" TextAntiAliasingQuality="High" Width="1400px" Height="500px" Visible="false">
<Series></Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
<AxisX IsStartedFromZero="True" LineColor="LightGray" LabelAutoFitStyle="LabelsAngleStep30" IsLabelAutoFit="true">
<MajorGrid LineColor="White" />
</AxisX>
<AxisY LineColor="LightGray">
<MajorGrid LineColor="LightGray" />
</AxisY>
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend BorderWidth="1"></asp:Legend>
</Legends>
</asp:Chart>
和代碼隱藏:
//first add your series
foreach (DataRow row in dtTipos.DefaultView.ToTable(true, new string[] { "Type"}).Rows)
{
Series series = new Series();
series.Name = (string)row["Type"];
series.ChartType = SeriesChartType.StackedColumn;
chartTipo1.Series.Add(series);
}
// then add your points;
foreach (DataRow row in dtTipos.Rows)
chartTipo1.Series[(string)row["Type"]].Points.AddXY(row["Location"], new object[] { row["Total"] });
double pInicial = 0.5;
for (int i = 0; i < listaLocais.Count; i++)
{
chartTipo1.ChartAreas[0].AxisX.CustomLabels.Add(pInicial, pInicial + 1, listaLocais[i]);
chartTipo1.ChartAreas[0].AxisX.IsStartedFromZero = true;
pInicial = pInicial + 1;
}
我創建了一系列針對每個不同的類型,然後加點。之後,我爲每列添加一個標籤(我應該有6個不同的列,從6個不同的位置)。
正如你可以看到,圖表錯誤的,因其位置「Azeitão」只有相關的一個類型,而不是更多的圖表中顯示。
我在做什麼錯?
這意味着您正在以錯誤的方式構建您的系列文件....首先構建您的位置座標軸,然後僅爲該位置添加客戶端(您應該先檢查客戶端是否屬於該位置,然後再將其添加到系列中) – Hackerman
那麼我的系列應該是類型還是位置? –
如果某個特定的'Location'沒有特定的'Type',則必須添加那個位置類型對,其'Total'值爲0(零)。然後它會按照您的期望工作。對於這種特定類型的堆棧柱狀圖,您不能簡單地將數據錶轉換爲「丟失」數據。圖表本身並不「足夠聰明」,無法爲你全部找到答案。你必須對你的數據進行處理,並將結構和一致性提供給圖表。 – jsanalytics