用C++对比X轴与类别轴
Contents
[
Hide
]
可能的使用场景
X轴有不同类型。而Y轴是一个值类型轴,X轴可以是分类类型轴或值类型轴。使用值轴,数据被视为连续变化的数值数据,并且标记位于沿轴的变动点,其位置根据其数值而变化。使用分类轴,数据被视为一系列非数值文本标签,并且标记位于沿轴的一个位置,其位置根据其在序列中的位置而变化。下面的示例说明了值轴和分类轴之间的区别。 我们的示例数据如下图中的示例表文件所示。第一列包含我们的X轴数据,可以视为分类或值。请注意,数字不是等间距的,也不按照数字顺序出现。
处理X轴和分类轴,就像处理Microsoft Excel一样
我们将在两种类型的图表上显示这些数据,第一种是XY(散点)图,X作为值轴,第二种是折线图,X作为类别轴。
示例代码
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
Aspose::Cells::Startup();
// Create an instance of Workbook
Workbook workbook;
// Access the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Put the sample values used in charts
worksheet.GetCells().Get(u"A2").PutValue(1);
worksheet.GetCells().Get(u"A3").PutValue(3);
worksheet.GetCells().Get(u"A4").PutValue(2.5);
worksheet.GetCells().Get(u"A5").PutValue(3.5);
worksheet.GetCells().Get(u"B1").PutValue(u"Cats");
worksheet.GetCells().Get(u"C1").PutValue(u"Dogs");
worksheet.GetCells().Get(u"D1").PutValue(u"Fishes");
worksheet.GetCells().Get(u"B2").PutValue(7);
worksheet.GetCells().Get(u"B3").PutValue(6);
worksheet.GetCells().Get(u"B4").PutValue(5);
worksheet.GetCells().Get(u"B5").PutValue(4);
worksheet.GetCells().Get(u"C2").PutValue(7);
worksheet.GetCells().Get(u"C3").PutValue(5);
worksheet.GetCells().Get(u"C4").PutValue(4);
worksheet.GetCells().Get(u"C5").PutValue(3);
worksheet.GetCells().Get(u"D2").PutValue(8);
worksheet.GetCells().Get(u"D3").PutValue(7);
worksheet.GetCells().Get(u"D4").PutValue(3);
worksheet.GetCells().Get(u"D5").PutValue(2);
// Create Line Chart: X as Category Axis
int pieIdx = worksheet.GetCharts().Add(ChartType::LineWithDataMarkers, 6, 15, 20, 21);
// Retrieve the Chart object
Chart chart = worksheet.GetCharts().Get(pieIdx);
// Add Series
chart.GetNSeries().Add(u"B2:D5", true);
// Set the category data
chart.GetNSeries().SetCategoryData(u"=Sheet1!$A$2:$A$5");
// Set the first series name
chart.GetNSeries().Get(0).SetName(u"Cats");
// Set the second series name
chart.GetNSeries().Get(1).SetName(u"Dogs");
// Set the third series name
chart.GetNSeries().Get(2).SetName(u"Fishes");
// Set the Legend at the bottom of the chart area
chart.GetLegend().SetPosition(LegendPositionType::Bottom);
// Fill the PlotArea area with nothing
chart.GetPlotArea().GetArea().GetFillFormat().SetFillType(FillType::None);
// Create XY (Scatter) Chart: X as Value Axis
pieIdx = worksheet.GetCharts().Add(ChartType::ScatterConnectedByLinesWithDataMarker, 6, 6, 20, 12);
// Retrieve the Chart object
chart = worksheet.GetCharts().Get(pieIdx);
// Add Series
chart.GetNSeries().Add(u"B2:D5", true);
// Set X values for series
chart.GetNSeries().Get(0).SetXValues(u"{1,3,2.5,3.5}");
chart.GetNSeries().Get(1).SetXValues(u"{1,3,2.5,3.5}");
chart.GetNSeries().Get(2).SetXValues(u"{1,3,2.5,3.5}");
// Set the first series name
chart.GetNSeries().Get(0).SetName(u"Cats");
// Set the second series name
chart.GetNSeries().Get(1).SetName(u"Dogs");
// Set the third series name
chart.GetNSeries().Get(2).SetName(u"Fishes");
// Set the Legend at the bottom of the chart area
chart.GetLegend().SetPosition(LegendPositionType::Bottom);
// Fill the PlotArea area with nothing
chart.GetPlotArea().GetArea().GetFillFormat().SetFillType(FillType::None);
// Save the Excel file
workbook.Save(u"XAxis.xlsx");
Aspose::Cells::Cleanup();
return 0;
}