+ Reply to Thread
Results 1 to 9 of 9

How to draw lines on an excel chart programmatically

  1. #1
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    How to draw lines on an excel chart programmatically

    I have an excel chart (Stock candlestick chart) which I created manually.
    I draw lines on the chart (say a horizontal line from (x1=1 to x2=4 along y = 27)
    You can see the red horizontal line in the screen shot below
    Candle.png

    Though for a quick screenshot purpose, I have shown a column chart above, I actually want to do it on a stock-candlestick chart.

    I am comfortable with writing excel functions which do other stuff. I am also able to call these functions from a cell =MyFunction(10,20). But unable to figure out how to do this for a sub/function/macro which draws a line on the chart.

    From the docs, it seems I need a sub/function which does something like this
    Sub DrawLine()
    ActiveSheets.ChartObjects(1).Shapes.AddLine(1, 27, 4, 27)
    End Sub
    But I have a few problems

    1) I want to the co-ordinates of the lines to be points on the chart (x & y co-ordinates as per the chart axis) - however AddLine seems to be absolute geometric points on the chart rather than points which can be represented by x,y co-ordinates as per the chart axes.

    2) I am unable to figure how to call this DrawLine so as to draw the line. If I call it from the Run Menu of the VBAProject, I get a

    Run Time error 24 - object required.

    So how do I call this? In the future, I will also pass parameters to the sub so as to draw multiple unconnected lines which don't pass through or connect the candle itself. How do I do this? I am experienced programmer but only have a basic experience with MS Office programming.
    Last edited by ExcelBoy90; 12-17-2019 at 06:09 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: How to draw lines on an excel chart programmatically

    Hi,

    Drawing with extra shapes ontop of the chart is never going to go right
    just add a series to your data and make that the line on your chart

    Grtz
    Please be as complete as possible in your asking so it may save use all the time to rework the solution because you didn't give all the requirements. If you have a layout in mind please work it out first so we can adapt our solution to it. Thanks.
    If you have been helped, maybe you could click the *

  3. #3
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to draw lines on an excel chart programmatically

    Quote Originally Posted by Joske920 View Post
    Hi,
    just add a series to your data and make that the line on your chart
    What do you mean? Could elaborate?

    Since this is a stock/candlestick chart, I cannot make a combo chart.
    Last edited by ExcelBoy90; 12-17-2019 at 07:19 AM.

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,127

    Re: How to draw lines on an excel chart programmatically

    How is this? I added a second dataset, added a trendline and then set dataset 2 to have no fill & no border
    Attached Files Attached Files
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

  5. #5
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to draw lines on an excel chart programmatically

    Quote Originally Posted by Glenn Kennedy View Post
    How is this? I added a second dataset, added a trendline and then set dataset 2 to have no fill & no border
    But your primary chart is not a stock/candlestick chart. As I have mentioned in my original question, my chart is a stock/candlestick chart - Excel doesn't allow you to combine it with a different kind of chart. If I add a second series to my chart, excel changes the type of my original chart.

  6. #6
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,127

    Re: How to draw lines on an excel chart programmatically

    Post your sheet. It was not especially helpful to post a non-editable picture of the wrong sort of chart!!! I'll be honest and admit to not having read all the blah blah that was in your post! So it may be possible, it may not be possible.

    Please post an Excel sheet containing representative data. Guidelines for posting a shhet can be found in the yellow banner (top of page).

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,849

    Re: How to draw lines on an excel chart programmatically

    my chart is a stock/candlestick chart - Excel doesn't allow you to combine it with a different kind of chart. If I add a second series to my chart, excel changes the type of my original chart.
    You are correct, Excel does not allow you to combine "specialty" charts (like stock charts) with other charts. However, most of the specialty charts (like stock candlestick charts) can easily be created from normal chart types, which can easily be combined with other chart types. A candlestick chart is essentially a box and whisker plot and a box and whisker plot is easily created as a stacked column chart, which can then be easily combined with a line/scatter chart to get what you want. Here's a tutorial that shows how to build a box and whisker plot complete with an additional line/scatter series: https://peltiertech.com/excel-box-an...ams-box-plots/ Go through the tutorial to understand how it all works, then adapt to your stock data.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Registered User
    Join Date
    12-17-2019
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    30

    Re: How to draw lines on an excel chart programmatically

    Quote Originally Posted by Glenn Kennedy View Post
    Please post an Excel sheet containing representative data. Guidelines for posting a shhet can be found in the yellow banner (top of page).
    I am not doing this here because this thread was about doing it using VBA. I created a separate thread in the Charts sub-forum about this & I have my issue solved for the manual approach - so that part is figured out. I am still interested in the programmatic way of drawing shapes on top of charts in this thread - if at all it's possible.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437

    Re: How to draw lines on an excel chart programmatically

    It's possible to add shapes to a chart using VBA. The difficult part is calculating the position in relation to the source data and scaling being used.
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel VBA : Chart : How do you determine, programmatically the chart range
    By TMS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-03-2016, 09:50 AM
  2. Hi need help to draw a line chart having two lines of data
    By Sabyasachi_Samal in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 08-11-2016, 04:24 PM
  3. How to draw static vertical lines in Excel graph
    By vivekengg in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-03-2011, 10:04 AM
  4. Replies: 6
    Last Post: 08-16-2008, 10:24 AM
  5. Draw lines in a chart
    By ravichander in forum Excel Charting & Pivots
    Replies: 33
    Last Post: 07-12-2008, 08:31 AM
  6. [SOLVED] Excel VBA - How to draw lines & boxes on a Form ?
    By tmb in forum Excel General
    Replies: 1
    Last Post: 04-07-2005, 10:06 PM
  7. how do i draw a chart with excel
    By bkhojolita in forum Excel General
    Replies: 1
    Last Post: 04-01-2005, 08:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1