CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Chart collection already has a chartarea problem

    Hi, I'm having a little doubt about this new error I got.

    I have a class that has a chart object as part of it. When the user adds data, chartareas are created in the class chart according to categories and points added to it.

    I now encountered the problem that when I add a second instance of the class, I get an error stating that there already exists a chartarea with the name "x".

    Are chartarea names searched globally??

    Example:
    Code:
    Class.Pet dogs = new Pet();
    Class.Pet cats = new Pet();
    
    dogs.chart1.ChartAreas.Add(new ChartArea("Population"));
    cats.chart1.ChartAreas.Add(new ChartArea("Population")); --> won't work
    But dogs and cats are two different objects! Evidently the name "Population" is registered globally... can I avoid that somehow? Else I would have to start indexing chartareas and that would bug me a lot =)

    Any ideas? Thanks a lot!

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Chart collection already has a chartarea problem

    Obviously MSChart controls use the chart name as a key and demand that they are unique. You aren't going about this correctly though, so you just need to switch up your approach.

    You don't want multiple ChartAreas; you want multiple plots inside of a single area. So, instead of adding new areas, add new Series object(s) to the ChartArea.Series collection.

  3. #3
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Chart collection already has a chartarea problem

    BigEd, thank you for the reply. No intention to offend, but I know what I want, and I do need different chartareas. Perhaps my example was a bit vague... here goes another one.

    I have to provide the user with narrow scaled graphs of percentages of a total. If I do that in just one chart area I could use multiple Axis for them, but it would be confusing since each value will have alarm lines and they would make reading the data difficult. If I want to observe a value of 20 +/- 0.5 and another value of 45 +/- 0.5 I can't have them together.

    Nevertheless, the problem is not the multiple charting, but the unique naming.

    To make the first example more complete, lets say I have 2 houses, with 100% pet population in each one. For each house I will a have a chart with one Dogs chartarea with the percentage of the population and one Cats chartarea with the same for Cats. For the second house I will have a new chart, with again 2 chartareas.
    Even if I did merge both population series in just one "Population" chartarea for the 1st house, I still would not be allowed to name the 2nd house's chartarea "Population". And I can't merge both house's graphics, so I cannot escape having at least two charts.

    Anyway, it wasn't so much a problem as just an inquiry. I'll just name them "PopulationX" according to the house they're in.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Chart collection already has a chartarea problem

    Ok, well (if I remember correctly) the key does not necessarily need to be displayed to the user, so using a different one as you suggest should suffice.

Tags for this Thread

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

Featured