SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CanvasJS Pie Chart - Preparing a JS Array to Hold Data

    I can successfully render a canvasJS pie chart using static data hard-coded within a js array like this:

    Code:
    // Hardcoded data - this will successfully render a pie chart
    var dps = [{ y: 1, indexLabel: "Writing" }, { y: 0, indexLabel: "External Collaboration" }, { y: 0, indexLabel: "Ready for Edit" }, { y: 0, indexLabel: "Editing" }, { y: 0, indexLabel: "Ready for Review" }, { y: 0, indexLabel: "In Pink Team" }, { y: 0, indexLabel: "In Gold Team" }, { y: 1, indexLabel: "In Red Team" }, { y: 0, indexLabel: "Final Versions" }]
    However, I need to simply reference an array already populated. I am populating an array from a table on the page. I canít understand why it wonít work because when I convert the array to string and print, everything is formatted correctly.

    Below is the link to my jsfiddle example code.

    http://jsfiddle.net/cgtrman/J4YLV/10/

    Any help is greatly appreciated.

  2. #2
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Understand JavaScript types: '{ y: 10, indexLabel: "Writing" }' does not equal { y: 10, indexLabel: "Writing" }. The first one is a string, the second one is an object.

    http://jsfiddle.net/J4YLV/49/

    You are not building the array the right way. You are putting text elements in it. You should put objects in it. You only need to use text concatenation for the key+(value) part.

    dsp is an array of objects now: dps.push({ y: Number(value), indexLabel: key + '(' + value + ')'});
    Before it was an array of arrays of strings.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by myty View Post
    Understand JavaScript types: '{ y: 10, indexLabel: "Writing" }' does not equal { y: 10, indexLabel: "Writing" }. The first one is a string, the second one is an object.

    http://jsfiddle.net/J4YLV/49/

    You are not building the array the right way. You are putting text elements in it. You should put objects in it. You only need to use text concatenation for the key+(value) part.

    dsp is an array of objects now: dps.push({ y: Number(value), indexLabel: key + '(' + value + ')'});
    Before it was an array of arrays of strings.
    That was the issue. Thanks for providing a quick response and solution!


Tags for this Thread

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
  •