1.自定義數(shù)組怎么關(guān)聯(lián)數(shù)據(jù)數(shù)組實現(xiàn)同步排序:
2.這是排序前的數(shù)據(jù)數(shù)組:
[null, null, null, 2015-11-23 15:26:57, 2015-11-23 15:3:23, null, null, 2015-11-23 15:16:8, 2015-11-23 15:26:59, null]
按順序?qū)?yīng)自定義字段名數(shù)組:
this.dataNames = ['采樣','送檢','簽收','已登記','IOM','離心','去蓋','測試中','已上傳','已送檢']
3.取數(shù)據(jù)數(shù)組的分鐘給數(shù)據(jù)數(shù)組排序后數(shù)組效果是這樣的:
[0, 0, 0, 0, 0, 0, 3, 16, 26, 26]
4.ECharts中的柱狀圖字段名數(shù)組和數(shù)據(jù)數(shù)組插入只能分開插入:
yAxis: {
type: 'category',
data: this.dataNames,
axisTick: {
alignWithLabel: true,
}
},
series: [
{
name: '2011年',
type: 'bar',
data: this._rowsData
}
]
5.問題:我怎么實現(xiàn)自定義的字段名數(shù)組跟隨數(shù)值數(shù)組同步排序????
這是交互模塊源碼:
// 交互模塊
SampleTime.prototype.getSampleStateBySampleNo = function (){
//直接調(diào)用Procedure存儲過程
var Request = Global.NewMessage();
//console.log(Request)
Request.ProcedureName = "[proc_datalink_getsample_module]";
Request.ParameterNames = ["SampleNo"];
Request.ParameterDataTypes = ["int"];
Request.ParameterValues = [1];
//獲取數(shù)據(jù)
//PostMessage 異步需回調(diào)
var Return = Global.SendMessage("DB_CallProcedure", Request);
var Returns = Return.Table0;
console.log(Returns)
//獲取值
this._rowsData = [];
this._rowsData = Returns._rowsData[0];
var datas = [];
var times = [];
for (var data=0;data<this._rowsData.length;data++){
times.push(this._rowsData[data]);
datas.push(new Date(this._rowsData[data]).getMinutes());
}
//排序,格式化分鐘
var compare = function (x,y) {//比較函數(shù)
if (x < y) {
return -1;
} else if (x > y) {
return 1;
} else {
return 0;
}
}
datas.sort(compare);
this._rowsData = datas;
console.log(this._rowsData)
//完整時間
this._rowsDatas = times;
//自定義字段名
this.dataNames = ['采樣','送檢','簽收','已登記',
'IOM','離心','去蓋','測試中','已上傳','已送檢']
}
SampleTime.prototype.getSampleStateBySampleNo();
6.這是后臺的數(shù)據(jù)格式:
7.這是效果圖,由0開始如果排序正確的話應(yīng)該是3,4,7,8有數(shù)據(jù),自定義字段對應(yīng)的應(yīng)該是:已登記,IOM,測試中,已上傳這幾項有值,但是現(xiàn)在目前貌似所有效果都是值對應(yīng)上了最后4項字段名:
存成對象數(shù)組
[
{
date,
type,
}
]
然后對數(shù)組中的date排序,最后分成兩個數(shù)組
補(bǔ)充一下。實際應(yīng)用還要考慮其他因素,比如后端返回的數(shù)據(jù)情況,數(shù)據(jù)的量級,代碼的可讀性和可維護(hù)性等。
=================
根據(jù)你的代碼做的修改,你中間有些步驟寫的有點冗余我改了下
ES5:
var dateData = this._rowsData = [null, null, null, '2015-11-23 15:26:57', '2015-11-23 15:3:23', null, null, '2015-11-23 15:16:8', '2015-11-23 15:26:59', null]
var typeData = this.dataNames = ['采樣', '送檢', '簽收', '已登記',
'IOM', '離心', '去蓋', '測試中', '已上傳', '已送檢'
]
var result = this.dataNames
.map(function (type, index) { // 拼對象數(shù)組
return {
type,
date: new Date(dateData[index]).getMinutes(),
}
})
.sort(function (a, b) { // 排序
// sort 不穩(wěn)定
if (a.type === null && b.type === null) {
return typeData.indexOf(a.type) - typeData.indexOf(b.type)
}
return new Date(a.date) - new Date(b.date)
})
.reduce(function (result, item) { // 分割對象數(shù)組為兩個數(shù)組
return {
date: result.date.concat(item.date),
type: result.type.concat(item.type),
}
}, { date: [], type: [] })
var date = result.date;
var type = result.type;
console.log(date, type)
// [0, 0, 0, 0, 0, 0, 3, 16, 26, 26]
// ["采樣", "送檢", "簽收", "離心", "去蓋", "已送檢", "IOM", "測試中", "已登記", "已上傳"]
ES6:
const dateData = this._rowsData = [null, null, null, '2015-11-23 15:26:57', '2015-11-23 15:3:23', null, null, '2015-11-23 15:16:8', '2015-11-23 15:26:59', null]
const typeData = this.dataNames = ['采樣', '送檢', '簽收', '已登記',
'IOM', '離心', '去蓋', '測試中', '已上傳', '已送檢'
]
const { date, type } = this.dataNames
.map((type, index) => ({ type, date: new Date(this._rowsData[index]).getMinutes() })) // 拼對象數(shù)組
.sort(({ date: dateA, type: typeA }, { date: dateB, type: typeB }) => // 排序
// sort不穩(wěn)定
(typeA === null && typeB === null)
? typeData.indexOf(typeA) - typeData.indexOf(typeB)
: new Date(dateA) - new Date(dateB))
.reduce((result, { date, type }) => ({ // 分割對象數(shù)組為兩個數(shù)組
date: [...result.date, date],
type: [...result.type, type]
}), { date: [], type: [] })
console.log(date, type)
// [0, 0, 0, 0, 0, 0, 3, 16, 26, 26]
// ["采樣", "送檢", "簽收", "離心", "去蓋", "已送檢", "IOM", "測試中", "已登記", "已上傳"]
謝謝大家,我解決了思路是二維數(shù)組排序,我把我實現(xiàn)代碼貼出來,謝謝幫助我的朋友們:
1.交互模塊:
// 交互模塊
SampleTime.prototype.getSampleStateBySampleNo = function () {
//直接調(diào)用Procedure存儲過程
var Request = Global.NewMessage();
Request.ProcedureName = "[proc_datalink_getsample_module]";
Request.ParameterNames = ["SampleNo"];
Request.ParameterDataTypes = ["int"];
Request.ParameterValues = [1];
//獲取數(shù)據(jù)
//PostMessage 異步需回調(diào)
var Return = Global.SendMessage("DB_CallProcedure", Request);
var Returns = Return.Table0;
console.log(Returns)
// //獲取值
this._rowsData = [];
this._rowsData = Returns._rowsData[0];
var datas = [];
var times = [];
for (var data = 0; data < this._rowsData.length; data++) {
times.push(this._rowsData[data]);
datas.push(new Date(this._rowsData[data]).getMinutes());
}
this._rowsData = datas;
console.log(this._rowsData)
//自定義數(shù)組:["采樣","送檢","簽收","已登記","IOM","離心","去蓋","測試中","已上傳","已送檢"]
//混合數(shù)組
this.dataValues = [
["采樣",this._rowsData[0]],
["送檢",this._rowsData[1]],
["簽收",this._rowsData[2]],
["已登記",this._rowsData[3]],
["IOM",this._rowsData[4]],
["離心",this._rowsData[5]],
["去蓋",this._rowsData[6]],
["測試中",this._rowsData[7]],
["已上傳",this._rowsData[8]],
["已送檢",this._rowsData[9]]
]
console.log(this.dataValues)
//給二維數(shù)組第二列排序
this.dataValues.sort(function(a,b){
return a[1]-b[1];
});
this._values = [];
this._datas = [];
for(var i=0;i<this.dataValues.length;i++){
this._values.push(this.dataValues[i][0]);
this._datas.push(this.dataValues[i][1]);
}
2.ECharts插入代碼:
yAxis: {
type: 'category',
data: this._values ,
axisTick: {
alignWithLabel: true,
}
},
series: [
{
name: '2011年',
type: 'bar',
data: this._datas,
}
var dateData1 = [null, null, null, '2015-11-23 15:26:57', '2015-11-23 15:3:23', null, null, '2015-11-23 15:16:8', '2015-11-23 15:26:59', null]
var typeData1 = ['采樣', '送檢', '簽收', '已登記',
'IOM', '離心', '去蓋', '測試中', '已上傳', '已送檢'
];
var dateData1IndexObj=new Array(dateData1.length);
var resultData={
date:[],
type:[]
};
dateData1.forEach(function(item,index){
dateData1IndexObj[index]={
minutes:item?new Date(item).getMinutes():0,
index:index
};
});
dateData1IndexObj.sort(function(itemA,itemB){
if(itemA.minutes<itemB.minutes){
return -1;
}else if(itemA.minutes>itemB.minutes){
return 1;
}else{
return 0;
}
});
dateData1IndexObj.forEach(function(item,index){
console.log(typeData1[item.index]);
resultData.date.push(item.minutes);
resultData.type.push(typeData1[item.index]);
});
console.log(resultData);
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。