Reputation: 77
I'm trying to create a chart from the following DataTable:
In my aspx page, I have a simple chart, the code is the following:
<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>
And the code-behind:
//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;
}
I create a series for each different Type and then add the points. After this I add a label for each column (I should be having 6 different columns, from 6 different locations).
The resulting chart is the following:
As you can see, the chart is wrongly built, since the Location 'Azeitão' only has one type associated, not more as displayed in the chart.
What am I doing wrong?
Upvotes: 1
Views: 101
Reputation: 77
With @jstreet 's help I was able to fix my problem.
What I did:
I created a list with all the different locations, and a list with all the different types. Then, for each one of my rows in the DataTable, I searched for a row with this location and this type, using
DataRow[] lRow = dtTipos.Select("LOCATION= '" + l + "' AND TYPE= '" + t + "'");
Then, if I had found a row, I would add a Point to the series with the correspondent value, else I would add a Point with value 0 to the column so I could create an empty column for each type that didn't exist for each location.
Problem solved.
Upvotes: 1