├── .editorconfig ├── .eslintrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── index.js ├── package.json ├── src └── horizon-chart.js └── test └── horizon-chart-test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | [**.js] 2 | indent_style = space 3 | indent_size = 4 4 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | parserOptions: 2 | sourceType: "module" 3 | 4 | env: 5 | es6: true 6 | 7 | extends: 8 | "eslint:recommended" 9 | 10 | rules: 11 | no-cond-assign: 0 12 | no-constant-condition: 0 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.sublime-workspace 2 | .DS_Store 3 | build/ 4 | node_modules 5 | npm-debug.log 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | build/*.zip 2 | test/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Kiril Mandov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![d3 horizon chart](http://kmandov.github.io/d3-horizon-chart/img/d3-horizon-charts-lead-01.png)](http://kmandov.github.io/d3-horizon-chart/) 2 | 3 | # d3-horizon-chart 4 | 5 | [![npm version](https://badge.fury.io/js/d3-horizon-chart.svg)](http://badge.fury.io/js/d3-horizon-chart) 6 | 7 | `d3-horizon-chart` is a d3 plugin that draws horizon charts using an Html5 Canvas. 8 | It provides an easy way to visualize large amounts of time series data. 9 | 10 | The plugin is heavily inspired by [cubism.js](https://square.github.io/cubism/), but doesn't make assumptions about real-time metrics and back-end servers such as Cube/Graphite. 11 | This makes it a good fit when you just want to plot your data as a horizon chart. 12 | 13 | `d3-horizon-chart` follows the [latest plugin guidelines](https://bost.ocks.org/mike/d3-plugin/) using D3’s new 4.0 module pattern. 14 | 15 | 16 | ## Examples 17 | 18 | - [Drawing a single horizon chart](http://bl.ocks.org/kmandov/a1abe4aa380fb8b4bd0b4c081a76ce13) 19 | - [Loosing resolution when using area chart](http://bl.ocks.org/kmandov/5af65af3875c5c4afcdc0d675f60bb45) 20 | - [Horizon charts preserve resolution at smaller heights](http://bl.ocks.org/kmandov/6e91165d4f32534ec4cab806b18b6684) 21 | - [Visualizing stock data using horizon charts](http://bl.ocks.org/kmandov/dcb94f02e71e3560eb3255f2de3120e4) 22 | 23 | 24 | Check out the [project page](http://kmandov.github.io/d3-horizon-chart/) for more examples. 25 | 26 | ## Installing 27 | 28 | If you use NPM, `npm install d3-horizon-chart`. Otherwise, download the [latest release](https://github.com/kmandov/d3-horizon-chart/releases/latest). 29 | 30 | ## API Reference 31 | 32 | # horizonChart() 33 | 34 | Constructs a new horizonChart generator with the default settings. 35 | 36 | # horizon.height([height]) 37 | 38 | If height is specified, sets the height of the chart to the specified number and returns this horizonChart generator. If height is not specified, returns the current height accessor, which defaults to: 39 | 40 | ```js 41 | function height() { 42 | return 30; 43 | } 44 | ``` 45 | 46 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export {default as horizonChart} from "./src/horizon-chart"; 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "d3-horizon-chart", 3 | "version": "0.0.6", 4 | "description": "D3 Plugin that draws horizon charts using canvas.", 5 | "keywords": [ 6 | "d3", 7 | "d3-module", 8 | "horizon chart", 9 | "time series", 10 | "data visualization", 11 | "morphocode" 12 | ], 13 | "author": { 14 | "name": "Kiril Mandov", 15 | "url": "http://morphocode.com" 16 | }, 17 | "license": "MIT", 18 | "main": "build/d3-horizon-chart.js", 19 | "jsnext:main": "index", 20 | "homepage": "https://github.com/kmandov/d3-horizon-chart", 21 | "repository": { 22 | "type": "git", 23 | "url": "https://github.com/kmandov/d3-horizon-chart.git" 24 | }, 25 | "scripts": { 26 | "pretest": "rm -rf build && mkdir build && rollup --banner \"$(preamble)\" -g d3-array:d3,d3-axis:d3,d3-scale:d3,d3-selection:d3 -f umd -n d3 -o build/d3-horizon-chart.js -- index.js", 27 | "test": "tape 'test/**/*-test.js' && eslint index.js src", 28 | "prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-horizon-chart.js -c -m -o build/d3-horizon-chart.min.js", 29 | "postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git push --tags && zip -j build/d3-horizon-chart.zip -- LICENSE README.md build/d3-horizon-chart.js build/d3-horizon-chart.min.js" 30 | }, 31 | "dependencies": { 32 | "d3-array": "^1.0.1", 33 | "d3-axis": "^1.0.1", 34 | "d3-scale": "^1.0.1", 35 | "d3-selection": "^1.0.2" 36 | }, 37 | "devDependencies": { 38 | "eslint": "^3.4.0", 39 | "json2module": "0.0.3", 40 | "package-preamble": "0.0.2", 41 | "rollup": "^0.34.13", 42 | "tape": "^4.6.0", 43 | "uglify-js": "^2.7.3" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/horizon-chart.js: -------------------------------------------------------------------------------- 1 | import {select} from 'd3-selection'; 2 | import {scaleLinear} from 'd3-scale'; 3 | import {axisTop} from 'd3-axis'; 4 | import {extent as d3_extent} from 'd3-array'; 5 | 6 | export default function () { 7 | 8 | // default settings: 9 | //var colors = ['#08519c','#3182bd','#6baed6','#bdd7e7','#bae4b3','#74c476','#31a354','#006d2c'], 10 | //var colors = ['#4575b4', '#74add1', '#abd9e9', '#e0f3f8', '#fee090', '#fdae61', '#f46d43', '#d73027'], 11 | //var colors = ["#5e4fa2", "#3288bd", "#66c2a5", "#abdda4", "#e6f598", "#fee08b", "#fdae61", "#f46d43", "#d53e4f", "#9e0142"], 12 | var colors = ["#313695", "#4575b4", "#74add1", "#abd9e9", "#fee090", "#fdae61", "#f46d43", "#d73027"], 13 | bands = colors.length >> 1, // number of bands in each direction (positive / negative) 14 | width = 1000, // width in pixels 15 | height = 30, 16 | offsetX = 0, 17 | step = 1, 18 | spacing = 0, 19 | mode = 'offset', 20 | axis = null, 21 | title = null, 22 | extent = null, // the extent is derived from the data, unless explicitly set via .extent([min, max]) 23 | //x = d3.scaleLinear(), // TODO: use ordinal scale instead? 24 | x = null, 25 | y = scaleLinear().range([0, height]), 26 | canvas = null; 27 | 28 | // Appends a canvas element to the current element 29 | // and draws a horizon graph based on data & settings 30 | function horizonChart(data) { 31 | 32 | var selection = select(this); 33 | var dIncrement = step + spacing; 34 | 35 | // update the width 36 | //width = horizon.node().getBoundingClientRect().width; 37 | width = dIncrement * data.length; 38 | canvas = selection.append('canvas'); 39 | 40 | canvas 41 | .attr('width', width) 42 | .attr('height', height); 43 | 44 | selection.append('span') 45 | .attr('class', 'title') 46 | .text(title); 47 | 48 | selection.append('span') 49 | .attr('class', 'value'); 50 | 51 | var context = canvas.node().getContext('2d'); 52 | //context.imageSmoothingEnabled = false; 53 | //context.translate(margin.left, margin.top); 54 | 55 | // update the y scale, based on the data extents 56 | var _extent = extent || d3_extent(data); 57 | 58 | var max = Math.max(-_extent[0], _extent[1]); 59 | y.domain([0, max]); 60 | //x = d3.scaleTime().domain[]; 61 | axis = axisTop(x).ticks(5); 62 | 63 | // Draw ---------------------------------------------------------------------------- 64 | 65 | context.clearRect(0, 0, width, height); 66 | //context.translate(0.5, 0.5); 67 | 68 | // the data frame currently being shown: 69 | var increment = step + spacing, 70 | startIndex = ~~Math.max(0, -(offsetX / increment)), 71 | endIndex = ~~Math.min(data.length, startIndex + width / increment); 72 | 73 | // skip drawing if there's no data to be drawn 74 | if (startIndex > data.length) return; 75 | 76 | 77 | // we are drawing positive & negative bands separately to avoid mutating canvas state 78 | // http://www.html5rocks.com/en/tutorials/canvas/performance/ 79 | 80 | var negative = false; 81 | // draw positive bands 82 | for (var b = 0; b < bands; b++) { 83 | context.fillStyle = colors[bands + b]; 84 | 85 | // Adjust the range based on the current band index. 86 | var bExtents = (b + 1 - bands) * height; 87 | y.range([bands * height + bExtents, bExtents]); 88 | 89 | // only the current data frame is being drawn i.e. what's visible: 90 | for (var i = startIndex, value; i < endIndex; i++) { 91 | value = data[i]; 92 | if (value <= 0) { negative = true; continue; } 93 | if (value === undefined) continue; 94 | context.fillRect(offsetX + i * increment, y(value), step, y(0) - y(value)); 95 | } 96 | } 97 | 98 | // draw negative bands 99 | if (negative) { 100 | 101 | // mirror the negative bands, by flipping the canvas 102 | if (mode === 'offset') { 103 | context.translate(0, height); 104 | context.scale(1, -1); 105 | } 106 | 107 | for (b = 0; b < bands; b++) { 108 | context.fillStyle = colors[bands - b - 1]; 109 | 110 | // Adjust the range based on the current band index. 111 | bExtents = (b + 1 - bands) * height; 112 | y.range([bands * height + bExtents, bExtents]); 113 | 114 | // only the current data frame is being drawn i.e. what's visible: 115 | for (var j = startIndex, nvalue; j < endIndex; j++) { 116 | nvalue = data[j]; 117 | if (nvalue >= 0) continue; 118 | context.fillRect(offsetX + j * increment, y(-nvalue), step, y(0) - y(-nvalue)); 119 | } 120 | } 121 | } 122 | 123 | /* 124 | // Offscreen Draw ----------------------------------------------------------------------- 125 | 126 | function createOffscreenCanvas(width,height){ 127 | var canvas = document.createElement('canvas'); 128 | canvas.width = width; 129 | canvas.height = height; 130 | return canvas; 131 | } 132 | 133 | var offscreenCanvas = createOffscreenCanvas(increment * data.length, height); 134 | var offscreenContext = offscreenCanvas.getContext('2d'); 135 | // draw each band: 136 | for (var b = 0; b < bands; b++) { 137 | offscreenContext.fillStyle = colors[b]; 138 | 139 | // Adjust the range based on the current band index. 140 | var y0 = (b + 1 - bands) * height; 141 | y.range([bands * height + y0, y0]); 142 | 143 | // draw the whole period on an offscreen canvas 144 | for (var i = 0; i < data.length; i++) { 145 | offscreenContext.fillRect(i * increment, y(data[i]), step, y(0) - y(data[i])); 146 | } 147 | } 148 | 149 | var onscreenImage; 150 | _draw = function() { 151 | onscreenImage = offscreenContext.getImageData(-offsetX, 0, width, height); 152 | context.putImageData(onscreenImage, 0, 0); 153 | 154 | //context.clearRect(0, 0, width, height); 155 | //context.translate(offsetX, 0); 156 | //context.drawImage(offscreenCanvas, offsetX, 0); 157 | }; 158 | */ 159 | } 160 | 161 | horizonChart.axis = function (_) { 162 | return arguments.length ? (axis = _, horizonChart) : axis; 163 | }; 164 | 165 | horizonChart.canvas = function (_) { 166 | return arguments.length ? (canvas = _, horizonChart) : canvas; 167 | }; 168 | 169 | // Array of colors representing the number of bands 170 | horizonChart.colors = function (_) { 171 | if (!arguments.length) return colors; 172 | colors = _; 173 | 174 | // update the number of bands 175 | bands = colors.length >> 1; 176 | 177 | return horizonChart; 178 | }; 179 | 180 | // get/set the height of the graph 181 | horizonChart.height = function (_) { 182 | return arguments.length ? (height = _, horizonChart) : height; 183 | }; 184 | 185 | // get/set the step of the graph, i.e. the width of each bar 186 | horizonChart.step = function (_) { 187 | return arguments.length ? (step = _, horizonChart) : step; 188 | }; 189 | 190 | // get/set the spacing between the bars of the graph 191 | horizonChart.spacing = function (_) { 192 | return arguments.length ? (spacing = _, horizonChart) : spacing; 193 | }; 194 | 195 | // get/set the title of the horizon 196 | horizonChart.title = function (_) { 197 | return arguments.length ? (title = _, horizonChart) : title; 198 | }; 199 | 200 | // mirror or offset 201 | horizonChart.mode = function (_) { 202 | return arguments.length ? (mode = _, horizonChart) : mode; 203 | }; 204 | 205 | // get/set the extents of the Y axis. If not set the extents are derived from the data 206 | horizonChart.extent = function (_) { 207 | return arguments.length ? (extent = _, horizonChart) : extent; 208 | }; 209 | 210 | horizonChart.offsetX = function (_) { 211 | return arguments.length ? (offsetX = _, horizonChart) : offsetX; 212 | }; 213 | 214 | // the data frame currently being shown: 215 | horizonChart.indexExtent = function () { 216 | var increment = step + spacing, 217 | startIndex = -offsetX / increment, 218 | endIndex = startIndex + width / increment; 219 | 220 | return [startIndex, endIndex]; 221 | }; 222 | 223 | return horizonChart; 224 | 225 | } -------------------------------------------------------------------------------- /test/horizon-chart-test.js: -------------------------------------------------------------------------------- 1 | var tape = require("tape"), 2 | hc = require("../"); 3 | 4 | tape("horizonChart() has a default height.", function(test) { 5 | test.equal(hc.horizonChart().height(), 30); 6 | test.end(); 7 | }); 8 | --------------------------------------------------------------------------------