我在这里试图找出如何获取我的图表,这是代码隐藏,以按日期显示点。我的观点显示完全正常,但每当我尝试将x轴标签设置为数据库中的日期时,程序就会合适。有人能告诉我代码是如何做到这一点的吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace BgTracker.NET.Reports
{
public partial class BPChart : Form
{
private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
public BPChart()
{
InitializeComponent();
}
private void BPChart_Load(object sender, EventArgs e)
{
chart1 = new Chart();
ChartArea chartArea1 = new ChartArea();
chart1.ChartAreas.Add(chartArea1);
chart1.ChartAreas["ChartArea1"].AxisX.Title = "Dates";
chart1.ChartAreas["ChartArea1"].AxisY.Title = "BP Rates";
Series series1 = new Series();
Series series2 = new Series();
Series series3 = new Series();
// Add data points to the first series
string ConnStr1 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCeConnection conn = new SqlCeConnection(ConnStr1);
string GetBPReadings = "SELECT Systolic, Diastolic, DateVal from Bp where DateVal BETWEEN '5/1/2000' AND '5/8/2000'";
SqlCeCommand cmd = new SqlCeCommand(GetBPReadings, conn);
conn.Open();
SqlCeDataReader rdrBP = cmd.ExecuteReader();
while (rdrBP.Read())
{
series1.Points.Add(Convert.ToInt32(rdrBP.GetValue(0).ToString()));
series2.Points.Add(Convert.ToInt32(rdrBP.GetValue(1).ToString()));
series3.AxisLabel = rdrBP.GetValue(2).ToString();
}
conn.Close();
chart1.Series.Add(series1);
chart1.Series.Add(series2);
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
chart1.Series["Series2"].ChartType = SeriesChartType.Line;
chart1.Series["Series1"].Color = System.Drawing.Color.Red;
chart1.Series["Series2"].Color = System.Drawing.Color.Blue;
// Set chart control location
chart1.Location = new System.Drawing.Point(0, 0);
// Set Chart control size
chart1.Size = new System.Drawing.Size(800, 600);
// Add chart control to the form
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.chart1 } );
}
}
}
答案 0 :(得分:0)
我终于找出了我所缺少的东西。要显示数据库中的日期,您必须在系列中将它们设置为Points。为了帮助他人,以下是我用来做的代码。
private void BPChart_Load(object sender, EventArgs e)
{
chart1 = new Chart();
ChartArea chartArea1 = new ChartArea();
chart1.ChartAreas.Add(chartArea1);
chart1.ChartAreas["ChartArea1"].AxisX.Title = "Dates";
chart1.ChartAreas["ChartArea1"].AxisY.Title = "BP Rates";
Series series1 = new Series();
Series series2 = new Series();
chart1.Series.Add(series1);
chart1.Series.Add(series2);
// Add data points to the first series
int i = 0;
string ConnStr1 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCeConnection conn = new SqlCeConnection(ConnStr1);
string GetBPReadings = "SELECT Systolic, Diastolic, [Date] from Bp where [Date] BETWEEN '5/1/2000' AND '5/8/2000'";
SqlCeCommand cmd = new SqlCeCommand(GetBPReadings, conn);
conn.Open();
SqlCeDataReader rdrBP = cmd.ExecuteReader();
while (rdrBP.Read())
{
series1.Points.Add(Convert.ToInt32(rdrBP.GetValue(0).ToString()));
series2.Points.Add(Convert.ToInt32(rdrBP.GetValue(1).ToString()));
chart1.Series["Series1"].Points[i].AxisLabel = rdrBP.GetValue(2).ToString();
i++;
}
conn.Close();
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
chart1.Series["Series2"].ChartType = SeriesChartType.Line;
chart1.Series["Series1"].Color = System.Drawing.Color.Red;
chart1.Series["Series2"].Color = System.Drawing.Color.Blue;
// Set chart control location
chart1.Location = new System.Drawing.Point(0, 0);
// Set Chart control size
chart1.Size = new System.Drawing.Size(800, 600);
// Add chart control to the form
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.chart1 });
}
}