画图不难,关键是背景色根据Y轴的值改变,苦苦摸索了好长时间才搞定。就是下图这个样子
前台代码:
<asp:Chart ID="Chart1" runat="server" Width="820" BackColor="#D3DFF0" Palette="BrightPastel" BorderDashStyle="Solid" BackGradientStyle="TopBottom"
BorderWidth="2" BorderColor="26, 59, 105" ImageType="Jpeg" EnableViewState="true">
<Titles>
<asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3" ForeColor="26, 59, 105">
</asp:Title>
</Titles>
<BorderSkin SkinStyle="Emboss" />
<Series>
<asp:Series Name="BMI曲线" ChartType="Line" MarkerSize="5" MarkerStyle="Circle" IsValueShownAsLabel="true">
</asp:Series>
<asp:Series ChartArea="ChartArea1" Name="BMI上限" ChartType="Line" MarkerStyle="Diamond" MarkerSize="1" MarkerStep="1" BorderWidth="1" IsValueShownAsLabel="false" Color="Red">
</asp:Series>
<asp:Series ChartArea="ChartArea1" Name="BMI下限" ChartType="Line" MarkerStyle="Diamond" MarkerSize="1" MarkerStep="1" BorderWidth="1" IsValueShownAsLabel="false" Color="Red">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BackImageWrapMode="TileFlipX" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="Transparent"
BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom">
<AxisY LineColor="64, 64, 64, 64" IsLabelAutoFit="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" />
<MajorGrid LineColor="64, 64, 64, 64" />
</AxisY>
<AxisX LineColor="64, 64, 64, 64" IsLabelAutoFit="False">
<LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" Interval="1" />
<MajorGrid LineColor="64, 64, 64, 64" />
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
后台代码:
添加引用
using System.Web.UI.DataVisualization.Charting;
using MySql.Data.MySqlClient;
using System.Drawing;
如果是VS2008需要引用“System.Web.DataVisualization.dll”。
protected void BindData()
{
this.Chart1.Titles[0].Text = "有子---BMI的正常范围是:18.5~23.9";//图表标题
int recordcount = 0;
DataTable list = GetByYear1(11980, "2012-11-02", "2012-11-15", out recordcount);
if (recordcount > 0)
{
this.Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Black;
52.8-43.2=(float)9.599998(decimal)9.6
//decimal jg = max - min;
//this.Chart1.ChartAreas[0].AxisY.Interval = Convert.ToDouble(jg); //Y轴间隔
//this.Chart1.ChartAreas[0].AxisY.Maximum = Convert.ToDouble(max + (jg * 2));//Y轴最大值
//this.Chart1.ChartAreas[0].AxisY.Minimum = Convert.ToDouble(min - (jg * 2));//Y轴最小值
this.Chart1.ChartAreas[0].AxisX.Interval = 1;
this.Chart1.ChartAreas[0].AxisY.Title = "会员体重(公斤)";//Y轴标题
this.Chart1.ChartAreas[0].AxisY.TitleFont = new Font("Arial Unicode MS", float.Parse("10"), FontStyle.Regular);
this.Chart1.ChartAreas[0].AxisY.Interval = 5.4; //Y轴间隔
this.Chart1.ChartAreas[0].AxisY.Maximum = 34.7; //Y轴最大值
this.Chart1.ChartAreas[0].AxisY.Minimum = 7.7; //Y轴最小值
if (list.Rows.Count > 12)
{
float w = (float)list.Rows.Count / 12;
float g = (float)w * 800;
string[] s = g.ToString("f0").Split(char.Parse("."));
this.Chart1.Width = int.Parse(s[0]);
this.Chart1.ChartAreas[0].Position = new ElementPosition(0, 20, 98, 75);
}
else
{
this.Chart1.Width = 820;
}
#region
体重上下限直线
//Series seriesBar1 = Chart1.Series["BMI上限"];
//Series seriesBar2 = Chart1.Series["BMI下限"];
//for (int i = 0; i < recordcount; i++)
//{
// //seriesBar1.Points.AddY(max);
// //seriesBar2.Points.AddY(min);
// seriesBar1.Points.AddY(23.9);
// seriesBar2.Points.AddY(18.5);
//}
StripLine stripLine1 = new StripLine();
stripLine1.BackColor = Color.FromArgb(147, 180, 224);
stripLine1.StripWidth = 18.5;
//stripLine1.BackGradientStyle = GradientStyle.TopBottom;
//stripLine1.BackImage = "../Images/chartps.jpg";
Chart1.ChartAreas["ChartArea1"].AxisY.StripLines.Add(stripLine1);
StripLine stripLine2 = new StripLine();
stripLine2.BackColor = Color.FromArgb(131, 199, 93);
stripLine2.IntervalOffset = 18.5;
stripLine2.StripWidth = 5.4;
//stripLine2.BackGradientStyle = GradientStyle.TopBottom;
Chart1.ChartAreas["ChartArea1"].AxisY.StripLines.Add(stripLine2);
StripLine stripLine3 = new StripLine();
stripLine3.BackColor = Color.FromArgb(245, 168, 154);
stripLine3.IntervalOffset = 23.9;
stripLine3.StripWidth = 10.8;
//stripLine3.BackGradientStyle = GradientStyle.TopBottom;
Chart1.ChartAreas["ChartArea1"].AxisY.StripLines.Add(stripLine3);
#endregion
//绑定图表
Series series = Chart1.Series["BMI曲线"];
DataView dv = new DataView(list);
series.Points.DataBindXY(dv, "WeightMeasuredTime", dv, "WeightValue");
//设置超出体重上下限的数据点和文本的颜色
for (int i = 0; i < list.Rows.Count; i++)
{
if (float.Parse(list.Rows[i][1].ToString()) > 23.9)
{
this.Chart1.Series[0].Points[i].LabelForeColor = System.Drawing.Color.Red;
this.Chart1.Series[0].Points[i].MarkerColor = System.Drawing.Color.Red;
}
if (float.Parse(list.Rows[i][1].ToString()) < 18.5)
{
this.Chart1.Series[0].Points[i].LabelForeColor = System.Drawing.Color.Blue;
this.Chart1.Series[0].Points[i].MarkerColor = System.Drawing.Color.Blue;
}
}
}
else
{
this.Chart1.Width = 820;
}
}
public DataTable GetByYear1(int AccountID, string startTime, string endTime, out int recordcount)
{
DataTable dt = GetByYear(AccountID, startTime, endTime);
DataTable dtNew = dt.Clone();
dtNew.Columns["WeightMeasuredTime"].DataType = typeof(string);
dtNew.Columns["WeightValue"].DataType = typeof(string);
dtNew.Columns["Stature"].DataType = typeof(string);
recordcount = dt.Rows.Count;
int len = dt.Rows.Count;
for (int i = 0; i < len; i++)
{
DataRow dr = dtNew.NewRow();
dr[0] = dt.Rows[i][0].ToString().Substring(2, 8);
//dr[1] = dt.Rows[i][1];
dr[1] = (float.Parse(dt.Rows[i][1].ToString()) / (float.Parse(dt.Rows[i][2].ToString()) * float.Parse(dt.Rows[i][2].ToString()) * 0.0001)).ToString("0.00");
dtNew.Rows.Add(dr);
}
return dtNew;
}
public DataTable GetByYear(int AccountID, string startTime, string endTime)
{
string sql = string.Format("SELECT WeightMeasuredTime,WeightValue,Stature FROM tbHealth_Weight WHERE AccountID=" + AccountID + " and WeightMeasuredTime between '" + startTime + "' and '" + endTime + "' order by WeightMeasuredTime");
return Query(sql).Tables[0];
}
public static DataSet Query(string SQLString)
{
string ConnectionString = ConfigurationManager.AppSettings["ADOSQLConnectionString"];
using (MySqlConnection connection = new MySqlConnection(ConnectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}