簡體   English   中英

從SpreadsheetGear 2010圖表系列中獲取點值

[英]Getting point values from a SpreadsheetGear 2010 chart series

我正在使用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文檔。

知道我如何獲得用於繪制點的值嗎?

這個答案不是很優雅,但是它可以滿足您的需求。 您已經有了使用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();

這樣,值不受標簽格式的控制。

如果單元格值為null,則在添加ToString()時將引發異常。

這是另一個版本,更改后的行分隔得更多,因此混亂程度更低。 另外,在使用ToString()之前還要檢查null值。

//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;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM