VS 2008的高图表问题

时间:2013-12-03 14:17:54

标签: c# asp.net .net visual-studio-2008 highcharts

我有一个严重的问题:(我在VS 2008上使用High Charts控件,每当我运行到Web应用程序时,我得到了“Silly”RunTime错误:

  

缺少编译器所需的成员'system.runtime.compilerservices.extensionattribute.ctor。

我不知道原因,我将不胜感激!。

1 个答案:

答案 0 :(得分:0)

首先,我要感谢所有评论我的愚蠢问题的人。特别是,wergeld(@wergeld),他给了我交响乐的第一首旋律:)...

我的解决方案是通过使用StringBuilder类编写图表的Javascript实现来解决这个愚蠢的编译错误,这是解决这个问题的主要思路。

让我们停止聊天:)并看看我的简单工作:

首先,我将所有图表实现插入到一个名为“ChartImplementor.Cs”的类中,如下所示:

public enum ChartTypes
{
    pie,
    column,
    bar
}

public class ChartImplementor
{
    #region Member Variables
    string _title = String.Empty;
    string _subTitle = String.Empty;
    string _YTitle = string.Empty;
    string _XTitle = string.Empty;
    #endregion

    #region Properties
    public string ChartTitle
    {
        set { _subTitle = value; }
        get { return _subTitle; }
    }

    public string ChartSubTitle
    {
        set { _title = value; }
        get { return _title; }
    }

    public string YTitle
    {
        set { _YTitle = value; }
        get { return _YTitle; }
    }

    public string XTitle
    {
        set { _XTitle = value; }
        get { return _XTitle; }
    }
    #endregion

   public string GenerateChartJS(ChartImplementor _chartObj,
        ChartTypes _chartType,
        Dictionary<object, object> _myXYDictionary, string _divID)
    {
        string newX_String = string.Empty;
        string newY_String = string.Empty;
        string ChartScript = string.Empty;
        StringBuilder builder = new StringBuilder();

        if (_chartType == ChartTypes.column || _chartType == ChartTypes.bar)
        {
            foreach (KeyValuePair<object, object> pair in _myXYDictionary)
            {
                string newXValue = String.Format("'{0}',", _myXYDictionary.Keys);
                newX_String += newXValue;
            }

            string XAxisData = string.Format("[{0}]", newX_String);

            builder.Append("<script language='javascript' type='text/javascript'>");
            builder.Append("$(function () {");
            builder.Append("$('#" + _divID + "').highcharts({");
            builder.Append("chart: {type:'" + _chartType + "'},");
            builder.Append("title: {text:'" + _chartObj.ChartTitle + "'},");
            builder.Append("subtitle: {text:'" + _chartObj.ChartSubTitle + "'},");
            builder.Append("xAxis: {title: { text:'" + _chartObj.XTitle + "'},categories:" + XAxisData + ",labels:{enabled:false}},");
            builder.Append("yAxis: { min: 0, title: { text:'" + _chartObj.YTitle + "'}},");
            builder.Append("tooltip: {headerFormat: '<span style=font-size:10px></span>");
            builder.Append("<table style=direction: rtl>',");
            builder.Append("pointFormat: '<tr><td style=color:{series.color};padding:0>{series.name}: </td><td style=padding:0><b>{point.y:.1f}</b></td></tr>',");
            builder.Append("footerFormat: '</table>',");
            //builder.Append("shared: true,");
            builder.Append("useHTML: true},");
            builder.Append("plotOptions: { column: { pointPadding: 0.2, borderWidth: 0}},");
            builder.Append("series:[");

            foreach (KeyValuePair<object, object> pair in _myXYDictionary)
            {
                string newXValue = String.Format("'{0}'", pair.Key);
                builder.Append("{ name:");
                builder.Append("" + newXValue + "");
                builder.Append(",data: ");
                builder.Append("[" + pair.Value + "]},");
            }
            builder.Append("]});");
            builder.Append("});");
            builder.Append("</script>");
            ChartScript = builder.ToString();

        }
        else if (_chartType == ChartTypes.pie)
        {
            builder.Append("<script language='javascript' type='text/javascript'>");
            builder.Append("$(function () {");
            builder.Append("$('#" + _divID + "').highcharts({");
            builder.Append("chart: {plotBackgroundColor: null,plotBorderWidth: null,plotShadow: false},");
            builder.Append("title: {text:'" + _chartObj.ChartTitle + "'},");
            //{series.name}: 
            builder.Append("tooltip: { pointFormat: '<b{point.percentage:.1f}%</b>'},");
            builder.Append("plotOptions: { pie: {allowPointSelect: true,cursor: 'pointer',dataLabels: { enabled: true, color: '#000000',connectorColor: '#000000',    format: '<b>{point.name}</b>: {point.percentage:.1f} %'}}},");
            builder.Append("series:[{type: 'pie',data:[");
            foreach (KeyValuePair<object, object> pair in _myXYDictionary)
            {
                string newXValue = String.Format("['{0}',{1}],", pair.Key, pair.Value);
                builder.Append(newXValue);
            }
            builder.Append("]}] });");
            builder.Append("});");
            builder.Append("</script>");
            ChartScript = builder.ToString();
        }
        return ChartScript;

    }

}

从类中可以看出目标方法包含4个参数:   1. ChartImplementer类的一个实例。   2.来自ChartTypes枚举的对象。   3.An对象字典,其键和值来自对象数据类型(此字典将采用图表的X / Y坐标进行绑定)。   4.将控制图表工作人员的控制ID。

我在字典控件中实现了图表的数据源,并将其作为目标页面中的参数传递如下:

    protected void ImageButtonPopulateChart_Click(object sender, ImageClickEventArgs e)
    {
        int? genderID = null;
        Dictionary<object, object> myXyDataObjects = new Dictionary<object, object>();

        ChartImplementor charting = new ChartImplementor()
        {
            ChartTitle = "A digram illustrates Employees ",
            ChartSubTitle = "HSBC Bank.",
            XTitle = "Employees",
            YTitle = "Salaries"

        };


        var chartQuery = SchoolDC.usp_Chart_Report(SchoolID, genderID, statusID).ToList();
        foreach (var item in chartQuery)
        {
            myXyDataObjects.Add(item.LevelName, item.stCount);
        }

        ScriptManager.RegisterStartupScript(UpdatePanel1,
UpdatePanel1.GetType(),
"script",charting.GenerateChartJS(charting, ChartTypes.column, myXyDataObjects, "container"), // container is the ID of the Div Control which will populate the Chart.
false);
}

这是.aspx页面:

<body>
    <form id="form1" runat="server">    

    <script src="http://code.highcharts.com/highcharts.js"></script>

    <script src="http://code.highcharts.com/modules/exporting.js"></script>

    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto">
    </div>
    <asp:ImageButton ID="ImageButtonPopulateChart" runat="server" onclick="ImageButtonPopulateChart_Click" />
    </form>
</body>

注意:为了能够填充图表,您需要调用Highcharts.Js脚本和Exporting.Js脚本,它负责以多种不同的格式导出图表(我喜欢该功能:))。

RIP史蒂夫乔布斯说,我们对我们的工作充满热情。我非常热衷于在这里分享我的解决方案,所以如果你修改我的代码并找到可以使代码看起来的东西,请更好或甚至任何其他疯狂的想法毫不犹豫地建议我:)。

祝大家度过快乐的一天。