【问题标题】:Databind ObjectDataSource with multiple Y-values to MSChart将具有多个 Y 值的 ObjectDataSource 数据绑定到 MSChart
【发布时间】:2013-06-14 20:55:39
【问题描述】:

我有一个简单的对象数据源,形式为 IENumerable<MyClass>,其中 MyClass 是:

class MyClass(){
    public DateTime Key;
    public Int Value1;
    public Int Value2;
    public Int Value3;
}

我希望在设计时使用 ObjectDataSource 对它进行数据绑定,而不是声明式的。 我希望这是:

<asp:Chart runat="server" DataSourceID="datasource">
    <Series>
        <asp:Series XValueMember="Key" YValueMembers="Value1"/>
        <asp:Series XValueMember="Key" YValueMembers="Value2"/>
    </Series>
</asp:Chart>
<asp:ObjectDataSource ID="datasource" runat="server" SelectMethod="DataObjectMethodName" TypeName="DataObjectClassName"/>

然而,每当我尝试对这个映射进行数据绑定时,我都会收到以下异常:

Series data points do not support values of MyClass only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort.

我还尝试让我的数据源返回 Dictionary&lt;DateTime, Int[]&gt;Dictionary&lt;DateTime, MyClass&gt;,但没有成功。

MSCharts 不支持对复杂对象的简单设计时数据绑定吗?我能够绑定到Dictionary&lt;DateTime, int&gt;。 最后,这应该是 StackedColumn 类型的图表。

我看到过类似的问题,例如 Multiple columns chart using asp.net chart control 没有回答这个问题。

【问题讨论】:

    标签: data-binding mschart design-time


    【解决方案1】:

    这些问题似乎在 SO 上不太受欢迎。

    我无法实现任何真正的数据绑定。为了能够继续使用设计器,我只是使用了一个 hack。我为设计师添加了一个系列,所以我可以使用拖放编辑器,让我的生活更轻松。我从“模板”系列中复制了值。

    
        //以一个设计师系列为模板
        var template = chartFares.Series.First();
        //只用于花哨设计器和添加演示设置
        chartFares.Series.Clear();
    
        foreach(系列中的var系列)
        {
            var chartSeries = chartFares.Series.Add(series.Name);
    
            //从模板复制设置(更好的克隆机制也很有用)
            chartSeries.ChartType = 模板.ChartType;
            chartSeries.ToolTip = 模板.ToolTip;
            chartSeries.PostBackValue = 模板.PostBackValue;
    
            foreach(系列中的var点。数据)
            {
                chartSeries.Points.AddXY(points.Period, points.Count);
            }
        }
    
    

    【讨论】:

      猜你喜欢
      • 2016-12-15
      • 2011-08-07
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多