//根据文章数,按文章发布时间产生折线图 public string CreateLine(string key, DataTable dt) { ChartTitle title = new ChartTitle() { text = key, subtext = "最近30天" }; ChartOption option = null; option = new BarChartOption(title, "", "line"); string[] days = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31".Split(','); int[] data_int = new int[days.Length]; for (int i = 0; i < days.Length; i++) { data_int[i] = dt.Select("Day=" + (i + 1)).Length; } List <ChartSeries> seriesList = new List <ChartSeries>()//这里的统计算法不对,需要统计出文章数 { new ChartSeries() { name = "文章数", type = "line", data_int = data_int } }; ((BarChartOption)option).AddData(days, seriesList, ""); option.tooltip.formatter = null;//使用默认格式 return(option.ToString()); }
public string BarChartBind(DataTable dt, string type) { //初始化图表 BarChartOption option = new BarChartOption(new ChartTitle() { text = "推广统计", subtext = "按月统计" }, "", type); ChartLegend legend = new ChartLegend(); List <ChartSeries> dataList = new List <ChartSeries>(); //默认按用户统计 List <string> years = new List <string>();//不存数据,只存年 foreach (DataRow dr in dt.DefaultView.ToTable(true, "Year").Rows) { years.Add(dr["Year"].ToString()); } foreach (string year in years) { List <int> months = new List <int>();//存12个月的数据,不足以0补齐 for (int i = 1; i <= 12; i++) { DataRow[] drs = dt.Select("Year=" + year + " AND Month=" + i); if (drs.Length > 0) { months.Add(Convert.ToInt32(drs[0]["PCount"])); } else { months.Add(0); } } dataList.Add(new ChartSeries() { type = type, name = year, data_int = months.ToArray() }); } /*数据完成,填充图表*/ legend.data = years.ToArray(); option.yAxis = new YAxis() { data = years.ToArray() }; //与数据无须name对应,y轴需要name对应 ((BarChartOption)option).AddData("1月,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月".Split(','), dataList, type); option.legend = legend; return(option.ToString()); }
protected void Page_Load(object sender, EventArgs e) { if (function.isAjax()) { string action = Request.Form["action"]; string result = ""; switch (action) { case "createimg": { ChartTitle title = JsonConvert.DeserializeObject <ChartTitle>(Request.Form["title"], new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); //数组中包含空数据等仍会造成问题 string toolbox = Request.Form["toolbox"]; ChartOption option = null; switch (Type) { #region AJAX请求 case "bar": case "line": { option = new BarChartOption(title, toolbox, Type); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); ((BarChartOption)option).AddData(packmod.rowdata, packmod.series, Tag); } break; case "pie": { option = new PieChartOption(title, toolbox); ChartLegend legend = new ChartLegend(); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); legend.data = packmod.rowdata; ((PieChartOption)option).AddData(legend, packmod.series, Tag); } break; case "dash": { option = new DashOption(title, toolbox); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); ((DashOption)option).AddData(packmod.series); } break; case "funnel": { option = new FunnelChartOption(title, toolbox); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); ChartLegend legend = new ChartLegend(); legend.data = packmod.rowdata; ((FunnelChartOption)option).AddData(legend, packmod.series); } break; case "scatter": { option = new ScatterChartOption(title, toolbox); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); ChartLegend legend = new ChartLegend(); legend.data = packmod.rowdata; ((ScatterChartOption)option).AddData(legend, packmod.series); } break; case "circle": { option = new CircleChartOption(title, toolbox); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); ChartLegend legend = new ChartLegend(); legend.data = packmod.rowdata; ((CircleChartOption)option).AddData(legend, packmod.series); } break; case "map": { option = new MapChartOption(title, toolbox); ChartPackage packmod = JsonConvert.DeserializeObject <ChartPackage>(Request.Form["packmod"]); ChartLegend legend = new ChartLegend(); legend.data = packmod.rowdata; ((MapChartOption)option).AddData(legend, packmod.series); } break; #endregion } option.calculable = bool.Parse(Request.Form["calculable"]); result = option.ToString(); break; } } Response.Clear(); Response.Write(result); Response.Flush(); Response.End(); } else if (!IsPostBack) { if (Mid > 0) { chartMod = chartBll.SelReturnModel(Mid); ChartTitle_Hid.Value = chartMod.ChartTitle; Cdate_Hid.Value = chartMod.CDate.ToString(); ToolBox_Hid.Value = chartMod.ToolBox; Package_Hid.Value = chartMod.Package; code.Text = chartMod.option; function.Script(this, "LoadEditConfig();"); } else//调用初始化方法 { function.Script(this, "LoadNewConfig();"); } Call.SetBreadCrumb(Master, "<li><a href='" + customPath2 + "Main.aspx'>工作台</a></li><li><a href='Default.aspx'>智慧图表</a></li><li><a href='AddChart.aspx'>创建图表</a></li><li class='active'><a href='" + Request.RawUrl + "'>图表配置</a>[" + chartMod.GetTypeStr(Type) + "]</li>"); } }