如何在年份之间获取特定的json数据?

问题描述 投票:2回答:3

我有一个带有Year及其值的数据结构json,我将在我的highcharts上显示它。

如何在多年之间过滤它?例如,如果我在2016年至2018年的下拉列表中选择,它将仅显示2016 2017和2018年的数据。

这是我的数据结构

[
{
    "New_Students": "321",
    "NSYEAR": "2014",
    "NSterm": null,
    "NStermCat": null,
    "NSCareer": null,
    "NSProgDescr": null,
    "NSStudent": null
},
{
    "New_Students": "1923",
    "NSYEAR": "2015",
    "NSterm": null,
    "NStermCat": null,
    "NSCareer": null,
    "NSProgDescr": null,
    "NSStudent": null
},
{
    "New_Students": "293",
    "NSYEAR": "2016",
    "NSterm": null,
    "NStermCat": null,
    "NSCareer": null,
    "NSProgDescr": null,
    "NSStudent": null
},
{
    "New_Students": "29",
    "NSYEAR": "2017",
    "NSterm": null,
    "NStermCat": null,
    "NSCareer": null,
    "NSProgDescr": null,
    "NSStudent": null
},
{
    "New_Students": "524",
    "NSYEAR": "2018",
    "NSterm": null,
    "NStermCat": null,
    "NSCareer": null,
    "NSProgDescr": null,
    "NSStudent": null
}
    ]

到目前为止这是我的输出。

enter image description here

这是我获取json数据的代码

 var strCampus = "<%=MyProperty%>";
    var MyUpdateDate = "<%=UpdateDate%>";
    var ParamYear;
    var OnClickYearVal;
    var Year = [];
    var user_data = [];
    //Rdata.ParamYear = ParamYear;

 var drill_down_data = [];
    $(function () {
        $.getJSON('http://localhost:37590/get_NSData/' + strCampus, function (jsonData) { 


            const data = jsonData
            console.log(data);


let categories = [],
  series = [],
  i,
  j;
  var chartSeriesData = [];
  var chartDrilldownData = [];

     for (i = 0; i < data.length; i++) {

          categories[i] = data[i].NSYEAR + '-' + [parseFloat(data[i].NSYEAR) + 1];
          Year = [data[i].NSYEAR]
          series.push({
          name: [+data[i].NSYEAR] + ' School Year',
          data: [{ y: +data[i].New_Students }],

      });

      for (j = 0; j < i; j++) {
        series[i].data.unshift(null);
      }

  }

提前谢谢你的帮助。

javascript json highcharts
3个回答
2
投票

您可以使用setExtremes轴方法将下拉事件与图表范围绑定:

var chart = Highcharts.chart('container', {
    ...,
    xAxis: {
        minRange: 0,
        ...
    }
});

document.getElementById('selectFrom')
    .addEventListener('change', function() {
        chart.xAxis[0].setExtremes(Number(this.value), chart.xAxis[0].max);
    });

document.getElementById('selectTo')
    .addEventListener('change', function() {
        chart.xAxis[0].setExtremes(chart.xAxis[0].min, Number(this.value));
    });

现场演示:http://jsfiddle.net/BlackLabel/xevgsjdo/

API参考:https://api.highcharts.com/class-reference/Highcharts.Axis#setExtremes


1
投票

解析您的JSON数据并使用Array.prototype.filter()

例如:

// allData is your parsed JSON data
const filteredByYear = allData.filter(function(single) {
    if (single["NSYEAR"] >= 2016 && single["NSYEAR"] <= 2018) {
        return single;
    }
});

1
投票

使用filtertypeOf NSYEARstring所以你必须转换成number

    function getFilterDataBetweenYears(fromYear, toYear) {
        return data.filter(obj => {
            if (+obj["NSYEAR"] >= fromYear && +obj["NSYEAR"] <= toYear) {
                return obj;
            }
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.