Asp.net图表多系列数据显示错误(Asp.net chart multi-series show

2019-10-17 07:22发布

解决后, 这个问题编程用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当前卖方的任何拒绝的提议。

代码来填充图:

我存储在由较低层的变种传入的数据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);
}

和数据绑定每个系列手工

// 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 /拒绝,在上面的例子),在错误中得出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是一个简单的类,输送值在查询结果中的每一行。

Answer 1:

我想你将不得不使用空数据点http://msdn.microsoft.com/en-us/library/dd456677.aspx对齐所有系列

这意味着:

  • 构建所有的x值的集合在系列
  • 做了左与您携手系列所有这些x值的检测所有失分
  • 空数据点填补了失分

希望这将有助于

编辑:你可以试试(未测试)像这样的批准/ AMT_C系列

var allXvalues = queryResult.select(r=>r.CTB_MONTH).Distinct().OrderBy(month=>month).ToList();


allXvalues
  .ForEach
   (
    xvalue=>
      {
        var p = approved.Where(o=>o.MONTH==xValue).FirstOrDefault();
        if(p==null)
            grafico.Series[0].Points.Add(new DataPoint(p.MONTH,p.AMT_C));
        else
            grafico.Series[0].Points.Add(new DataPoint(xvalue,0){IsEmpty=true});
      }
   );


文章来源: Asp.net chart multi-series showing wrong data