c# - ASP.Net 图表 - 带有未在图表中使用的额外数据的标签?

标签 c# asp.net charts

我有一个 asp:Chart 控件,它运行良好。我只是传递时间(以军事格式),然后是请求时间的平均长度值。下面的代码满足了我的需要 - 几乎(如果我太过分了,请随意标记它,因为我是图表控件的新手)。

我的数据在表中如下:

Date by Hours 9:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 17:00 18:00 
12/03/2010     8    43    53    55    33    46    51    60    50     9 
Friday       1.773 1.337 1.242 1.239 1.340 1.191 1.479 1.223 1.178 1.516 

给我一​​张漂亮的图表。我的问题在这段代码下面:

List<double> yValues = new List<double>();
List<string> xValues = new List<string>();

// First and Last columns do not contain chartable data
for (int i = 1; i < dtResults.Columns.Count - 1; i++) {
    double d;
    if (double.TryParse(dtResults.Rows[1][i].ToString(), out d))
        yValues.Add(d == 0 ? double.NaN : d);
    else
        yValues.Add(double.NaN);
} // foreach of the Average Time Values the chart

// foreach of the column names
for (int i = 1; i < dtResults.Columns.Count - 1; i++)
    xValues.Add(dtResults.Columns[i].ColumnName);

this.Chart.Titles["Title1"].Text = string.Format(
    "Average Request Time In Seconds On {0:MM/dd/yyyy} Between {1:HH} and {2:HH} In {3}",
    this.DateRange.BeginDate.Value,
    this.ucsTimePicker.BeginTime,
    this.ucsTimePicker.EndTime,
    this.SelectedSourceEnvironmentName
);
this.Chart.Series["Series1"].Points.DataBindXY(xValues, yValues);
this.Chart.Series["Series1"].ChartType = SeriesChartType.Line;
this.Chart.Series["Series1"].IsValueShownAsLabel = true;
this.Chart.Series["Series1"]["ShowMarkerLines"] = "true";
this.Chart.Series["Series1"].Label = "#VALY{0.000}"; // Make sure they have only 3 decimal places

this.Chart.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;
this.Chart.ChartAreas["ChartArea1"].AxisX.Title = "Hours of the Day";
this.Chart.ChartAreas["ChartArea1"].AxisY.Title = "Time in Seconds";

// Handle styling when there is a Zero or missing value
this.Chart.Series["Series1"].EmptyPointStyle.Color = Color.Red;
this.Chart.Series["Series1"].EmptyPointStyle.BorderWidth = 3;
this.Chart.Series["Series1"].EmptyPointStyle.BorderDashStyle = ChartDashStyle.Dash;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerStyle = MarkerStyle.Diamond;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerColor = Color.Red;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerSize = 8;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerBorderColor = Color.Black;
this.Chart.Series["Series1"]["EmptyPointValue"] = "Zero";

有标签显示(上表中的十进制数字)但我想做的是让标签Also显示请求总数,即表中数据的第二行多于。我能够使用以下代码将值添加到图表中:

for (int i = 1; i < dtResults.Columns.Count - 1; i++) {
  int n;
  if (int.TryParse(dtResults.Rows[0][i].ToString(), out n))
    this.Chart.Series["Series1"].Points.AddY(n);
  else
    this.Chart.Series["Series1"].Points.AddY(0);
} // foreach of the Count of Request within the Hour values

这似乎没有任何问题,但我无法通过以下调整访问这些值:

this.Chart.Series["Series1"].Label = "#VALY{0.000}\n#VALY2{0}";

我得到的只是出现两次的原始值 (1.773)。

那么有没有一种方法可以将数据添加到仅用于标记目的的图表中,然后访问它?

最佳答案

好吧,在这里没有任何帮助(这实际上让我感到震惊)之后,我能够在本网站之外的一些帮助下解决这个问题。本质上,我不必添加“额外”数据,但我必须修改每个标签,而不是像下面这样只使用通用标签:

