1

我正在使用 SpreadsheetGear 2010 来绘制柱形图,并希望循环遍历所有数据点,将负数着色为红色......但是当我循环遍历时,我看不到获得该点“值”的方法。

以下(将数据标签字符串解释为双精度值)大部分时间都有效:

for (int i = 0; i < chart.SeriesCollection.Count; i++)
{
    positiveColor = Color.FromArgb(79, 129, 189); // blue
    negativeColor = Color.FromArgb(192, 80, 77); // red
    chart.SeriesCollection[i].HasDataLabels = true;

    for (int j = 0; j < chart.SeriesCollection[i].Points.Count; j++)
    {
        double pointValue;

        // If the point is -0.004 but the number format is "0.00",
        // label will be "0.00"
        string label = chart.SeriesCollection[i].Points[j].DataLabel.Text;

        chart.SeriesCollection[i].Points[j].Format.Fill.ForeColor.RGB = 
            (System.Double.TryParse(label, out pointValue) && pointValue >= 0)
            ? positiveColor
            : negativeColor;
    }
}

...但是,如果该值略微为负,则数据标签仅显示为零,因此 pointValue >= 0 并且我将该点解释为正。这会导致恼人的小蓝点从我的 X 轴上垂下来。

SpreadsheetGear.Charts.IPoint似乎没有任何有用的属性来检索用于绘制点的值。

chart.SeriesCollection[i].Values看起来很有希望,但返回一个object字符串解释为“=Data!$B$25:$B$44”。我似乎无法将其转换为任何有用的东西,也找不到任何相关的 SpreadsheetGear 文档。

知道如何获得用于绘制点的值吗?

4

1 回答 1

1

这个答案不是很优雅,但它应该给你你需要的东西。您已经有了使用chart.SeriesCollection[i].Values. 如果使用其中包含的地址chart.SeriesCollection[i].Values,您可以从图表获取值的相同数据中获取值以创建列。

替换定义字符串标签的行代码。

string label = chart.SeriesCollection[i].Points[j].DataLabel.Text;

有了这条线。

string label = chart.Sheet.Workbook.Worksheets[chart.Sheet.Name].Cells[chart.SeriesCollection[i].Values.ToString().Replace("=" + chart.Sheet.Name + "!", "")][0, j].Value.ToString();

这样,值就不受标签格式的控制。

如果单元格值为空,ToString()则添加时会抛出异常。

这是另一个版本,将更改的行分开得更多,因此不会造成混淆。在使用之前还有一个空值检查ToString()

//if you do not have direct access to the worksheet object.
SpreadsheetGear.IWorksheet worksheet1 = chart.Sheet.Workbook.Worksheets[chart.Sheet.Name];
for (int i = 0; i < chart.SeriesCollection.Count; i++)
{
  Color positiveColor = Color.FromArgb(79, 129, 189); // blue
  Color negativeColor = Color.FromArgb(192, 80, 77); // red
  chart.SeriesCollection[i].HasDataLabels = true;

  //Get the address of the series
  string address = chart.SeriesCollection[i].Values.ToString().Replace("=" + chart.Sheet.Name + "!", "");
  for (int j = 0; j < chart.SeriesCollection[i].Points.Count; j++)
  {
    double pointValue;
    //bool usePositiveValueColor = false;
    // If the point is -0.004 but the number format is "0.00",
    // label will be "0.00"
    //string label = chart.SeriesCollection[i].Points[j].DataLabel.Text;
    string label = (worksheet1.Cells[address][0, j].Value != null) 
      ? worksheet1.Cells[address][0, j].Value.ToString() : "0";

    chart.SeriesCollection[i].Points[j].Format.Fill.ForeColor.RGB =
        (System.Double.TryParse(label, out pointValue) && pointValue >= 0)
        ? positiveColor
        : negativeColor;
  }
}
于 2014-01-27T20:43:38.903 回答