在以编程方式用asp.net多系列图表解决了这个问题后,我在另一个多系列图表中发现了另一个问题:
我想展示一份月度销售报告,显示最近m
几个月在批准和拒绝提案中的演变。有几个月,没有任何被拒绝的提案,因此以下查询 (C#/Oracle) 返回一些空结果。
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:在绑定:MONTHS
参数之前,我确保它的值是负数。
查询结果示例:
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
注意:在这种情况下,当前卖家在 201209 中没有任何被拒绝的提案。
填充图表的代码:
我将较低层传递的数据存储在 var 中report
,并按其状态过滤数据APPROVED
:
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()
;
然后,我在循环中创建系列
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);
}
并手工对每个系列进行DataBinding
// 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;
注意:grafico
是我的图表对象;一些视觉设置在<asp:Chart>
标签中定义。
当我运行应用程序时,完整的系列被正确绘制,但不完整的系列(201209 / REJECTED,在上面的例子中)被绘制在错误的X
坐标中(201210 的值被绘制在 201209 的列中,在这种情况下) ,好像控件忽略了方法X
中传递的值DataBindXY
并按顺序绘制值。
有人知道如何解决这个问题吗?提前致谢。
[已解决] 感谢@jbl,图表现在将值绘制在正确的位置。
编码:
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;
注意: MonthlyData 是一个简单的类,用于传输查询结果中每一行的值。