Free Support Forum - aspose.com

How to set axis values in a chart at run time?

Hi,
I am generating a X:Y scatter chart.
The chart is generating at the run time.
I wanted to know How can I set the values of the x-axis and the y-axis.
I mean the range that gets displayed on the x-axis and the y-Axis…


Is there a way that the range comes automatically depending on what values I have
set in the series.

Hi,


Well the range is automatically set based on the values you provide for data Series you provide. If you need to customize and want to set the minimum and maximum values for the Axis, you may use MaxValue and MinValue attributes for ValueAxis and CategoryAxis accordingly. See the sample code below:

Sample code:

Workbook excel = new Workbook();
Worksheet sheet = excel.Worksheets[0];
Cells cells = excel.Worksheets[0].Cells;
cells[“A1”].PutValue(“Daily Rainfall”);
cells[“B1”].PutValue(“Particulate”);
cells[“A2”].PutValue(1.9);
cells[“B2”].PutValue(137);
cells[“A3”].PutValue(3.6);
cells[“B3”].PutValue(128);
cells[“A4”].PutValue(4.1);
cells[“B4”].PutValue(122);
cells[“A5”].PutValue(4.3);
cells[“B5”].PutValue(117);
cells[“A6”].PutValue(5);
cells[“B6”].PutValue(114);
cells[“A7”].PutValue(5.4);
cells[“B7”].PutValue(114);
cells[“A8”].PutValue(5.7);
cells[“B8”].PutValue(112);
cells[“A9”].PutValue(5.9);
cells[“B9”].PutValue(110);
cells[“A10”].PutValue(7.3);
cells[“B10”].PutValue(104);
excel.Worksheets.Add(SheetType.Chart);
ChartCollection charts = excel.Worksheets[1].Charts;
int chartIndex = charts.Add(ChartType.Scatter, 1, 3, 25, 12);
Chart chart = charts[chartIndex];

chart.Legend.Position = LegendPositionType.Top;
chart.Title.Text = “Scatter Chart:Particulate Levels in Rainfall”;
chart.Title.TextFont.Color = Color.Black;
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Size = 12;
chart.ValueAxis.MinValue = 50;
chart.ValueAxis.MaxValue = 200;
chart.NSeries.Add(“Sheet1!B2:B10”, true);
//chart.NSeries.CategoryData = “Sheet1!A2:A10”;
chart.NSeries[0].XValues = “Sheet1!A2:A10”;

chart.CategoryAxis.Title.Text = cells[“A1”].Value.ToString();
chart.ValueAxis.Title.Text = cells[“B1”].Value.ToString();
excel.Save(“d:\test\out_tstscatter_chart.xls”);