+ Reply to Thread
Results 1 to 28 of 28

Rectangle graph

  1. #1
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Rectangle graph

    Hi,

    I would like to develop an xy-graph consisting of coloured rectangles close to each other.

    The basic data are:

    Please Login or Register  to view this content.
    The x-values represent per cent and the cumulative sum equals 100. The x_cumulative values represent the x-axis in the graph.
    y and x_cumulative values make up the rectangles, in this case 5.
    The rectangles are labelled inside the box and would have different colours.
    The graph should have a secondary y-axis with the same appearance as the primary y-axis.

    I started the programming but came only a small bit.

    Please Login or Register  to view this content.
    Kind regards,

    Jan

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

    Re: Rectangle graph

    Are you after something like this?
    http://www.andypope.info/charts/colwidth.htm
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    It is almost what I am looking for.

    However, I want to have an x-axis going from 0 to 100.
    Besides, I would appreciate a secondary y-axis with the same appearance as the primary one.

    The row with the values 20%, 15%, 12%, 30% etc. from the shown graph can be kept, because there I would like to show the the real x-values, not percentage, from column 2 , i.e. 10, 35, 20, 15, 20 in my test table.

    Kind regards,

    Jan

  4. #4
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    Do you have the VBA-code for the graph linked to your indicated address
    http://www.andypope.info/charts/colwidth.htm ?

    If so the changes might not be so big to get what I am looking for.

    Kind regards,

    Jan

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

    Re: Rectangle graph

    That example does not require any code, so no.

  6. #6
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    It is pity that you don't have the code. I have drawn the graph manually, but if you have many graphs of that type it would make it easier if you had a macro.

    Kind regards,

    Jan

  7. #7
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi,

    I have the following table where x-y-values are the coordinates for the corners of in this case 4 rectangles. The x-axis goes from 0 to 100.
    I am looking for the VBA macro to create this. The rectangles should be in different colors and labelled A, B, C and D.

    Please Login or Register  to view this content.
    Can someone help me?

    Kind regards,

    Jan

  8. #8
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi again,

    The data of the two last lines in the example were somewhat displaced. They should be in the last column.

    Kind regards,

    Jan

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

    Re: Rectangle graph

    In order to plot the rectangle you will need 5 pairs of xy positions.
    To plot a label, assuming the label is center of rectangle you will need another 2 pairs of values. The first would contain no value the second the center point of the rectangle.

    The reason for the non value pair is to cause a break in the line between rectangle and label point.

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I tried to use the macro you wrote but it did not function for me. I suppose the reason is that I do not know exactly how the new basic table should look like. Could you please rewrite my table with your changes so it will function. Your statement Range ("A2:A8") looks curious to me. Is it the values for a single rectangle including the label?

    I have not been able to really test the macro but when I read it I cannot see anything about colors of the rectangles. Will the rectangles have different colors?

    Kind regards,

    Jan

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

    Re: Rectangle graph

    I thought I had attached example file, apparently not
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I have copied your Excel-file with the basic data. When I run the macro I get the message "Application defined or object defined error". The program indicates that the statement "Do while rngDataX.Cells(1, 1).Offset(0, -1) <> " is not correct. What do you say?

    I do not understand the statement Set rngDataX = Range("A2:A8"). Are we then not outside the data of the Excel sheet? Column "A" is a data empty column if I have understood you right.

    Kind regards,

    Jan

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

    Re: Rectangle graph

    change it to
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi again Andy,

    I think I have found the error. I changed Range("A2:A8") to Range("C2:C8") and the program now runs properly.

    But I have still a question. How to get the rectangles colored, not only the borders?

    Kind regards,

    Jan

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

    Re: Rectangle graph

    *** explained in the example are mentioned at the start. In order to fill the rectangles you need to use column charts with x axis as dates.

  16. #16
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    Thank you for all your help. Now, the macro functions properly except that I had hoped to get rectangles filled with different colors. Not only borders in different colors.

    How can the x-y-diagram be changed to column charts to make this possible?

    Kind regards,

    Jan

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

    Re: Rectangle graph

    Combination area/line chart

    Please Login or Register  to view this content.
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I have tried to edit the y-axis and "Major gridlines" somewhat but with little success.

    To improve the appearance of the rectangles I have also tried to add a thin, black border around them but I am uncertain of the VBA code.

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Rectangle graph

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I have tried to develop the graph further but with little success.

    1. Major gridlines should be dotted or dashed but have disappeared completely.

    2. A dotted red line representing the average y-values with the label "Average" in red should be added. (Red data in the data sheet).

    3. A new set of labels (AAAA, BBBB, CCCC, DDDD) have been added but they do not seem to have been included in the graph. Is the location of the graph influencing the data of the cells? The graph covers some of the data.


    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Rectangle graph

    Please Login or Register  to view this content.
    you has x1Dash rather than xlDash. (1 one rather than l L)
    you need to change chart type of 5th series to line
    those new series data labels are at the top of the chart

  22. #22
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I tried to extend the labels with another series (AAAA, BBBB, CCCC, and DDDD), but then the macro does not seem to function at all. Have you any explanation to this?

    Besides, the color of the dotted line representing "average" is still blue, even if the code indicates that the line should be red. Why?

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Rectangle graph

    Please Login or Register  to view this content.
    Charting code between xl2007 and xl2010 is different.

  24. #24
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I have tested the above macro under more realistic conditions. The result is, however, not encouraging.

    If you open the attached file "Rectangle_graph -Sheet1" and run the macro "VariWidthColumnsSmall" it functions properly.

    If you instead open "Rectangle_graph -Sheet2" and run the macro "VariWidthColumnsLarg" it does not function at all. Do you have any explanation to that? The macros should be the same except that they are adjusted for the number of cells included.

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Rectangle graph

    If you remove the x from B1 then Excel will not assume the first column is a series of data rather than category values AND if you change the Minimum scale value from zero to -3, as you data now includes negative values, it will work.

    Please Login or Register  to view this content.
    I have also included a variable to automatically calculate the series indexes for various sections of the code.
    Attached Files Attached Files

  26. #26
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Rectangle graph

    Hi Andy,

    I tried to add another series of data to the graph (AB1:AI1) but then the program indicated an Error. Do you have any explanation to that?

    Kind regards,

    Jan
    Attached Files Attached Files

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

    Re: Rectangle graph

    Do you bother to see what the code actually generates? You may not know the code to fix the problem but at least you would see the problem.

    Because you now have more columns than rows excel switches the expected layout of data. Notice the category axis is no longer numbers but text.

    Please Login or Register  to view this content.
    So force the chart data layout to be by columns.

    Also you will need to alter the lngNCols value so the right series are formatted correctly. For this I have reverted to explicitly setting the value to 8.

  28. #28
    Forum Contributor
    Join Date
    12-30-2013
    Location
    Falun, Sweden
    MS-Off Ver
    Excel 2007
    Posts
    112

    Red face Re: Rectangle graph

    Hi Andy,

    I have got very valuable help from you with the enclosed macro.

    I would like to go somewhat further and test the labels: "AA", "BB", "CC" and "DD".

    Are some of the labels numbers and if so, are some negative, i.e. less than 0. Then negative numbers shall be in red color instead of black in the graph.


    I also wonder if it is possible to choose different color palettes for the graphs? If so, I would like to have somewhat brighter colors than at present.

    Kind regards,

    Jan
    Attached Files Attached Files

+ 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. Puzzling grey rectangle
    By norgro in forum Excel General
    Replies: 10
    Last Post: 12-22-2012, 02:51 AM
  2. [SOLVED] Rectangle with curved corners
    By Zone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-03-2006, 02:40 PM
  3. Delete a rectangle using a macro
    By cailotto@sbcglobal.net in forum Excel General
    Replies: 2
    Last Post: 09-29-2005, 04:05 PM
  4. Code to get a Range RECTangle or PT
    By keepITcool in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-12-2005, 02:05 PM
  5. Rectangle that activates a macro
    By in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2005, 03: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