this.Chart.Series["Series1"].Label = "#VALY{0.000}"; // Make sure they have only 3 decimal places      

我还必须删除以下行:

this.Chart.Series["Series1"].IsValueShownAsLabel = true;

因此,为了简洁起见,这里是整个代码,它为我提供了两行标签,其中第一行显示平均值(这是实际的图表数据),第二行是根本不在数据中的计数。

List<double> yValues = new List<double>();
List<string> xValues = new List<string>();
List<int> zValues = new List<int>();

// First and Last columns do not contain chartable data
for (int i = 1; i < dtResults.Columns.Count - 1; i++) {
    double d;
    if (double.TryParse(dtResults.Rows[1][i].ToString(), out d))
        yValues.Add(d == 0 ? double.NaN : d);
    else
        yValues.Add(double.NaN);
} // foreach of the Average Time Values the chart

// foreach of the column names
for (int i = 1; i < dtResults.Columns.Count - 1; i++)
    xValues.Add(dtResults.Columns[i].ColumnName);

this.Chart.Titles["Title1"].Text = string.Format(
    "Average Request Time In Seconds On {0:MM/dd/yyyy} Between {1:HH} and {2:HH} In {3}",
    this.DateRange.BeginDate.Value,
    this.ucsTimePicker.BeginTime,
    this.ucsTimePicker.EndTime,
    this.SelectedSourceEnvironmentName
);
this.Chart.Series["Series1"].Points.DataBindXY(xValues, yValues);

/// This loop will setup the point labels in a two line format where the first line displays
/// the Average that the point is actually showing.  The second line is data taken from the
/// results table and is not a part of the chart at all but is useful information and is the
/// Count of records in that time frame.
/// In order for this to work, the Series property IsValueShownAsLabel needs to be NOT True.
for (int i = 0; i < this.Chart.Series["Series1"].Points.Count; i++) {
    int n = 0;
    int.TryParse(dtResults.Rows[0][i + 1].ToString(), out n);

    this.Chart.Series["Series1"].Points[i].Label = string.Format("Avg: #VALY{{0.000}}\nCount: {0}", n);
} // foreach of the Count of Request within the Hour values

this.Chart.Series["Series1"].ChartType = SeriesChartType.Line;
this.Chart.Series["Series1"]["ShowMarkerLines"] = "true";
this.Chart.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;
this.Chart.ChartAreas["ChartArea1"].AxisX.Title = "Hours of the Day";
this.Chart.ChartAreas["ChartArea1"].AxisY.Title = "Time in Seconds";

// Handle styling when there is a Zero or missing value
this.Chart.Series["Series1"].EmptyPointStyle.Color = Color.Red;
this.Chart.Series["Series1"].EmptyPointStyle.BorderWidth = 3;
this.Chart.Series["Series1"].EmptyPointStyle.BorderDashStyle = ChartDashStyle.Dash;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerStyle = MarkerStyle.Diamond;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerColor = Color.Red;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerSize = 8;
this.Chart.Series["Series1"].EmptyPointStyle.MarkerBorderColor = Color.Black;
this.Chart.Series["Series1"]["EmptyPointValue"] = "Zero";

关于c# - ASP.Net 图表 - 带有未在图表中使用的额外数据的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4369304/

相关文章:

c# - 如何从 MySQL 数据库内容动态创建 Access DB 并保存在本地

excel - 如何在Power Point中通过VBA "Refresh Data"?

c# - 如何在代码后面设置视频标签的来源?

ASP.NET MVC 2 教程

javascript - dc.js 生成包含前 5 个值的饼图

Django 应用程序中的 Javascript 饼图 - 显示动态数据的问题

c# - 如何执行参数化 SQL 查询(使用带@变量的 LIKE)

c# - 使用ImageSharp,将包含Bgra32的传入缓冲区转换为ImageSharp Image <Rgba24>图像的最佳方法是什么?

c# - DateTime.Now 到 mysql 日期时间

asp.net - 更改 ASP.Net WebForms 中的默认 JSON 序列化