Skip to content

Commit a20387b

Browse files
committed
change select date code
1 parent 2b34c83 commit a20387b

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

dist/widgets.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/chart_by_time.vue

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,39 +378,67 @@
378378
select.style.height = y4 - y3 + 'px';
379379
}
380380
381-
function getDataPoints(areaBoungingBox, numberOfRows){
381+
function getDataPoints(areaBoungingBox, minDate, maxDate, dateFormat){
382+
let dateKey = 'months';
383+
switch(dateFormat){
384+
case '%Y':
385+
dateKey = 'years';
386+
break;
387+
case '%Y-%m':
388+
break;
389+
case '%Y-%m-%d':
390+
dateKey = 'days';
391+
break;
392+
}
393+
let numberOfRows = getNumberOfRows(minDate, maxDate, dateFormat);
382394
let onePoint = areaBoungingBox.width/numberOfRows;
383395
let points = [];
384396
for (var row = 0; row < numberOfRows; row++){
385-
points[row] = {left: (onePoint*row)+areaBoungingBox.left, width: onePoint}
397+
let min = moment(minDate).add(row, dateKey);
398+
points[row] = {left: (onePoint*row)+areaBoungingBox.left, width: onePoint, date: min, dateFormatted: min.format('YYYY-MM-DD')}
399+
}
400+
return points;
401+
}
402+
403+
function getNumberOfRows(minDate, maxDate, dateFormat){
404+
switch(dateFormat){
405+
case '%Y':
406+
return maxDate.endOf('years').diff(minDate, 'years')+1;
407+
break;
408+
case '%Y-%m':
409+
return maxDate.endOf('months').diff(minDate, 'months')+1;
410+
break;
411+
case '%Y-%m-%d':
412+
default:
413+
return maxDate.endOf('days').diff(minDate, 'days')+1;
386414
}
387-
return points
388415
}
389416
390417
function selectPoints() {
391-
let data = google.visualization.arrayToDataTable(it.prepareChartData(it.context.vm.result, it.options));
418+
let prepareChartData = it.prepareChartData(it.context.vm.result, it.options);
392419
let xx1 = x3 - graph.getBoundingClientRect().left + parseInt(graph.getAttribute('x'));
393420
let xx2 = x4 - graph.getBoundingClientRect().left + parseInt(graph.getAttribute('x'));
394421
let selection = [];
395-
let dataPoints = getDataPoints(chartLayout.getChartAreaBoundingBox(), data.getNumberOfRows());
396-
for (var row = 0; row < data.getNumberOfRows(); row++) {
422+
let dataPoints = getDataPoints(
423+
chartLayout.getChartAreaBoundingBox(),
424+
moment(_.minBy(prepareChartData.slice(1), function(pcd){return pcd[0].v})[0].v),
425+
moment(_.maxBy(prepareChartData.slice(1), function(pcd){return pcd[0].v})[0].v),
426+
it.context.query.variables.dateFormat
427+
);
428+
for (var row = 0; row < dataPoints.length; row++) {
397429
let point = dataPoints[row];
398430
if ((point.left >= (xx1 - point.width)) && ((point.left + point.width) <= (xx2 + point.width))){
399-
selection.push(row);
431+
selection.push(point);
400432
}
401433
}
402434
if(selection.length>0){
403-
// console.log(moment(data.getValue(selection[0], 0)).format('YYYY-MM-DD'));
404-
// console.log(it.setTill(data.getValue(selection[selection.length-1], 0), it.context.query.variables.dateFormat));
405-
// it.context.is_request = true;
406435
if (Array.isArray(it.historyRequests)){
407436
it.historyRequests.push(it.context.query.variables);
408437
}else{
409438
it.historyRequests = [it.context.query.variables];
410439
}
411440
it.hasInnerRequest = true;
412-
it.context.query.request({from: moment(data.getValue(selection[0], 0)).format('YYYY-MM-DD'), till: it.setTill(data.getValue(selection[selection.length-1], 0), it.context.query.variables.dateFormat)}, function (data, his) {
413-
// it.context.is_request = false;
441+
it.context.query.request({from: selection[0].date.format('YYYY-MM-DD'), till: it.setTill(selection[selection.length-1].date, it.context.query.variables.dateFormat)}, function (data, his) {
414442
it.hasInnerRequest = true;
415443
});
416444
}

0 commit comments

Comments
 (0)