Results 1 to 5 of 5

Thread: [RESOLVED] [Excel]Is it possible to fill in areas of overlap in an excel graph?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    67

    Resolved [RESOLVED] [Excel]Is it possible to fill in areas of overlap in an excel graph?

    My situation: I have an XY scatter plot that plots multiple circles. The number of circles, their size and their position is defined at runtime. What I want to do is to colour in the area of overlap where multiple circles intercept. (What I REALLY want to do is to colour areas differently depending how many circles are overlapping, but... baby steps.) Think of something along the lines of a Venn diagram.

    I found a few solutions for shading underneath a line, or between two lines, that use a second Area chart under the Scatter chart and some tinkering; however the crux is that I need to be able to do this programmatically with no intervention by the user, and the fine tuning required is beyond my current VB programming abilities.
    My third problem is that it needs to be fairly fast. The drawing of the circles already takes noticeable seconds and I'd rather not extend that time considerably for what is essentially a "nice feature" and not a necessary one.

    So, Do any of you have any experience with this? Is it possible? Is it possible but not worth the coding effort and processing time? Is it possible but very hard to implement?

    Do you have any ideas you want to throw around and discuss? I will gladly answer any questions you have concerning both the details of the program and my current level of experience.

    (Just so you know, if you do think you have a solution, a plain English explanation or pseudo-code algorithm is all I'm looking for, I don't require actual coding done.)

    edit: Sorry if my terminology throws you off, I'm looking at this from a straight programming perspective, not really an application-enhancing or macro perspective.
    Last edited by nanoinfinity; May 26th, 2009 at 11:34 AM. Reason: Added last sentence

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    67

    Re: [Excel]Is it possible to fill in areas of overlap in an excel graph?

    No ideas? Is it not do-able, then?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    67

    Re: [Excel]Is it possible to fill in areas of overlap in an excel graph?

    Bringing this up again in case anyone was interested.

    I am sure there are other ways to accomplish this, but they would be way over my level of ability. However, I did find a workable solution - to an extent. It is not ideal for me, but may work for others so I will post it.

    It is possible to use the Drawing tools provided by Excel to trace over the series points in a chart and create a shape out of that. You can then set the shape's properties to be different colours, patterns, and transparency. When shapes with a non-zero and non-100 transparency overlap, they create a visual indicator (in the form of a darker colour) of the overlap.

    If you have a lot of shapes on top of your chart (even partially transparent ones), you may have to draw another set of shapes on top of those ones, but have the fill 100% transparent and leave just a border to represent your original lines. Shapes float above a chart, not under it, so non-transparent or faintly-transparent shapes will obscure your chart liens.

    My issues was that 1) I cannot use the slightly better freeformbuilder method (used in the link where I got my first example) because apparently it is bugged and will crash if given nodes too close together... I believe nodes with a distance between them of > 1, but I haven't tested it fully. My nodes are usually at a distance of 0.0002 (Before alteration by the formula), so you can see it won't work.

    2) The shapes do not render very efficiently, so having many shapes with half-transparency made scrolling the page rather buggy, as the shapes redrew themselves every time the page moved significantly. This was not as much an issue with opaque shapes.

    3)Generation of the shapes is a bit time consuming, and again, having many shapes (Like I do) can make this solution impractical.

    I am now hoping to find some magical way to merge shapes that touch each other together into a single larger shape, but doubt I'll have any luck with that.

    vb Code:
    1. '----The button event where we actually draw the chart
    2. Private Sub cmdPlot_Click()
    3.  
    4.     '---Add series to chart stuff here
    5.  
    6.     '---the following is where we fill in the circles
    7.     For i = 1 To Worksheets(Constants.GRAPH_SHEET).ChartObjects(1).Chart.SeriesCollection.count
    8.         DrawFilledPolygon(Worksheets(Constants.GRAPH_SHEET).ChartObjects(1).Chart, i, 25, 0.75)
    9.    Next i
    10. End Sub
    11.  
    12.  
    13.  
    14. '---Read more: [url]http://www.peltiertech.com/Excel/Charts/VBAdraw.html#FilledPolyCode#ixzz0JvGqwjIa&C[/url]
    15. '-----This sub is adapted form the one given above. The one above uses a freeform shape, but it is a
    16. '-----known bug (in the community, at least) that a freeform-generated shape will crash if the nodes
    17. '-----are too close together (and they will be in this case)
    18. '-----
    19. '-----When given a chart, it will take the indicated series and draw a filled-in polygon out
    20. '-----of the points in that series.
    21.  
    22. Sub DrawFilledPolygon(myChart As Chart, seriesNum As Integer, fillColour As Integer, transVal As Double)
    23.     Dim myPoly As Object
    24.     Dim pointsSeries As Series
    25.     Dim Npts As Integer, Ipts As Integer
    26.     Dim myShape As Shape
    27.     Dim Xnode As Double, Ynode As Double
    28.     Dim Xmin As Double, Xmax As Double
    29.     Dim Ymin As Double, Ymax As Double
    30.     Dim Xleft As Double, Ytop As Double
    31.     Dim Xwidth As Double, Yheight As Double
    32.  
    33.     '---Here we collect the various data we need to be able to position the polygon appropriately.
    34.     With myChart
    35.         Xleft = .PlotArea.InsideLeft
    36.         Xwidth = .PlotArea.InsideWidth
    37.         Ytop = .PlotArea.InsideTop
    38.         Yheight = .PlotArea.InsideHeight
    39.         Xmin = .Axes(1).MinimumScale
    40.         Xmax = .Axes(1).MaximumScale
    41.         Ymin = .Axes(2).MinimumScale
    42.         Ymax = .Axes(2).MaximumScale
    43.         Set pointsSeries = .SeriesCollection(seriesNum)
    44.     End With
    45.     Npts = pointsSeries.Points.count
    46.  
    47.     '---Set the first node. This creates the drawing at the specified point on the chart.
    48.     '---I'm not entirely sure what this formula does (it was from the link) but it works so I'm not messing with it.
    49.     Xnode = Xleft + (pointsSeries.XValues(Npts) - Xmin) * Xwidth / (Xmax - Xmin)
    50.     Ynode = Ytop + (Ymax - pointsSeries.values(Npts)) * Yheight / (Ymax - Ymin)
    51.     '---The following creates the polygon. You add vertexes to change the shape. The issue here is that the polygon is
    52.     '---assumed to be "finished" form the moment of creation, so it is regenerated every time you add a vertex.
    53.     '---I am assuming that using the FreeformBuilder would be superior in this regard.
    54.     '---I also had a hard time finding documentation for this method, so don't really know what the parameters represent.
    55.     Set myPoly = myChart.Drawings.Add(Xnode, Ynode, Xnode, Ynode, False)
    56.  
    57.     '---add the rest of the nodes/points in the series.
    58.     For Ipts = 1 To Npts - 1
    59.         Xnode = Xleft + (pointsSeries.XValues(Ipts) - Xmin) * Xwidth / (Xmax - Xmin)
    60.         Ynode = Ytop + (Ymax - pointsSeries.values(Ipts)) * Yheight / (Ymax - Ymin)
    61.         myPoly.AddVertex Xnode, Ynode
    62.     Next
    63.  
    64.     '---Apply fill effects
    65.     With myPoly
    66.         .ShapeRange.Fill.Visible = msoTrue
    67.         .ShapeRange.Fill.Solid
    68.         .ShapeRange.Fill.ForeColor.SchemeColor = fillColour
    69.         .ShapeRange.Fill.Transparency = transVal
    70.         .ShapeRange.Line.Visible = msoFalse 'makes the line invisible
    71. '        .ShapeRange.Line.ForeColor.SchemeColor = fillColour
    72. '        .ShapeRange.Line.Style = msoLineSingle
    73. '        .ShapeRange.Line.Weight = 2
    74.     End With
    75. End Sub

    I've attached a before and after picture below. The transparency should be lower and I should have drawn outline shapes on top, but I didn't.

    I've marked this thread as resolved because my question WAS resolved - I found a method to indicate areas of overlap, and it WAS too unruly to implement seriously. Although if anyone reading has any questions, feel free to bump up the thread to ask.
    Attached Images Attached Images   
    Last edited by nanoinfinity; Jul 2nd, 2009 at 12:40 PM.

  4. #4
    Lively Member
    Join Date
    Jun 2016
    Posts
    106

    Re: [RESOLVED] [Excel]Is it possible to fill in areas of overlap in an excel graph?

    please sample file workbook excel of this chart.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] [Excel]Is it possible to fill in areas of overlap in an excel graph?

    as this is from 8 years ago, you would be pretty lucky to get a response from the OP
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width