Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Dec 17, 2021 2:22 pm

HI,
I have two questios:

1. I hace a Line Chart but my horizontal Axes no adjust to data. Id like my axes from values 0 to 6. Attach image "Sin título1.png".
2. It is possible that the legend xx is collocated on the Y axis, that is, on the 140. Similar to sample in attach "Sin título2.png"

This my actual Code

//My principal fucntion:
public voidCreateGraf01()
{
SpireHelpers spireHelpers = new SpireHelpers();

Workbook workbook = new Workbook();
//Initailize worksheet
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "TRA";
sheet.GridLinesVisible = false;
//Writes chart data
CreateChartData(sheet);
//Add a new chart worsheet to workbook
Chart chart = sheet.Charts.Add();
chart.ChartType = ExcelChartType.Line;
//Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 6;
chart.RightColumn = 11;
chart.BottomRow = 29;
//Chart title
chart.ChartTitle = "";
//Set region of chart data
chart.DataRange = sheet.Range["B1:H2"];
chart.SeriesDataFromRange = true;

Spire.Xls.Charts.ChartSerie cs = chart.Series[0];

//Chart Labels resource
cs.CategoryLabels = sheet.Range["B1:H1"];
//Chart value resource
cs.Values = sheet.Range["B2:H2"];

cs.UsePrimaryAxis = true;

//Division Lines
chart.PrimaryCategoryAxis.HasMajorGridLines = true;
chart.PrimaryCategoryAxis.HasMinorGridLines = true;
chart.PrimaryValueAxis.HasMajorGridLines = true;
chart.PrimaryValueAxis.HasMinorGridLines = true;
chart.SecondaryCategoryAxis.HasMajorGridLines = true;
chart.SecondaryCategoryAxis.HasMinorGridLines = true;
chart.SecondaryValueAxis.HasMajorGridLines = true;
chart.SecondaryValueAxis.HasMinorGridLines = true;
//Division Lines Color

chart.PrimaryValueAxis.MajorGridLines.LineProperties.Color = Color.Red;
chart.PrimaryValueAxis.MinorGridLines.LineProperties.Color = Color.Black;
chart.PrimaryCategoryAxis.MajorGridLines.LineProperties.Color = Color.Red;
chart.PrimaryCategoryAxis.MinorGridLines.LineProperties.Color = Color.Black;

chart.PrimaryCategoryAxis.CrossValue = 0;
chart.PrimaryValueAxis.CrossValue = 0;
chart.SecondaryCategoryAxis.CrossValue = 0;
chart.SecondaryValueAxis.CrossValue = 0;




//Chart Axes
chart.PrimaryCategoryAxis.Title = "%Elongación";
chart.PrimaryCategoryAxis.LabelFrequency = 1;
chart.PrimaryCategoryAxis.MinValue = 0;
chart.PrimaryCategoryAxis.MaxValue = 6;
chart.PrimaryCategoryAxis.Font.Color = Color.Black;



chart.PrimaryValueAxis.IsReverseOrder = false;
chart.PrimaryValueAxis.Title = "Fuerza (KN)";
chart.PrimaryValueAxis.IsAutoCross = true;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.MaxValue = 140;
chart.PrimaryValueAxis.CrossesAt = 0;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 0;
chart.SecondaryCategoryAxis.Title = "Fuerza1 (KN)";
chart.SecondaryValueAxis.Title = "SecondaryValueAxis";
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.PlotArea.Visible = false;
chart.Legend.Delete();
workbook.SaveToFile(@"D:\TEMP\IMAGES\GRAF_001.xlsx");

}

// My data
private void CreateChartData(Worksheet sheet)
{
sheet.Range["B1"].Value = "0";
sheet.Range["B2"].NumberValue = 0;
sheet.Range["C1"].Value = "1";
sheet.Range["C2"].NumberValue = 7.5;
sheet.Range["D1"].Value = "2";
sheet.Range["D2"].NumberValue = 7.7;
sheet.Range["E1"].Value = "3";
sheet.Range["E2"].NumberValue = 8;
sheet.Range["F1"].Value = "4";
sheet.Range["F2"].NumberValue = 98;
sheet.Range["G1"].Value = "5";
sheet.Range["G2"].NumberValue = 100;
sheet.Range["H1"].Value = "6";
sheet.Range["H2"].NumberValue = 110;
sheet.Range["B2:H2"].Style.NumberFormat = "0.0"; //"\"$\"#,##0";
}


Thanks.
Attachments
Sin título2.png
Sin título2.png (186.25 KiB) Viewed 817 times
Sin título.png
Sin título.png (29.32 KiB) Viewed 817 times

E.PTENA@LKS.ES
 
Posts: 8
Joined: Fri Nov 21, 2014 10:40 am

Mon Dec 20, 2021 6:07 am

Hello,

Thanks for your inquiry!

Please refer to the following code to achieve your needs.

1. Set the category axis from values 0 to 6:
Code: Select all
            chart.PrimaryCategoryAxis.MinValue = 0;
            chart.PrimaryCategoryAxis.MaxValue = 7;


2. Change the position of the value axis title:
Code: Select all
            chart.PrimaryValueAxis.TitleArea.X = 350;
            chart.PrimaryValueAxis.TitleArea.Y = 250;


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Dec 20, 2021 8:28 am

Thanks for your answer but it doesn't work.

With respect to point 1,
when I set the minimum and maximum values ​​of the axes, the vertical axes are not positioned at points 0 and 6 but at previous values ​​(before 0 and after 6). You can see in attach image likedo.png


With respect to point 2,
When I write this code generarate Sistem Null Exception

I send the code in my previous post.

Thanks
Attachments
Like.png
Like.png (211.1 KiB) Viewed 798 times

E.PTENA@LKS.ES
 
Posts: 8
Joined: Fri Nov 21, 2014 10:40 am

Mon Dec 20, 2021 9:13 am

Hello,

Thanks for your reply!

For the point 1: Please use the following code to achieve the effect your screenshot shows.
Code: Select all
            chart.PrimaryCategoryAxis.MinValue = 1;
            chart.PrimaryCategoryAxis.MaxValue = 7;
            chart.PrimaryCategoryAxis.AxisBetweenCategories = false;


For the point 2: Sorry that I did not generate System Null Exception on my side. I am using the latest Spire.Xls for Net version 11.12.2, if you did not use the latest version, I just recommend that you can try it first.

Here I also attached the full code I used and the generated file of mine. If the issue still exists, please provide us with your system information (E.g. Win7, 64 bit) and region setting (E.g. China, Chinese) for reference. Thanks in advance.
chart.png
chart.png (12.61 KiB) Viewed 797 times

Sincerely,
Marcia
E-iceblue support team
Attachments
generate.zip
(9.85 KiB) Downloaded 98 times
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Tue Jan 04, 2022 6:05 am

Hello,

Hope you are doing well!

Has the issue been solved now? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.XLS