How to create an Excel chart, lable the axes and change their front size
6 views (last 30 days)
Show older comments
Hi :) maybe someone here can help me.
I have to create an Excel chart (line chart), label the axes and change their font size. But i don't no how.
A sample of my code would be the following...
excelApp = actxserver('Excel.Application');
ewb = excelApp.Workbooks.Open(Destination); % open file (enter full path!)
myWorkSheet = ewb.Worksheets.get('Item', numofFiles); % ...get('Item', 1 2 ...) => sheet1 sheet2 ...
myChartObject = myWorkSheet.ChartObjects.Add(10, 275, 500, 250);
myPlots = myChartObject.Chart;
myPlots.HasTitle = true;
myPlots.ChartTitle.Text = 'Name Diagramm';
line1 = myPlots.SeriesCollection.NewSeries;
myPlots.SeriesCollection(1).XValue = resultsData(1:20,1); % X-axis
myPlots.SeriesCollection(1).Values = resultsData(1:20,2); % Y-axis
line1.ChartType = 'xlLine';
line1.Name = 'Name Linie';
ewb.Save
ewb.Close(false)
excelApp.Quit
Would be cool if someone could help me :)
0 Comments
Answers (1)
Nipun Agarwal
on 18 Jun 2020
Edited: Nipun Agarwal
on 18 Jun 2020
Hey,
You need to modify the way you are adding charts to the excel sheet. You can use following commands to add charts and adding other features.
chart1 = ewb.Charts.Add % To add the Chart
To change the chart type use
chart1.ChartType = 'xlLine';
To add the Xaxis Title use,
chart1.Axes(1).HasTitle = true.
chart1.Axes(1).AxisTitle.Text = 'X axis Title'.
To change the font size you can use
chart1.Axes(1).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 20;
Attaching the full code for your reference.
ewb = excelApp.Workbooks.Open('Put file Path here');
myWorkSheet = ewb.Worksheets.get('Item', 1);
rg1 = myWorkSheet.Range('$A:$A'); %getting x axis data from excel sheet column A
rg2 = myWorkSheet.Range('$B:$B'); %getting x axis data from excel sheet column B
rg = excelApp.Union(rg1, rg2);
chart1 = ewb.Charts.Add;
chart1.SetSourceData(rg); %Adding data to chart from excel sheet
chart1.ChartType = 'xlLine'; %defining chart type
chart1.Axes(1).HasTitle = true;
chart1.Axes(1).AxisTitle.Text = 'X axis Title'; % XAxes
chart1.Axes(2).HasTitle = true;
chart1.Axes(2).AxisTitle.Text = 'Y axis Title'; %YAxes
chart1.Axes(1).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 20; %changing font Size of X axis
chart1.Axes(2).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 20; %changing font size of Y axis
ewb.Save
ewb.Close(false)
excelApp.Quit
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!