Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve time shift #5140

Merged
merged 8 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('nvd3 viz', () => {
expect(formatLabel(['foo'], verboseMap)).to.equal('Foo');
expect(formatLabel(['foo', 'bar', 'baz'], verboseMap)).to.equal('Foo, Bar, baz');
});
it('deals with --- properly', () => {
expect(formatLabel(['foo', '---'], verboseMap)).to.equal('Foo ---');
expect(formatLabel(['foo', 'bar', 'baz', '---'], verboseMap)).to.equal('Foo, Bar, baz ---');
it('deals with time shift properly', () => {
expect(formatLabel(['foo', '1 hour offset'], verboseMap)).to.equal('Foo, 1 hour offset');
expect(formatLabel(['foo', 'bar', 'baz', '2 hours offset'], verboseMap)).to.equal('Foo, Bar, baz, 2 hours offset');
});
});
});
3 changes: 2 additions & 1 deletion superset/assets/src/modules/colors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import d3 from 'd3';
import { TIME_SHIFT_PATTERN } from '../utils/common';

export const brandColor = '#00A699';
export const colorPrimary = { r: 0, g: 122, b: 135, a: 1 };
Expand Down Expand Up @@ -181,7 +182,7 @@ export const getColorFromScheme = (function () {
const selectedScheme = scheme ? ALL_COLOR_SCHEMES[scheme] : ALL_COLOR_SCHEMES.bnbColors;
let stringifyS = String(s).toLowerCase();
// next line is for superset series that should have the same color
stringifyS = stringifyS.replace(' ---', '');
stringifyS = stringifyS.split(', ').filter(k => !TIME_SHIFT_PATTERN.test(k)).join(', ');

if (forcedColor && !forcedColors[stringifyS]) {
forcedColors[stringifyS] = forcedColor;
Expand Down
3 changes: 3 additions & 0 deletions superset/assets/src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const DEFAULT_LONGITUDE = -122.405293;
export const DEFAULT_LATITUDE = 37.772123;
export const DEFAULT_ZOOM = 11;

// Regexp for the label added to time shifted series (1 hour offset, 2 days offset, etc.)
export const TIME_SHIFT_PATTERN = /\d+ \w+ offset/;

export function kmToPixels(kilometers, latitude, zoomLevel) {
// Algorithm from: http://wiki.openstreetmap.org/wiki/Zoom_levels
const latitudeRad = latitude * (Math.PI / 180);
Expand Down
30 changes: 30 additions & 0 deletions superset/assets/src/visualizations/nvd3_vis.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ g.opacityHigh path, line.opacityHigh {
stroke-opacity: .8
}

g.time-shift-0 path, line.time-shift-0 {
stroke-dasharray: 5, 5;
}
g.time-shift-1 path, line.time-shift-1 {
stroke-dasharray: 1, 5;
}
g.time-shift-2 path, line.time-shift-2 {
stroke-dasharray: 10, 5;
}
g.time-shift-3 path, line.time-shift-3 {
stroke-dasharray: 5, 1;
}
g.time-shift-4 path, line.time-shift-4 {
stroke-dasharray: 5, 10;
}
g.time-shift-5 path, line.time-shift-5 {
stroke-dasharray: 0.9;
}
g.time-shift-6 path, line.time-shift-6 {
stroke-dasharray: 15, 10, 5;
}
g.time-shift-7 path, line.time-shift-7 {
stroke-dasharray: 15, 10, 5, 10;
}
g.time-shift-8 path, line.time-shift-8 {
stroke-dasharray: 15, 10, 5, 10, 15;
}
g.time-shift-9 path, line.time-shift-9 {
stroke-dasharray: 5, 5, 1, 5;
}
7 changes: 2 additions & 5 deletions superset/assets/src/visualizations/nvd3_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import AnnotationTypes, {
} from '../modules/AnnotationTypes';
import { customizeToolTip, d3TimeFormatPreset, d3FormatPreset, tryNumify } from '../modules/utils';
import { formatDateVerbose } from '../modules/dates';
import { isTruthy } from '../utils/common';
import { isTruthy, TIME_SHIFT_PATTERN } from '../utils/common';
import { t } from '../locales';

// CSS
Expand Down Expand Up @@ -103,11 +103,8 @@ export function formatLabel(input, verboseMap = {}) {
const verboseLkp = s => verboseMap[s] || s;
let label;
if (Array.isArray(input) && input.length) {
const verboseLabels = input.filter(s => s !== '---').map(verboseLkp);
const verboseLabels = input.map(l => TIME_SHIFT_PATTERN.test(l) ? l : verboseLkp(l));
label = verboseLabels.join(', ');
if (input.length > verboseLabels.length) {
label += ' ---';
}
} else {
label = verboseLkp(input);
}
Expand Down
6 changes: 5 additions & 1 deletion superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,8 @@ def run_extra_queries(self):
if not isinstance(time_compare, list):
time_compare = [time_compare]

classes = ['time-shift-{}'.format(i) for i in range(10)]
i = 0
for option in time_compare:
query_object = self.query_obj()
delta = utils.parse_human_timedelta(option)
Expand All @@ -1223,11 +1225,13 @@ def run_extra_queries(self):

df2 = self.get_df_payload(query_object).get('df')
if df2 is not None:
classed = classes[i % len(classes)]
i += 1
label = '{} offset'. format(option)
df2[DTTM_ALIAS] += delta
df2 = self.process_data(df2)
self._extra_chart_data.extend(self.to_series(
df2, classed='superset', title_suffix=label))
df2, classed=classed, title_suffix=label))

def get_data(self, df):
df = self.process_data(df)
Expand Down