I'm trying to set the axis scale for a bar chart using Aspose.Cells for C#. What I'm currently doing is essentially this:
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;
However, when the spreadsheet renders the chart's major unit scale is automatically set to .1, so all the bars look disproportionately small. How can I change the scale of the series axis, so it has tick marks spaced apart by .01?
Please use the following code for your needs.
C#
//Set the major unit to 0.01
ch.ValueAxis.IsAutomaticMajorUnit = false;
ch.ValueAxis.MajorUnit = 0.01;
Here is the full sample code and the screenshot showing the output Excel file generated by the code for your reference.
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);
Note: I am working as Developer Evangelist at Aspose
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.