简体   繁体   中英

jqPlot Data Doesn't Show on Line Chart

My inexperience with jQuery is flagrant at the moment. I am receiving jSON data with columns "location, YearPostDate, MonthPostDate, and AverageSalary". Locations can be "Local" or "National". I want one line of local and one line of national by YearPostdate-MonthPostdate. ALMOST there, but I know I'm not assembling and present the structures/arrays correctly.

I've run through this many different way. I had it working with plain-jane code, so I know it works, but when I move to actually pulling in the data and generating the structures, I've got something twisted. I have tested the incoming jSON - that's not a problem. It's my jQuery and jqPlot skills that are sorely lacking.

Here's the meat of my code that needs help:

$('#btnTrend').click( function() {
$( '#jobsDisplay' ).hide();
$( '#jobsDisplay' ).empty();
$( '#trendsError' ).hide();
if ( ($( '#rolesTitle' ).val() != null ) && ($( '#rolesLocation' ).val() != null ) ) {
    $('#rates').text("Loading...");
    $('#duration').text("Loading...");
    $('#jobs').text("Loading...");
    $('#trendData').show();
    $.ajax({
        url: 'http://somedomain.com/components/jobsDatabase.cfc',
        type: 'post',
        dataType: 'json',
        data: {
            method: 'getMarketRateTrend',
            role: $('#rolesTitle').val(),
            region_state: $('#rolesLocation').val(),
            returnFormat: 'json'
        },
        success: function(data) {
            var localList = [];
            var nationalList = [];
            var xAxis = [];
            var yValue = 0;
            var maxY = 100;
            var minY = 0;
            var lastValue = '';
            var finalLocal = '';
            var finalNational = '';


                $.each(data, function(i,val){ 
                    /*var xValue = data[i]['YearPostDate']+'-'+data[i]['MonthPostdate'];*/
                    var xValue = data[i]['MonthPostdate'];
                    if (lastValue != xValue) {
                        xAxis.push(xValue);
                        lastValue=xValue;
                    }
                    yValue = data[i]['AverageSalary'];
                    var zLocation = data[i]['location'];
                    if (zLocation=='National') {
                        nationalList.push([xValue,yValue]);
                        if (yValue > maxY) {
                            maxY = yValue;
                        }
                    } else {
                        localList.push([xValue,yValue]);
                        if (yValue > maxY) {
                            maxY = yValue;
                        }
                    }
                });
                yValue = yValue * 1.10;
                $( '#rates' ).empty();
                $.jqplot('rates',  
                    [[localList],
                    [nationalList]],
                    { title:'Market Rates',
                        series:[{color:'#5FAB78'}]
                    });
            },
            error: function(xhr, textStatus,errorThrown){
                alert(errorThrown);
            }
        });

    } else {
        $( '#trendsError' ).show();
    };
});

The data coming in is

Local   2018    3   33.41
National    2018    3   33.23
Local   2018    4   34.09
National    2018    4   32.62
Local   2018    5   32.55
National    2018    5   32.82
Local   2018    6   34.98
National    2018    6   34.08

My result right now is a blank graph with the Market Rates header. I'm looking for a two-line chart to be displayed with one line for local data and one for national data, by month/year, but the chart is empty.

It turns out that I was doing everything right except:

  • Case was returning as all lower, so rather than data[i]['MonthPostDate'] it should have been data[i]['monthpostdate']
  • Needed or not, encasing the data[i][whatever] in the appropriate Number() or String() functions seemed to make it all happier.
  • For more love, I changed yValue to Number(data[i]['averagesalary'].toFixed(2)) since it was a dollar amount.

Hope these small, seemingly insignificant things helps the next person.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM