listview中的amchart发回

时间:2016-08-02 09:41:29

标签: c# listview amcharts

我有一个webform应用程序,我使用listview列出项目。 在每个项目中都创建了一个amchart,我使用stringBuilder从代码中编写脚本,然后将其插入名为" chartArea"的div中。在每个amchart项中加载脚本:

Literal lit = new Literal();
lit.Text = sb2.ToString();
chartArea.Controls.Add(lit);

以上在第一页加载期间效果很好,但是当我触发回发事件时,我在amchart文件上收到错误:

http://localhost:33154/Scripts/amcharts/amcharts.js第75行第106行未处理的异常 0x80004005 - JavaScript运行时错误:未指定错误。

我该怎么办?

这是我加载图表的代码:

public void buildChart(string ACreg, HtmlGenericControl chartArea)
    {
        Literal lit = new Literal();
        lit.Text = "";
        chartArea.Controls.Add(lit);
        //-------- create chart data table----------
        int Rdays = 90;
        DataTable engChartData = new DataTable();            
        engChartData.Columns.Add("date", typeof(string));
        engChartData.Columns.Add("eng1Val", typeof(string));
        engChartData.Columns.Add("eng2Val", typeof(string));
        DateTime initialDate = DateTime.Now.AddDays(-Rdays);
        for (int i = 0; i <= Rdays; i++)
        {

            string dateMe = initialDate.AddDays(i).ToString("yyyy-MM-dd");
            engChartData.Rows.Add(dateMe, "0", "0");
        }
        //--------- bind chart data-----------------    
        using (APMEntitiesW APMe = new APMEntitiesW())
        {
            var countDate = APMe.Points.Where(x => x.point_Del == false && x.point_Act == true && x.point_Reg == ACreg).GroupBy(g => new { g.point_DateTime, g.point_EGT1, g.point_EGT2 }).Select(lg => new { date = EntityFunctions.TruncateTime(lg.Key.point_DateTime), eng1Val = lg.Key.point_EGT1, eng2Val = lg.Key.point_EGT2 }).ToList();
            foreach (DataRow dr in engChartData.Rows)
            {
                foreach (var data in countDate)
                {
                    string datFromQ = data.date.Value.ToString("yyyy-MM-dd").Replace(" 12:00:00 AM", "");
                    string deteFromDT = dr["date"].ToString().Replace(" 12:00:00 AM", "");
                    if (datFromQ == deteFromDT)
                    {
                        dr["eng1Val"] = data.eng1Val.ToString();
                        dr["eng2Val"] = data.eng2Val.ToString();
                        break;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            foreach (DataRow item in engChartData.Rows)
            {
                string date = string.Format("{0:MMM DD,YYYY}", item["date"]);
                sb.AppendLine(@"{""date"": """ + date + @""",");
                sb.AppendLine(@"""Engine1"": """ + item["eng1Val"] + @""",");
                sb.AppendLine(@"""Engine2"": """ + item["eng2Val"] + @"""},");
            }

            //ChartData = sb.ToString();               
            StringBuilder sb2 = new StringBuilder();
            sb2.AppendLine(@"<script> var chart = AmCharts.makeChart(""chart" + ACreg + @""", {");
            sb2.AppendLine(@"""type"": ""serial"",""categoryField"": ""date"", ""startEffect"": ""easeOutSine"",""dataDateFormat"": ""DD-MM-YYYY"", ""startDuration"": 1,");
            sb2.AppendLine(@"""categoryAxis"": {""gridPosition"": ""start"", ""autoGridCount"": false, ""balloonDateFormat"": ""DD MMM YYYY""},");
            sb2.AppendLine(@" ""chartScrollbar"": {""enabled"": true, ""offset"": 40, ""oppositeAxis"": false, ""scrollbarHeight"": 13},");
            sb2.AppendLine(@" ""chartCursor"": { ""enabled"": true}, ""trendLines"": [],");
            sb2.AppendLine(@"""graphs"": [{""balloonText"": ""[[title]]: [[value]]"", ""bullet"": ""round"", ""id"": ""AmGraph-1"", ""title"": ""Engine1"", ""type"": ""smoothedLine"", ""valueField"": ""Engine1"", ""bullet"": ""bubble"", ""bulletBorderAlpha"": 1, ""bulletSize"": 1,""bulletBorderColor"": ""#FF0000"",""bulletBorderThickness"": 1, ""bulletColor"": ""#000000"",""fillAlphas"": 0.41, },");
            sb2.AppendLine(@"{""balloonText"": ""[[title]]: [[value]]"",""bullet"": ""square"", ""id"": ""AmGraph-2"",  ""title"": ""Engine2"", ""type"": ""smoothedLine"", ""valueField"": ""Engine2"", ""bullet"": ""bubble"",""bulletBorderAlpha"": 1, ""bulletSize"": 1,    ""bulletBorderColor"": ""#0098FF"",""bulletBorderThickness"": 1, ""bulletColor"": ""#0098FF"", ""lineColor"": ""#0098FF"",}],");
            sb2.AppendLine(@"""guides"": [], ""valueAxes"": [ { ""id"": ""ValueAxis-1"",  ""title"": ""Consumption Unit""} ],   ""allLabels"": [], ""balloon"": {}, ""legend"": { ""enabled"": true, ""markerBorderThickness"": 0,   ""useGraphSettings"": true},");
            sb2.AppendLine(@"""titles"": [    { ""id"": ""Title-1"",  ""size"": 15,  ""text"": ""Engine Fuel Consumption""  }  ],   ""dataProvider"": [" + sb.ToString() + " ]   })  </script>");

            lit.Text = sb2.ToString();
            chartArea.Controls.Add(lit);
        }
    }

0 个答案:

没有答案