使用Aspose.Cells for C#更改条形图轴刻度

时间:2018-06-04 22:25:03

标签: c# excel aspose-cells

我正在尝试使用Aspose.Cells为C#设置条形图的轴刻度。我目前正在做的基本上是这样的:

var chart = worksheet.Charts.Add(ChartType.BarChart, 1, 1, 15, 15)
chart.NSeries.AddSeries("{.015,.03,.04}", true)
chart.NSeries.CategoryData = "{Apples,Oranges,Pears}"
chart.SeriesAxis.IsAutomaticMajorUnit = false;
chart.SeriesAxis.MajorUnit = .01;

但是,当电子表格呈现图表的主要单位比例自动设置为.1时,所有条形看起来都不成比例地小。如何更改系列轴的比例,因此它的刻度标记间隔为.01?

1 个答案:

答案 0 :(得分:1)

请根据您的需要使用以下代码。

<强> C#

//Set the major unit to 0.01
ch.ValueAxis.IsAutomaticMajorUnit = false;
ch.ValueAxis.MajorUnit = 0.01;

以下是完整的示例代码和屏幕截图,其中显示了代码生成的输出Excel文件供您参考。

Set the Chart Axis Major Unit to 0.01 using Aspose.Cells APIs

<强> C#

// Create empty workbook.
Workbook wb = new Workbook();

// Access first worksheet.
Worksheet worksheet = wb.Worksheets[0];

// Add Bar chart in first worksheet.
int idx = worksheet.Charts.Add(ChartType.Bar, 5, 2, 20, 10);

// Access Bar chart.
Chart ch = worksheet.Charts[0];

// Add two number series, true means they are vertical.
ch.NSeries.Add("{.015,.03,.04}", true);

// Set the category data to show on X-axis.
ch.NSeries.CategoryData = "{Apples,Oranges,Pears}";

// Set the name of first and second series.
ch.NSeries[0].Name = "Cricket";

//Set the major unit to 0.01
ch.ValueAxis.IsAutomaticMajorUnit = false;
ch.ValueAxis.MajorUnit = 0.01;

// Save the output in xlsx format.
wb.Save("outputBarChart.xlsx", SaveFormat.Xlsx);

注意: 我在Aspose担任开发人员传播者