用 Aspose.Words 净值在折线图中显示最新值
Showing the latest value in a line chart with Aspose.Words Net
按照 Working with Charts 教程,我一直在尝试在当前或上一年的图表线系列中显示最新值。我收到如下所示的两个矩阵,其中它们的计数 属性 始终介于 1 和 2 之间。如前所述,DateTime 矩阵可能有一个或两个列表,每个列表至少有 365 天。第二个矩阵每一天都有相应的double。
List<List<DateTime>> AxisX, List<List<double>> AxisY, List<string> seriesName
shape = (Shape)doc.Range.Bookmarks[bookmarkName.GetDescription()].BookmarkStart.NextSibling;
Chart chart = shape.Chart;
chart.Legend.Position = LegendPosition.TopRight;
chart.Legend.Overlay = true;
chart.Series.Clear();
chart.AxisX.CategoryType = AxisCategoryType.Time;
chart.AxisX.BaseTimeUnit = AxisTimeUnit.Days;
chart.AxisX.MajorUnitScale = AxisTimeUnit.Months;
chart.AxisX.MajorUnit = 1;
chart.AxisX.TickLabelAlignment = ParagraphAlignment.Center;
if (eixoX.Any() && eixoX.ElementAt(0).Any())
{
chart.AxisX.Scaling.Maximum = new AxisBound(eixoX.ElementAt(0).Last());
chart.AxisX.Scaling.Minimum = new AxisBound(eixoX.ElementAt(0).First());
}
直到现在,如果 AxisY[1] 具有如下 continuous 值,我能够在图表中显示最新值:
chart.AxisX.NumberFormat.FormatCode = "mmm";
eixosY.ForEach((eixoY, i) =>
{
if (eixoX.ElementAt(i).Any() && eixoY.Any())
{
var x = eixoX.ElementAt(i).ToArray();
var y = eixoY.ToArray();
var s = nomeSeries.ElementAt(i);
chart.Series.Add(s, x, y);
}
});
然后我遍历 AxisY[1] 并获得最新值如下:
var array = chart.Series.Count - 1;
var serie = eixosY[array].ToArray();
var last = 0;
for (int i = serie.Length - 1; i >= 0; i--)
{
if (Double.IsNaN(serie[i])) continue;
last = i;
break;
}
var labels = chart.Series[array].DataLabels;
ChartDataLabel l = labels.Add(last);
l.ShowValue = true;
按预期产生以下结果:
现在是问题。当 AxisY[1] 不连续时,我无法得到相同的结果。相反,我得到了这样的东西:
我只是无法像上一张图片那样显示元组 X 和 Y 值。我希望图表显示值 57.1,因为它是 AxisY[1]:
中数据的最新值
- y {double[366]} double[]
[0] 29.9338 double
[1] 29.5862 double
[2] NaN double
[3] NaN double
[4] NaN double
[5] NaN double
[6] NaN double
[7] NaN double
[8] NaN double
[9] NaN double
[10] NaN double
[11] NaN double
[12] NaN double
[13] NaN double
[14] NaN double
[15] NaN double
[16] NaN double
[17] NaN double
[18] NaN double
[19] NaN double
[20] NaN double
[21] NaN double
[22] NaN double
[23] NaN double
[24] NaN double
[25] NaN double
[26] NaN double
[27] NaN double
[28] NaN double
[29] NaN double
[30] NaN double
[31] NaN double
[32] NaN double
[33] NaN double
[34] 23.282 double
[35] NaN double
[36] NaN double
[37] NaN double
.
.
.
[147] NaN double
[148] 16.5327 double
.
.
.
.
[254] 57.1 double
任何帮助将不胜感激。提前致谢。
在您的情况下,您不需要使用 DataLabels.Add method, since it is obsolete. It is enough to set ChartSeries.HasDataLabels 属性 到 true
然后通过索引访问所需的 DataLabel
。我创建了一个简单的代码示例,其中 X 值在中间和末尾具有 NaN 值。如屏幕截图所示,数据标签正确显示。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 500, 300);
Chart chart = shape.Chart;
chart.Series.Clear();
double[] xValues = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
double[] yValues = new double[] { 1, 2, double.NaN, double.NaN, 5, 6, 7, 8, 9, 10, 11, double.NaN };
ChartSeries series = chart.Series.Add("Test", xValues, yValues);
// Determine the last no NaN value index.
int last = yValues.Length - 1;
while (double.IsNaN(yValues[last]) && last > 0)
last--;
// Enable data labels.
series.HasDataLabels = true;
// Display the value of the last not NaN value.
series.DataLabels[last].ShowValue = true;
doc.Save(@"C:\Temp\out.docx");
按照 Working with Charts 教程,我一直在尝试在当前或上一年的图表线系列中显示最新值。我收到如下所示的两个矩阵,其中它们的计数 属性 始终介于 1 和 2 之间。如前所述,DateTime 矩阵可能有一个或两个列表,每个列表至少有 365 天。第二个矩阵每一天都有相应的double。
List<List<DateTime>> AxisX, List<List<double>> AxisY, List<string> seriesName
shape = (Shape)doc.Range.Bookmarks[bookmarkName.GetDescription()].BookmarkStart.NextSibling;
Chart chart = shape.Chart;
chart.Legend.Position = LegendPosition.TopRight;
chart.Legend.Overlay = true;
chart.Series.Clear();
chart.AxisX.CategoryType = AxisCategoryType.Time;
chart.AxisX.BaseTimeUnit = AxisTimeUnit.Days;
chart.AxisX.MajorUnitScale = AxisTimeUnit.Months;
chart.AxisX.MajorUnit = 1;
chart.AxisX.TickLabelAlignment = ParagraphAlignment.Center;
if (eixoX.Any() && eixoX.ElementAt(0).Any())
{
chart.AxisX.Scaling.Maximum = new AxisBound(eixoX.ElementAt(0).Last());
chart.AxisX.Scaling.Minimum = new AxisBound(eixoX.ElementAt(0).First());
}
直到现在,如果 AxisY[1] 具有如下 continuous 值,我能够在图表中显示最新值:
chart.AxisX.NumberFormat.FormatCode = "mmm";
eixosY.ForEach((eixoY, i) =>
{
if (eixoX.ElementAt(i).Any() && eixoY.Any())
{
var x = eixoX.ElementAt(i).ToArray();
var y = eixoY.ToArray();
var s = nomeSeries.ElementAt(i);
chart.Series.Add(s, x, y);
}
});
然后我遍历 AxisY[1] 并获得最新值如下:
var array = chart.Series.Count - 1;
var serie = eixosY[array].ToArray();
var last = 0;
for (int i = serie.Length - 1; i >= 0; i--)
{
if (Double.IsNaN(serie[i])) continue;
last = i;
break;
}
var labels = chart.Series[array].DataLabels;
ChartDataLabel l = labels.Add(last);
l.ShowValue = true;
按预期产生以下结果:
现在是问题。当 AxisY[1] 不连续时,我无法得到相同的结果。相反,我得到了这样的东西:
我只是无法像上一张图片那样显示元组 X 和 Y 值。我希望图表显示值 57.1,因为它是 AxisY[1]:
- y {double[366]} double[]
[0] 29.9338 double
[1] 29.5862 double
[2] NaN double
[3] NaN double
[4] NaN double
[5] NaN double
[6] NaN double
[7] NaN double
[8] NaN double
[9] NaN double
[10] NaN double
[11] NaN double
[12] NaN double
[13] NaN double
[14] NaN double
[15] NaN double
[16] NaN double
[17] NaN double
[18] NaN double
[19] NaN double
[20] NaN double
[21] NaN double
[22] NaN double
[23] NaN double
[24] NaN double
[25] NaN double
[26] NaN double
[27] NaN double
[28] NaN double
[29] NaN double
[30] NaN double
[31] NaN double
[32] NaN double
[33] NaN double
[34] 23.282 double
[35] NaN double
[36] NaN double
[37] NaN double
.
.
.
[147] NaN double
[148] 16.5327 double
.
.
.
.
[254] 57.1 double
任何帮助将不胜感激。提前致谢。
在您的情况下,您不需要使用 DataLabels.Add method, since it is obsolete. It is enough to set ChartSeries.HasDataLabels 属性 到 true
然后通过索引访问所需的 DataLabel
。我创建了一个简单的代码示例,其中 X 值在中间和末尾具有 NaN 值。如屏幕截图所示,数据标签正确显示。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Line, 500, 300);
Chart chart = shape.Chart;
chart.Series.Clear();
double[] xValues = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
double[] yValues = new double[] { 1, 2, double.NaN, double.NaN, 5, 6, 7, 8, 9, 10, 11, double.NaN };
ChartSeries series = chart.Series.Add("Test", xValues, yValues);
// Determine the last no NaN value index.
int last = yValues.Length - 1;
while (double.IsNaN(yValues[last]) && last > 0)
last--;
// Enable data labels.
series.HasDataLabels = true;
// Display the value of the last not NaN value.
series.DataLabels[last].ShowValue = true;
doc.Save(@"C:\Temp\out.docx");