Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How do I make my array accessible in my code

    Hello developers

    I need help to make a variable accessible by my other code.

    I have an array named arrYears with this value: 2007,2008,2009,2010,2011,2012,2013

    I want to use this array to set values in the row "Years" below:
    This is what I want to achieve.
    Code:
      "data": [
        ["Years",2007,2008,2009,2010,2011,2012,2013],
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]
    My code looks like:

    Code:
      "data": [
        ["Years",arrYears],
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]
    What happens is that arrYears is treated like one string and that is what I want help with. How can I get my array to work?

    If I use this code It works but that is not very useful to me ofcourse.

    Code:
      "data": [
        ["Years",arrYears[0],arrYears[1],arrYears[2]...],
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]
    I hope my question make sense to somebody that can help me out.

    Regards,
    ZnappHane

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    var arrYears = [ 2007,2008,2009,2010,2011,2012,2013 ],
    
    myObj =
    {
     "data": [
        ["Years"],
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]
    };
    
    myObj[ 'data' ][ 0 ].push( arrYears );
      
    alert( myObj[ 'data' ][ 0 ] )
    If myObj[ 'data' ][ 0 ] contains other elements that you want to replace with arrYears, then they must be spliced-out first.

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey Logic Ali and thank you for your quick response.

    I´m not sure how to apply you sollution to my code. I post all code here so you can see it. I only posted the code I thought was relevant but maybe it´s not.

    Code:
    	
    var chartdata = {
    
    "config": {
        "title": "Title",
        "subTitle": "Sub Title",
        "type": "line",
    	"width" : "900",
      },
    
      "data": [
        ["Years", arrYears],
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]	  
    	};
    		
    	ccchart.init("survey_dashboard", chartdata)
    	
    }
    You have it all at http://ccchart.com/#47

    Regards,
    ZnappHane

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    49
    Thanks
    0
    Thanked 13 Times in 12 Posts
    Code:
     "data": [
        ["Years"].concat(arrYears),
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]

  • The Following 2 Users Say Thank You to Goos For This Useful Post:

    Toshiro Takahas (07-13-2013), ZnappHane (07-02-2013)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    But I think that the entire approach is wrong.

    I think it should be
    Code:
    var chartdata = {
        config: {
            title    : "Title",
            subTitle : "Sub Title",
            type     : "line",
    	width    : "900"
        },
        data : {
            Years : arrYears,
            John : [60,435,456,352,567,678,1260],
            Vera : [200,123,312,200,402,300,512]
        }
    };
    But I realize that you are stuck with what ccchart gives you. Not your fault. Their bad design.

    Anyway, given that you nust follow their bad design, Goos is surely correct.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, this did it for me. Thank you very much. I did put some time to this.

    Regards,
    ZnappHane

    Quote Originally Posted by Goos View Post
    Code:
     "data": [
        ["Years"].concat(arrYears),
        ["John",60,435,456,352,567,678,1260],
        ["Vera",200,123,312,200,402,300,512]
      ]

  • #7
    New Coder
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    75
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Old P -

    i just posted a thread very much along the lines of your response to ZnappHane.
    i just can't grasp the structure.

    Code:
    var chartdata = {
        config: {
            title    : "Title",
            subTitle : "Sub Title",
            type     : "line",
    	width    : "900"
        },
        data : {
            Years : arrYears,
            John : [60,435,456,352,567,678,1260],
            Vera : [200,123,312,200,402,300,512]
        }
    };
    i get that chartdata is a variable, but is 'config' one too ?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    No, config is a *PROPERTY* of the object REFERENCED by the chartdata variable.

    As is data. And the other names are properties of the sub-objects named by config and data.

    That is:
    Code:
    alert( chartdata.config.width ); // will alert 900
    alert( chartdata.data.John[2] ); // will alert 456
    And as a not so minor point: Remember, chartdata is *NOT* an object! chartdata references an object.

    You could do:
    Code:
    var foo = chartdata;
    foo.config.width = 750;
    alert( chartdata.config.width ); // will alert 750 !!
    Emphasizing that there is only one object with two (in this case, could be many!) references to it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    Jul 2013
    Location
    Japan
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi, i'm Auther of ccchaert.

    The data that is not guarantee of the order is not useful in many cases of the charts.
    Object of JavaScript is not guaranteed the order, unlike the Array.
    In addition, the speed is faster than the Object is often Array.
    Therefore, ccchart is using an Array in data.

    regards

  • #10
    New to the CF scene
    Join Date
    Jul 2013
    Location
    Japan
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was wrong registration name ^ ^;
    My name is Toshiro Takahashi.

  • #11
    New to the CF scene
    Join Date
    Jul 2013
    Location
    Japan
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry a lot of mistakes of my english.

    Oh, sorry! it's full of mistakes. lol
    Google translate is not really translate well. and my mistakes.

    Hi, i'm Auther of ccchaert.
    ccchart

    In addition, the speed is faster than the Object is often Array.
    Often, Array, faster than the Object.

    i can't use English well.

  • #12
    New to the CF scene
    Join Date
    Jul 2013
    Location
    Japan
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    for your information
    Speed ​​comparison: oj vs array

    http://ccchart.com/test/arry-vs-oj/test.htm

    By the way, in this case, i think ["Years"].concat(arrYears) is good.

  • #13
    New to the CF scene
    Join Date
    Jul 2013
    Location
    Japan
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    ccchart.util.cnfExtend is deep copy method for ccchart

    Quote Originally Posted by Old Pedant View Post
    And as a not so minor point: Remember, chartdata is *NOT* an object! chartdata references an object.

    You could do:
    Code:
    var foo = chartdata;
    foo.config.width = 750;
    alert( chartdata.config.width ); // will alert 750 !!
    Emphasizing that there is only one object with two (in this case, could be many!) references to it.
    If you want use deep copy, you can use ccchart.util.cnfExtend(baseCnf, newCnf).

    This method is used on ccchart.base Method.

    For Example

    http://ccchart.com/test/base/base-v1.06-b7.htm

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    Quote Originally Posted by Toshiro Takahas View Post
    Object of JavaScript is not guaranteed the order, unlike the Array.
    Really? I have never seen any evidence of that. If you can show an example where order is changed by JavaScript, I would really like to see it.

    And I doubt that the difference in speed is more than a few microseconds, if that much.

    But never mind, you are of course entitled to make your own decisions in your own designs. And welcome to the forums.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    New to the CF scene
    Join Date
    Jul 2013
    Location
    Japan
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Really? I have never seen any evidence of that. If you can show an example where order is changed by JavaScript, I would really like to see it.
    I previously has been seen in some browsers.

    ECMA-262 3rd Edition that is the most popular specification
    of JavaScript

    8.6 The Object Type
    An Object is an *unordered* collection of properties. Each property consists of a name, a value and a set of attributes.
    I want to know. Do you know that is guaranteed in the 5th Edition?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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