After solving this problem with asp.net multi-series chart programmatically, I found another problem in another multi-series chart:
I want to show a monthly sales report, showing the evolution of the last m
months in approved and rejected proposals. Some months, there aren't any rejected proposals, so the following query (C#/Oracle) returns some empty results.
select to_char(c.date,'YYYYMM') ctb_month,
c.approved,
count(distinct c.f1) amt_c,
count(b.f1) amt_b,
sum(nvl(b.value,0)) sum_values
from bens b
join contracts c
on b.contract_id = c.f1
where b.seller = :USR_ID
AND c.date
BETWEEN add_months(:DATAI,:MONTHS) AND :DATAI
group by to_char(c.date,'YYYYMM'), c.approved
order by ctb_month
OBS: Before binding the :MONTHS
parameter, I make sure its value is negative.
Example of the query result:
CTB_MONTH APPROVED AMT_C AMT_B SUM_VALUES
201209 APPROVED 10 20 1234.56
201209 PENDING 3 3 120.21
201210 APPROVED 12 18 850.52
201210 PENDING 4 4 158.71
201210 REJECTED 1 1 80.40
NOTE: in this case, there weren't any rejected proposal in 201209 for the current seller.
Code to fill the chart:
I stored the data passed by the lower layers in a var report
and filtered the data by it's APPROVED
status:
var approved = queryResult
.Where(r => r.APPROVED == "APPROVED")
.ToList()
;
var rejected = queryResult
.Where(r => r.APPROVED == "REJECTED")
.ToList()
;
var pending = queryResult
.Where(r => r.APPROVED == "PENDING")
.ToList()
;
Then, I'm creating the series in a loop
for (int i = 0; i < 6; i++) {
Series temp = new Series {
XAxisType = AxisType.Primary,
XValueType = ChartValueType.DateTime,
YAxisType = AxisType.Primary,
//mostra só a quantidade de contratos
IsValueShownAsLabel = i % 2 == 0 ? true : false,
ChartType = SeriesChartType.Column,
CustomProperties = "EmptyPointValue=Zero",
Legend = "Legenda"
};
grafico.Series.Add(temp);
}
And DataBinding each series by hand
// approved contracts
grafico.Series[0].Points.DataBindXY(approved, "MONTH", approved, "AMT_C");
grafico.Series[0].LegendText = "Cont. approved";
// approved bens
grafico.Series[1].Points.DataBindXY(approved, "MONTH", approved, "AMT_B");
grafico.Series[1].LegendText = "Ben. approved";
grafico.Series[1].ChartType = SeriesChartType.Line;
// pending contracts
grafico.Series[2].Points.DataBindXY(pending, "MONTH", pending, "AMT_C");
grafico.Series[2].LegendText = "Cont. pending";
// pending bens
grafico.Series[3].Points.DataBindXY(pending, "MONTH", pending, "AMT_B");
grafico.Series[3].LegendText = "Ben. pending";
grafico.Series[3].ChartType = SeriesChartType.Line;
// rejected contracts
grafico.Series[4].Points.DataBindXY(rejected, "MONTH", rejected, "AMT_C");
grafico.Series[4].LegendText = "Cont. rejected";
// rejected bens
grafico.Series[5].Points.DataBindXY(rejected, "MONTH", rejected, "AMT_B");
grafico.Series[5].LegendText = "Ben. rejected";
grafico.Series[5].ChartType = SeriesChartType.Line;
NOTES: grafico
is my Chart object; Some of the visual settings are defined in the <asp:Chart>
tag.
When I run the app, the complete series are correctly drawn, but the incomplete series (201209 / REJECTED, in the above example) is drawn in the wrong X
coordinates (the values for 201210 are draw in the column for 201209, in this case), as if the control were ignoring the X
values passed in the DataBindXY
method and drawing the values in sequence.
Does somebody know how to fix this issue? Thanks in advance.
[SOLVED] Thanks to @jbl, the chart now draws the values in correct places.
The code:
var allMonths = queryResult
.Select(x => x.MONTH)
.Distinct()
.OrderBy(mes => mes)
;
foreach (var mes in allMonths) {
bool hasData = rejected.Any(r => r.MONTH == mes);
if (hasData == false) {
rejected.Add(new MonthlyData() { MONTH = mes, APPROVED = "REJECTED" });
}
hasData = pending.Any(r => r.MONTH == mes);
if (hasData == false) {
pending.Add(new MonthlyData() { MONTH = mes, APPROVED = "PENDING" });
}
hasData = approved.Any(r => r.MONTH == mes);
if (hasData == false) {
approved.Add(new MonthlyData() { MONTH = mes, APPROVED = "APPROVED" });
}
}
approved = approved.OrderBy(v => v.MONTH).ToList();
pending = pending.OrderBy(v => v.MONTH).ToList();
rejected = rejected.OrderBy(v => v.MONTH).ToList();
grafico.ChartAreas[0].AxisX.Interval = 1;
grafico.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
grafico.ChartAreas[0].AxisX.IntervalOffset = 1;
grafico.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Months;
Note: MonthlyData is a simple class that transports the values for every line in the query result.