MsChart中添加、删除Series
前台
1 <asp:Panel ID="panelch" runat="server" Visible="false" Style="margin-top: 99px; margin-left: 10px;"> 2 <asp:Chart ID="chAnalysis" Width="1025px" runat="server" BackGradientStyle="TopBottom" 3 BackSecondaryColor="White" BackColor="WhiteSmoke" ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)" 4 Visible="False" OnClick="chAnalysis_Click"> 5 <Legends> 6 <asp:Legend IsTextAutoFit="false" LegendStyle="Column" Docking="Bottom" Name="Default" 7 BackColor="Transparent" Font="Trebuchet MS, 8pt"> 8 <Position Height="32" Width="20" X="85" Y="10" /> 9 </asp:Legend> 10 </Legends> 11 <Series> 12 <asp:Series Name="ResultData" ChartType="Line"> 13 </asp:Series> 14 <asp:Series Name="UpperLimit" ChartType="Line"> 15 </asp:Series> 16 <asp:Series Name="LowerLimit" ChartType="Line"> 17 </asp:Series> 18 </Series> 19 <ChartAreas> 20 <asp:ChartArea Name="ChartArea1" BorderColor="64,64,64,64" BackSecondaryColor="White" 21 BackColor="Gainsboro" ShadowColor="Transparent" BackGradientStyle="TopBottom"> 22 <Area3DStyle Rotation="0" Perspective="10" Inclination="15" IsRightAngleAxes="false" 23 WallWidth="0" IsClustered="false" /> 24 <AxisY> 25 <LabelStyle IntervalType="Number" /> 26 </AxisY> 27 </asp:ChartArea> 28 </ChartAreas> 29 </asp:Chart> 30 <br /> 31 <asp:CheckBoxList ID="chbox" runat="server" Width="200" RepeatDirection="Horizontal" 32 Style="float: left; background-color: White;"> 33 <asp:ListItem Text="结果" Value="0" Selected="True"></asp:ListItem> 34 <asp:ListItem Text="上限" Value="1" Selected="True"></asp:ListItem> 35 <asp:ListItem Text="下限" Value="2" Selected="True"></asp:ListItem> 36 </asp:CheckBoxList> 37 虚线 38 <asp:TextBox ID="txtLine" runat="server" Width="100"></asp:TextBox> 39 <asp:Button ID="btnCh" runat="server" Text="确定" OnClick="btnCh_Click" /> 40 </asp:Panel>
后台:
后台
1 #region 删除线或者添加线 2 /// <summary> 3 /// 删除线或者添加线 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 protected void btnCh_Click(object sender, EventArgs e) 8 { 9 BindChart(); 10 this.chAnalysis.Visible = true; 11 if (!chbox.Items[0].Selected) 12 { 13 for (int j = 0; j < this.chAnalysis.Series.Count; j++) 14 { 15 if (this.chAnalysis.Series[j].Name == "ResultData") 16 { 17 this.chAnalysis.Series.Remove(this.chAnalysis.Series[j]); 18 break; 19 } 20 } 21 } 22 if (!chbox.Items[1].Selected) 23 { 24 for (int j = 0; j < this.chAnalysis.Series.Count; j++) 25 { 26 if (this.chAnalysis.Series[j].Name == "UpperLimit") 27 { 28 this.chAnalysis.Series.Remove(this.chAnalysis.Series[j]); 29 break; 30 } 31 } 32 } 33 if (!chbox.Items[2].Selected) 34 { 35 for (int j = 0; j < this.chAnalysis.Series.Count; j++) 36 { 37 if (this.chAnalysis.Series[j].Name == "LowerLimit") 38 { 39 this.chAnalysis.Series.Remove(this.chAnalysis.Series[j]); 40 break; 41 } 42 } 43 } 44 BindDotLine(); 45 46 } 47 #endregion 48 49 public void BindDotLine() 50 { 51 double d = 0.0; 52 if (!string.IsNullOrEmpty(this.txtLine.Text.Trim())) 53 { 54 if (!double.TryParse(this.txtLine.Text.Trim(), out d)) 55 { 56 return; 57 } 58 if (d > this.chAnalysis.ChartAreas[0].AxisY.Maximum) 59 { 60 return; 61 } 62 if (d < this.chAnalysis.ChartAreas[0].AxisY.Minimum) 63 { 64 return; 65 } 66 Series sline = new Series(); 67 sline.Name = "xuxian"; 68 sline.ChartType = SeriesChartType.Line; 69 sline.BorderDashStyle = ChartDashStyle.Dot; 70 if (ViewState["dt"] == null) { return; } 71 DataTable dtResult = (DataTable)ViewState["dt"]; 72 for (int i = 0; i < dtResult.Rows.Count; i++) 73 { 74 sline.Points.AddXY(dtResult.Rows[i]["batch_name"].ToString(), d); 75 } 76 sline.LegendText = "虚线"; 77 this.chAnalysis.Series.Add(sline); 78 }
绑定
1 public void BindChart() 2 { 3 if (ViewState["dt"] == null) { return; } 4 DataTable dtResult = (DataTable)ViewState["dt"]; 5 this.chAnalysis.Series["ResultData"].PostBackValue = "#AXISLABEL"; 6 this.chAnalysis.Series["ResultData"].LegendText = "结果"; 7 this.chAnalysis.Series["ResultData"].MarkerStyle = MarkerStyle.Square; 8 this.chAnalysis.Series["ResultData"].ToolTip = "批次:\t#VALX\n结果:\t#VALY"; 9 this.chAnalysis.Series["UpperLimit"].MarkerStyle = MarkerStyle.Circle; 10 this.chAnalysis.Series["UpperLimit"].LegendText = "上限"; 11 this.chAnalysis.Series["UpperLimit"].ToolTip = "上限:#VALY"; 12 this.chAnalysis.Series["LowerLimit"].MarkerStyle = MarkerStyle.Circle; 13 this.chAnalysis.Series["LowerLimit"].LegendText = "下限"; 14 this.chAnalysis.Series["LowerLimit"].ToolTip = "下限:#VALY"; 15 this.chAnalysis.DataSource = dtResult; 16 this.chAnalysis.Series["ResultData"].XValueMember = "batch_name"; 17 this.chAnalysis.Series["ResultData"].YValueMembers = "RESULTVALUE"; 18 this.chAnalysis.Series["LowerLimit"].XValueMember = "batch_name"; 19 this.chAnalysis.Series["LowerLimit"].YValueMembers = "MIN_LIMIT"; 20 this.chAnalysis.Series["UpperLimit"].XValueMember = "batch_name"; 21 this.chAnalysis.Series["UpperLimit"].YValueMembers = "MAX_LIMIT"; 22 this.chAnalysis.DataBind(); 23 DataTable newdt = CreateWarnTable(); 24 for (int j = 0; j < dtResult.Rows.Count; j++) 25 { 26 DataRow dr = newdt.NewRow(); 27 decimal lowerLimit = decimal.Parse(dtResult.Rows[j]["MIN_LIMIT"].ToString()); 28 decimal upperLimit = decimal.Parse(dtResult.Rows[j]["MAX_LIMIT"].ToString()); 29 decimal resultData = decimal.Parse(dtResult.Rows[j]["RESULTVALUE"].ToString()); 30 dr["MIN_LIMIT"] = lowerLimit; 31 dr["MAX_LIMIT"] = upperLimit; 32 dr["RESULTVALUE"] = resultData; 33 dr["batch_name"] = dtResult.Rows[j]["batch_name"].ToString(); 34 dr["component_name"] = dtResult.Rows[j]["component_name"].ToString(); 35 if (resultData > upperLimit) 36 { 37 this.chAnalysis.Series["ResultData"].Points[j].MarkerColor = Color.Red; 38 newdt.Rows.Add(dr); 39 } 40 if (resultData < lowerLimit) 41 { 42 this.chAnalysis.Series["ResultData"].Points[j].MarkerColor = Color.Red; 43 newdt.Rows.Add(dr); 44 } 45 } 46 chAnalysis.Titles.Add("检测项目质量回顾分析"); 47 DataView dv = dtResult.DefaultView; 48 dv.Sort = "RESULTVALUE desc"; 49 double step = Math.Round((double.Parse(dv[0]["RESULTVALUE"].ToString()) - double.Parse(dv[dv.Count - 1]["RESULTVALUE"].ToString())) / 4, 1); 50 double min = double.Parse(dv[dv.Count - 1]["RESULTVALUE"].ToString()); 51 chAnalysis.ChartAreas[0].AxisY.Maximum = (double)(double.Parse(dv[0]["RESULTVALUE"].ToString()) + step); 52 chAnalysis.ChartAreas[0].AxisY.Minimum = min - step <= 0 ? 0 : min - step; 53 chAnalysis.ChartAreas[0].AxisY.Interval = step; 54 chAnalysis.ChartAreas[0].AxisX.Interval = 1; 55 this.GridView1.DataSource = newdt; 56 this.GridView1.DataBind(); 57 }