superset/caravel/assets/visualizations/big_number.js

197 lines
5.5 KiB
JavaScript
Raw Normal View History

import d3 from 'd3';
import { formatDate } from '../javascripts/modules/dates';
2016-03-18 02:44:58 -04:00
require('./big_number.css');
function bigNumberVis(slice) {
const div = d3.select(slice.selector);
2016-03-18 02:44:58 -04:00
function render() {
d3.json(slice.jsonEndpoint(), function (error, payload) {
// Define the percentage bounds that define color from red to green
2016-03-18 02:44:58 -04:00
if (error !== null) {
slice.error(error.responseText, error);
return;
2016-03-18 02:44:58 -04:00
}
div.html(''); // reset
const fd = payload.form_data;
const json = payload.data;
const f = d3.format(fd.y_axis_format);
const fp = d3.format('+.1%');
const width = slice.width();
const height = slice.height();
const svg = div.append('svg');
svg.attr('width', width);
svg.attr('height', height);
const data = json.data;
let vCompare;
let v;
2016-04-13 20:27:00 -04:00
if (fd.viz_type === 'big_number') {
v = data[data.length - 1][1];
} else {
2016-04-13 20:27:00 -04:00
v = data[0][0];
}
2016-03-18 02:44:58 -04:00
if (json.compare_lag > 0) {
const pos = data.length - (json.compare_lag + 1);
2016-03-18 02:44:58 -04:00
if (pos >= 0) {
vCompare = (v / data[pos][1]) - 1;
2016-03-18 02:44:58 -04:00
}
}
const dateExt = d3.extent(data, (d) => d[0]);
const valueExt = d3.extent(data, (d) => d[1]);
const margin = 20;
const scaleX = d3.time.scale.utc().domain(dateExt).range([margin, width - margin]);
const scaleY = d3.scale.linear().domain(valueExt).range([height - (margin), margin]);
const colorRange = [d3.hsl(0, 1, 0.3), d3.hsl(120, 1, 0.3)];
const scaleColor = d3.scale
.linear().domain([-1, 1])
2016-04-13 20:27:00 -04:00
.interpolate(d3.interpolateHsl)
.range(colorRange)
.clamp(true);
const line = d3.svg.line()
2016-04-13 20:27:00 -04:00
.x(function (d) {
return scaleX(d[0]);
2016-04-13 20:27:00 -04:00
})
.y(function (d) {
return scaleY(d[1]);
2016-04-13 20:27:00 -04:00
})
.interpolate('basis');
2016-03-18 02:44:58 -04:00
let y = height / 2;
let g = svg.append('g');
// Printing big number
g.append('g').attr('class', 'digits')
.attr('opacity', 1)
.append('text')
2016-04-13 20:27:00 -04:00
.attr('x', width / 2)
.attr('y', y)
.attr('class', 'big')
.attr('alignment-baseline', 'middle')
.attr('id', 'bigNumber')
.style('font-weight', 'bold')
.style('cursor', 'pointer')
.text(f(v))
.style('font-size', d3.min([height, width]) / 3.5)
.attr('fill', 'white');
if (fd.viz_type === 'big_number') {
// Drawing trend line
2016-04-13 20:27:00 -04:00
g.append('path')
.attr('d', function () {
2016-03-18 02:44:58 -04:00
return line(data);
})
.attr('stroke-width', 5)
.attr('opacity', 0.5)
.attr('fill', 'none')
.attr('stroke-linecap', 'round')
.attr('stroke', 'grey');
2016-03-18 02:44:58 -04:00
2016-04-13 20:27:00 -04:00
g = svg.append('g')
2016-03-18 02:44:58 -04:00
.attr('class', 'digits')
.attr('opacity', 1);
if (vCompare !== null) {
2016-04-13 20:27:00 -04:00
y = (height / 8) * 3;
}
2016-03-18 02:44:58 -04:00
const c = scaleColor(vCompare);
// Printing big number subheader text
2016-04-13 20:27:00 -04:00
if (json.subheader !== null) {
g.append('text')
.attr('x', width / 2)
.attr('y', y + d3.min([height, width]) / 4.5)
.text(json.subheader)
.attr('id', 'subheader_text')
.style('font-size', d3.min([height, width]) / 16)
.style('text-anchor', 'middle')
.attr('fill', c)
.attr('stroke', c);
2016-04-13 20:27:00 -04:00
}
// Printing compare %
if (vCompare !== null) {
2016-04-13 20:27:00 -04:00
g.append('text')
2016-03-18 02:44:58 -04:00
.attr('x', width / 2)
.attr('y', (height / 16) * 12)
.text(fp(vCompare) + json.compare_suffix)
2016-03-18 02:44:58 -04:00
.style('font-size', d3.min([height, width]) / 8)
.style('text-anchor', 'middle')
.attr('fill', c)
.attr('stroke', c);
2016-04-13 20:27:00 -04:00
}
2016-03-18 02:44:58 -04:00
const gAxis = svg.append('g').attr('class', 'axis').attr('opacity', 0);
g = gAxis.append('g');
const xAxis = d3.svg.axis()
.scale(scaleX)
2016-03-18 02:44:58 -04:00
.orient('bottom')
.ticks(4)
.tickFormat(formatDate);
g.call(xAxis);
2016-04-13 20:27:00 -04:00
g.attr('transform', 'translate(0,' + (height - margin) + ')');
2016-03-18 02:44:58 -04:00
g = gAxis.append('g').attr('transform', 'translate(' + (width - margin) + ',0)');
const yAxis = d3.svg.axis()
.scale(scaleY)
2016-03-18 02:44:58 -04:00
.orient('left')
.tickFormat(d3.format(fd.y_axis_format))
.tickValues(valueExt);
g.call(yAxis);
2016-04-13 20:27:00 -04:00
g.selectAll('text')
2016-03-18 02:44:58 -04:00
.style('text-anchor', 'end')
.attr('y', '-7')
.attr('x', '-4');
g.selectAll('text')
2016-03-18 02:44:58 -04:00
.style('font-size', '10px');
div.on('mouseover', function () {
const el = d3.select(this);
el.selectAll('path')
.transition()
.duration(500)
.attr('opacity', 1)
2016-04-13 20:27:00 -04:00
.style('stroke-width', '2px');
el.selectAll('g.digits')
.transition()
.duration(500)
.attr('opacity', 0.1);
el.selectAll('g.axis')
.transition()
.duration(500)
.attr('opacity', 1);
2016-03-18 02:44:58 -04:00
})
.on('mouseout', function () {
const el = d3.select(this);
el.select('path')
.transition()
.duration(500)
.attr('opacity', 0.5)
2016-04-13 20:27:00 -04:00
.style('stroke-width', '5px');
el.selectAll('g.digits')
.transition()
.duration(500)
.attr('opacity', 1);
el.selectAll('g.axis')
.transition()
.duration(500)
.attr('opacity', 0);
2016-03-18 02:44:58 -04:00
});
2016-04-13 20:27:00 -04:00
}
2016-03-18 02:44:58 -04:00
slice.done(payload);
});
}
return {
render,
resize: render,
2016-03-18 02:44:58 -04:00
};
}
module.exports = bigNumberVis;