Skip to content

Commit

Permalink
Merge branch 'master' of github.com:novus/nvd3
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidpele committed Jun 30, 2018
2 parents 28ed858 + 254bb45 commit 447cce8
Show file tree
Hide file tree
Showing 15 changed files with 1,746 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"always"
],
"prefer-arrow-callback": [
"error",
"never",
{
"allowNamedFunctions": true
}
Expand Down
12 changes: 8 additions & 4 deletions GruntFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ module.exports = function(grunt) {
karma: {
unit: {
options: {
logLevel: 'ERROR',
logLevel: 'DEBUG',
browsers: ['Firefox'],
browserNoActivityTimeout: 60000,
browserDisconnectTimeout: 60000,
captureTimeout: 60000,
frameworks: [ 'mocha', 'sinon-chai' ],
reporters: [ 'spec', 'junit', 'coverage'],
singleRun: true,
Expand All @@ -130,10 +133,12 @@ module.exports = function(grunt) {
},
files: [
'bower_components/d3/d3.js',
'node_modules/moment/moment.js',
'src/*.js',
'src/models/*.js',
'test/mocha/*.coffee',
'https://cdn.rawgit.com/Kcnarf/d3-beeswarm/fbda9b54/build/d3-beeswarm.min.js',
'https://cdn.rawgit.com/Kcnarf/d3-beeswarm/fbda9b54/build/d3-beeswarm.min.js',
'test/mocha/*.js'
],
exclude: [
'src/intro.js',
Expand All @@ -145,8 +150,7 @@ module.exports = function(grunt) {
'src/models/indented*',
'src/models/linePlus*',
'src/models/ohlcBar.js',
'src/models/candlestickBar.js',
'src/models/multiChart.js'
'src/models/candlestickBar.js'
]
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/actual.json

Large diffs are not rendered by default.

177 changes: 177 additions & 0 deletions examples/differenceChart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" />
<link href="../build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.js"></script>
<script src="../build/nv.d3.js?dsf"></script>

<style>
text {
font: 12px sans-serif;
}
svg {
display: block;
}
html, body, svg {
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
}
</style>
</head>
<body class='with-3d-shadow with-transitions'>
<script>
var expandLegend = function() {
var exp = chart.multiChart.legend.expanded();
chart.multiChart.legend.align(false);
chart.update();
}
</script>
<svg id="chart2"></svg>
<div style="height: 1000px; background-color: red;">

</div>
<script>
'use strict';

// Use the browser's built-in functionality to quickly and safely escape
// the string
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}

var chart;

function readTextFile(file) {
return new Promise(function (resolve) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType('application/json');
rawFile.open('GET', file, true);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4 && rawFile.status == '200') {
resolve(rawFile.responseText);
}
};
rawFile.send(null);
});
}

var keyForActualLessThanPredicted = 'Savings (kWh)';
var keyForActualGreaterThanPredicted = 'Excess Usage (kWh)';

function loadData() {
return Promise.all([readTextFile('./actual.json'), readTextFile('./predicted.json')]).then(function (jsonResult) {
return new Promise(function (resolve, reject) {
var actualData = JSON.parse(jsonResult[0]);
var predictedData = JSON.parse(jsonResult[1]);

resolve([{
key: 'Actual usage (kWh)',
type: 'actual',
values: actualData[0].values,
color: 'black'
}, {
key: 'Predicted usage (kWh)',
type: 'expected',
values: predictedData.map(function (d) {
return {
recorded_at: d.falling_edge,
value: d.predicted
};
}),
color: 'blue'
}]);
});
});
}

function main() {
var colors = d3.scale.category20();
var lang = d3.locale({
"dateTime": '%H:%M %A %e %B, %Y',
"date": '%Y-%m-%d',
"time": "%H:%M:%S",
"periods": ['am', 'pm'],
"days": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"shortDays": ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
"months": ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
"shortMonths": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
});

var tickFormatMaxMin = lang.timeFormat.multi([["%b %d", function (d) {
return true;
}]]);
var tickFormat = lang.timeFormat.multi([[":%L", function (d) {
return d.getMilliseconds();
}], ["%S", function (d) {
return d.getSeconds();
}], ["%H:%M", function (d) {
return d.getMinutes();
}], ["%H:%M", function (d) {
return d.getHours();
}], ["%d/%m", function (d) {
return d.getDay() && d.getDate() != 1;
}], ["%b %d", function (d) {
return d.getDate() != 1;
}], ["%b", function (d) {
return d.getMonth();
}], ["%Y", function () {
return true;
}]]);

loadData().then(function (data) {
nv.addGraph(function () {
chart = nv.models.differenceChart();
chart.keyForXValue('recorded_at').x(function (d) {
return new Date(d.recorded_at);
}).y(function (d) {
return d.value;
}).keyForActualLessThanPredicted(keyForActualLessThanPredicted).keyForActualGreaterThanPredicted(keyForActualGreaterThanPredicted).height(500).showPredictedLine(true).interpolate('step-before').tickFormat(tickFormat).strokeWidth(0.5);

chart.multiChart.interactiveLayer.tooltip.contentGenerator(function (d) {
var header = d.value;
var headerhtml = "<thead><tr><td colspan='3'><strong class='x-value'>" + header + "</strong></td></tr></thead>";
var bodyhtml = "<tbody>";
var series = d.series;
series.forEach(function (c) {
var value = (c.value || 0).toFixed(2);
if (c.key === keyForActualGreaterThanPredicted || c.key === keyForActualLessThanPredicted) {
var diff = Math.abs(c.data.y0 - c.data.y1);
if (diff === 0) {
value = '-';
} else {
value = diff.toFixed(2);
}
}
bodyhtml = bodyhtml + "<tr><td class='legend-color-guide'><div style='background-color: " + escapeHtml(c.color) + ";'></div></td><td class='key'>" + escapeHtml(c.key) + "</td><td class='value'>" + escapeHtml(value) + "</td></tr>";
});
bodyhtml = bodyhtml + "</tbody>";
return "<table>" + headerhtml + '' + bodyhtml + "</table>";
});

chart.xAxis.tickFormatMaxMin(tickFormatMaxMin);
chart.focus.xAxis.tickFormatMaxMin(tickFormatMaxMin);

chart.focus.height(40);
chart.focusMargin({ top: 10 });

chart.margin({ top: 60 });

d3.select('svg#chart2').datum(data).call(chart);

nv.utils.windowResize(chart.update);
return chart;
});
});
}

main();
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions examples/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,11 @@ <h3 class="option"><span class="expand all">[ <a href="javascript:$('#optionlist
desc: "Whether to show the grid lines surrounding cells.",
default: "false",
},
cornerRadius: {
desc: "This sets the corner radius (in pixels) of each bar in a discrete bar chart",
default: 0,
examples: [1, 2, 3],
},
};
</script>

Expand Down
1 change: 1 addition & 0 deletions examples/predicted.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"d3": "^3.4.4"
},
"devDependencies": {
"es6-promise": "^4.0.3",
"autoprefixer": "^6.5.0",
"grunt-cli": "^1.2.0",
"es6-promise": "^4.0.3",
"grunt": "^0.4.5",
"grunt-cli": "^1.2.0",
"grunt-contrib-concat": "~1.0.1",
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-cssmin": "~0.13.0",
Expand All @@ -46,6 +46,7 @@
"karma-mocha": "^0.1.9",
"karma-sinon-chai": "^0.2.0",
"karma-spec-reporter": "0.0.13",
"mocha": "^1.21.4"
"mocha": "^1.21.4",
"moment": "^2.18.1"
}
}
3 changes: 2 additions & 1 deletion src/models/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ nv.models.axis = function() {
var w;
switch (axis.orient()) {
case 'top':
xLabelMargin = axisLabelDistance + 36;
axisLabel.enter().append('text').attr('class', 'nv-axislabel');
w = 0;
if (scale.range().length === 1) {
Expand All @@ -89,7 +90,7 @@ nv.models.axis = function() {
};
axisLabel
.attr('text-anchor', 'middle')
.attr('y', 0)
.attr('y', -xLabelMargin)
.attr('x', w/2);
if (showMaxMin) {
axisMaxMin = wrap.selectAll('g.nv-axisMaxMin')
Expand Down
Loading

0 comments on commit 447cce8

Please sign in to comment.