From 4e6aa4c5b5b6c8a88567978e088eec22f451add5 Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Sat, 31 Oct 2015 15:32:49 -0700 Subject: [PATCH] Improvments around date handling for nvd3 charts --- panoramix/static/widgets/viz_nvd3.js | 34 ++++++++++++++++------------ panoramix/viz.py | 2 +- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/panoramix/static/widgets/viz_nvd3.js b/panoramix/static/widgets/viz_nvd3.js index db2b4110a0..29ec245ad8 100644 --- a/panoramix/static/widgets/viz_nvd3.js +++ b/panoramix/static/widgets/viz_nvd3.js @@ -15,6 +15,11 @@ function viz_nvd3(data_attribute) { ["%B", function(d) { return d.getMonth(); }], ["%Y", function() { return true; }] ]); + function formatDate(dttm) { + var d = UTC(new Date(dttm)); + //d = new Date(d.getTime() - 1 * 60 * 60 * 1000); + return tickMultiFormat(d); + } colors = [ "#FF5A5F", "#007A87", "#7B0051", "#00D1C1", "#8CE071", "#FFB400", "#FFAA91", "#B4A76C", "#9CA299", "#565A5C" @@ -35,20 +40,21 @@ function viz_nvd3(data_attribute) { if (viz_type === 'line') { if (viz.form_data.show_brush) { var chart = nv.models.lineWithFocusChart() - var xext = chart.xAxis.scale().domain(); - chart - .x2Axis - .tickFormat(function (d) {return tickMultiFormat(UTC(new Date(d))); }) - .tickValues([]); - chart.y2Axis.tickFormat(d3.format('.3s')); + //chart.lines2.xScale( d3.time.scale.utc()); + chart.lines2.xScale(d3.time.scale.utc()); + chart.x2Axis + .showMaxMin(true) + .tickFormat(formatDate); } else { var chart = nv.models.lineChart() } + // To alter the tooltip header + // chart.interactiveLayer.tooltip.headerFormatter(function(){return '';}); + chart.xScale(d3.time.scale.utc()); chart.interpolate(viz.form_data.line_interpolation); - chart.xScale(d3.time.scale()); chart.xAxis - .showMaxMin(false) - .tickFormat(function (d) {return tickMultiFormat(new Date(d)); }); + .showMaxMin(true) + .tickFormat(formatDate); chart.showLegend(viz.form_data.show_legend); chart.yAxis.tickFormat(d3.format('.3s')); if (viz.form_data.contribution || viz.form_data.num_period_compare) { @@ -63,7 +69,7 @@ function viz_nvd3(data_attribute) { .groupSpacing(0.1); chart.xAxis .showMaxMin(false) - .tickFormat(function (d) {return tickMultiFormat(UTC(new Date(d))); }); + .tickFormat(formatDate); chart.showLegend(viz.form_data.show_legend); chart.yAxis.tickFormat(d3.format('.3s')); @@ -95,10 +101,10 @@ function viz_nvd3(data_attribute) { } else if (viz_type === 'compare') { var chart = nv.models.cumulativeLineChart(); - chart.xScale(d3.time.scale()); + chart.xScale(d3.time.scale.utc()); chart.xAxis .showMaxMin(false) - .tickFormat(function (d) {return tickMultiFormat(new Date(d)); }); + .tickFormat(formatDate); chart.showLegend(viz.form_data.show_legend); chart.yAxis.tickFormat(d3.format('.3p')); @@ -111,10 +117,10 @@ function viz_nvd3(data_attribute) { } else if (viz_type === 'area') { var chart = nv.models.stackedAreaChart(); - chart.xScale(d3.time.scale()); + chart.xScale(d3.time.scale.utc()); chart.xAxis .showMaxMin(false) - .tickFormat(function (d) {return tickMultiFormat(new Date(d)); }); + .tickFormat(formatDate); chart.showLegend(viz.form_data.show_legend); chart.yAxis.tickFormat(d3.format('.3s')); } diff --git a/panoramix/viz.py b/panoramix/viz.py index 3feb0408e0..f21d2d7ad2 100644 --- a/panoramix/viz.py +++ b/panoramix/viz.py @@ -110,7 +110,7 @@ class BaseViz(object): raise Exception("No data, review your incantations!") else: if 'timestamp' in df.columns: - df.timestamp = pd.to_datetime(df.timestamp) + df.timestamp = pd.to_datetime(df.timestamp, utc=False) if self.datasource.offset: df.timestamp += timedelta(hours=self.datasource.offset) return df