简体   繁体   English

jqPlot数据未显示在折线图上

[英]jqPlot Data Doesn't Show on Line Chart

My inexperience with jQuery is flagrant at the moment. 目前,我对jQuery缺乏经验是很明显的。 I am receiving jSON data with columns "location, YearPostDate, MonthPostDate, and AverageSalary". 我正在接收带有“位置,YearPostDate,MonthPostDate和AverageSalary”列的jSON数据。 Locations can be "Local" or "National". 位置可以是“本地”或“国家”。 I want one line of local and one line of national by YearPostdate-MonthPostdate. 我想要在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. 我已经测试了传入的jSON-这不是问题。 It's my jQuery and jqPlot skills that are sorely lacking. 严重缺乏我的jQuery和jqPlot技能。

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. 我现在的结果是带有Market Rates标头的空白图表。 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'] 案例返回的全部较低,因此应该是data [i] ['monthpostdate'],而不是data [i] ['MonthPostDate']
  • Needed or not, encasing the data[i][whatever] in the appropriate Number() or String() functions seemed to make it all happier. 不管是否需要,将数据[i] [无论什么]封装在适当的Number()或String()函数中似乎都使它变得更快乐。
  • For more love, I changed yValue to Number(data[i]['averagesalary'].toFixed(2)) since it was a dollar amount. 为了获得更多的爱,我将yValue更改为Number(data [i] ['averagesalary']。toFixed(2)),因为它是美元金额。

Hope these small, seemingly insignificant things helps the next person. 希望这些细微的,看似微不足道的事情对下一个人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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