1

I am making arrays out of columns in a table that I use to graph Flot charts. I start by working my way down the column and storing each column as its own array, then I check the column to the right and repeat.

Everything has been working great, except for this one tiny issue I am having where I am reading out the string values from the last column and parsing them into floats. All the values in last column parse to floats except for a few. Sometimes it's the 0th and 2nd element in the array, or sometimes the 0th and 1st.

Here's what it looks like when I console.log the arrays:

Here's what it looks like when I <code>console.log</code> the arrays:

The table also known as #maintable:

enter image description here

As you can see in the last array the 0th and 2nd values in the array are returned as strings, but the rest are returned as floats.

Here is the code:

HTML:

<div id="chart_container" class="widget-body col-md-12" style="height: 453px;">
    <section id="mainchart" class="col-md-12" style="height: 453px;"></section>
</div>

JS:

var slices = [];
var sliceNames, sliceNights, sliceNightsAvg, sliceRevs = [];
var sliceSet = [];
var colorPalette = [];



//Get number of columns by counting number of cells in the header row
var numOfColumns = $("#maintable tr th").length;

function getCellData(array, columnNum) {
    $("#maintable tr td:nth-child(" + columnNum + ")").each(function (k, v) {
        if(!array) {
            array = [];
        }

        //Store the slice labels in array
        array[k] = $(this).text();

        //Remove any long whitespace from cell
        array[k] = array[k].trim();



        //Check if the data in the cell is a number or not
        if (parseFloat(array[k]) % 1 === 0) {
            //Filter the values

            array[k] = array[k].replace("$", "");
            array[k] = array[k].replace(",", "");
            array[k] = array[k].replace("K", "000");
            array[k] = array[k].replace("M", "000000");

            //If there's a decimal
            /*if (array[k].indexOf(".") !== -1) {
                //Then parse as a float
                array[k] = parseFloat(array[k]);
            } else {
                //Otherwise, parse as an integer
                array[k] = parseInt(array[k]);
            }*/

            array[k] = parseFloat(array[k]);
        }
    });

    return array;
}

//Get cell data
sliceNames = getCellData(sliceNames, 1);
sliceNights = getCellData(sliceNights, 2);
sliceNightsAvg = getCellData(sliceNightsAvg, 3);
sliceRevs = getCellData(sliceRevs, 4);

console.log(sliceNames);
console.log(sliceNights);
console.log(sliceNightsAvg);
console.log(sliceRevs);

//Loop through and create the slices
for (var i = 0; i < sliceNames.length; i++) {
    if (!sliceSet) {
        sliceSet = [];
    }

    //Setup the slices
    var slice = {};
    slice.label = sliceNames[i];
    slice.data = sliceNights[i];

    //Push the slice to the set of slices
    sliceSet.push(slice);
}

//Plot the Pie chart
plotPieChart(sliceSet);


function plotPieChart(data) {
    $.plot($("#mainchart"), data, {
        series: {
            pie: {
                show: true,
                innerRadius: 0.3,
                label: {
                    show: true,
                    threshold: 0.01
                }
            },
            grid: {
                hoverable: true,
                clickable: true
            },
            legend: {
                show: true
            }
        }
    }); 
}
6
  • 1
    parseFloat(array[k]) % 1 === 0 checks if the number is an integer. It will be false for numbers that have a fractional part. Is that really what you want? Commented Dec 22, 2014 at 17:58
  • I would use isNaN() to check if it is a number. Commented Dec 22, 2014 at 17:59
  • @Juhana This is true. But I am still a little confused how it's worked for the other ones? Commented Dec 22, 2014 at 18:00
  • Consider using Number(...) instead of parseInt/parseFloat; it has fewer edge cases. Commented Dec 22, 2014 at 18:02
  • Juhana is right about parseFloat(array[k]) % 1 === 0 returning false for non-integers, I was going to include that in a formal answer, but how are any floats coming in as the correct type at all? Unless the source value sometime comes in as a number already, there is another issue in addition to that one. Commented Dec 22, 2014 at 18:03

2 Answers 2

1

Some time there is an invisible character in a string, please remove this character. Please check length of the string, if length is greater than visible characters then invisible character is existed. this character is not ASCII character so I am unable to write here. The character code is "u+200E".

Sign up to request clarification or add additional context in comments.

Comments

0

I was not able to duplicate your results exactly(some numbers coming correctly but others not), but I am very confidant that switching parseFloat(array[k]) % 1 === 0 to !isNaN(array[k]) or similar will fix your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.