From 63c25a452e97374ca95d533b20344735c6c9e7b0 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 21 Jun 2018 15:41:20 -0500 Subject: [PATCH] Add bar chart component --- .../vue_shared/components/bar_chart.vue | 391 ++++++++++++++++++ .../components/bar_chart_constants.js | 4 + .../vue_shared/components/svg_gradient.vue | 37 ++ app/assets/stylesheets/pages/graph.scss | 58 +++ package.json | 1 + .../vue_shared/components/bar_chart_spec.js | 85 ++++ yarn.lock | 160 ++++++- 7 files changed, 717 insertions(+), 19 deletions(-) create mode 100644 app/assets/javascripts/vue_shared/components/bar_chart.vue create mode 100644 app/assets/javascripts/vue_shared/components/bar_chart_constants.js create mode 100644 app/assets/javascripts/vue_shared/components/svg_gradient.vue create mode 100644 spec/javascripts/vue_shared/components/bar_chart_spec.js diff --git a/app/assets/javascripts/vue_shared/components/bar_chart.vue b/app/assets/javascripts/vue_shared/components/bar_chart.vue new file mode 100644 index 00000000000..3ced4eb691a --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/bar_chart.vue @@ -0,0 +1,391 @@ + + diff --git a/app/assets/javascripts/vue_shared/components/bar_chart_constants.js b/app/assets/javascripts/vue_shared/components/bar_chart_constants.js new file mode 100644 index 00000000000..6957b112da6 --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/bar_chart_constants.js @@ -0,0 +1,4 @@ +export const GRADIENT_COLORS = ['#000', '#a7a7a7']; +export const GRADIENT_OPACITY = ['0', '0.4']; +export const INVERSE_GRADIENT_COLORS = ['#a7a7a7', '#000']; +export const INVERSE_GRADIENT_OPACITY = ['0.4', '0']; diff --git a/app/assets/javascripts/vue_shared/components/svg_gradient.vue b/app/assets/javascripts/vue_shared/components/svg_gradient.vue new file mode 100644 index 00000000000..b61a1befcd6 --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/svg_gradient.vue @@ -0,0 +1,37 @@ + + diff --git a/app/assets/stylesheets/pages/graph.scss b/app/assets/stylesheets/pages/graph.scss index 84da9180f93..49d8a5d959b 100644 --- a/app/assets/stylesheets/pages/graph.scss +++ b/app/assets/stylesheets/pages/graph.scss @@ -31,3 +31,61 @@ color: $gl-text-red; } } + +.svg-graph-container { + width: 100%; + + .axis-tick { + opacity: 0.4; + } + + .tick-text { + fill: $gl-text-color-secondary; + } + + .x-axis-text { + fill: $theme-gray-900; + } + + .bar-rect { + fill: rgba($blue-500, 0.1); + stroke: $blue-500; + } + + .bar-rect:hover { + fill: rgba($blue-700, 0.3); + } + + .y-axis-label { + line { + stroke: $stat-graph-axis-fill; + } + + text { + font-weight: bold; + font-size: 12px; + fill: $theme-gray-800; + } + } +} + +.svg-graph-container-with-grab { + cursor: grab; + cursor: -webkit-grab; +} + +.svg-graph-container-grabbed { + cursor: grabbing; + cursor: -webkit-grabbing; +} + +@keyframes flickerAnimation { + 0% { opacity: 1; } + 50% { opacity: 0; } + 100% { opacity: 1; } +} + +.animate-flicker { + animation: flickerAnimation 1.5s infinite; + fill: $theme-gray-500; +} diff --git a/package.json b/package.json index 256ebc1fb6e..33fa8665a58 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "core-js": "^2.4.1", "cropper": "^2.3.0", "css-loader": "^1.0.0", + "d3": "4.12.2", "d3-array": "^1.2.1", "d3-axis": "^1.0.8", "d3-brush": "^1.0.4", diff --git a/spec/javascripts/vue_shared/components/bar_chart_spec.js b/spec/javascripts/vue_shared/components/bar_chart_spec.js new file mode 100644 index 00000000000..7e91cd6f63f --- /dev/null +++ b/spec/javascripts/vue_shared/components/bar_chart_spec.js @@ -0,0 +1,85 @@ +import Vue from 'vue'; +import BarChart from '~/vue_shared/components/bar_chart.vue'; +import mountComponent from 'spec/helpers/vue_mount_component_helper'; + +function getRandomArbitrary(min, max) { + return Math.random() * (max - min) + min; +} + +function generateRandomData(dataNumber) { + const randomGraphData = []; + + for (let i = 1; i <= dataNumber; i += 1) { + randomGraphData.push({ + name: `random ${i}`, + value: parseInt(getRandomArbitrary(1, 8), 10), + }); + } + + return randomGraphData; +} + +describe('Bar chart component', () => { + let barChart; + const graphData = generateRandomData(10); + + beforeEach(() => { + const BarChartComponent = Vue.extend(BarChart); + + barChart = mountComponent(BarChartComponent, { + graphData, + yAxisLabel: 'data', + }); + }); + + afterEach(() => { + barChart.$destroy(); + }); + + it('calculates the padding for even distribution across bars', () => { + barChart.vbWidth = 1000; + const result = barChart.calculatePadding(30); + + // since padding can't be higher than 1 and lower than 0 + // for more info: https://github.com/d3/d3-scale#band-scales + expect(result).not.toBeLessThan(0); + expect(result).not.toBeGreaterThan(1); + }); + + it('formats the tooltip title', () => { + const tooltipTitle = barChart.setTooltipTitle(barChart.graphData[0]); + + expect(tooltipTitle).toContain('random 1:'); + }); + + it('has a translates the bar graphs on across the X axis', () => { + barChart.panX = 100; + + expect(barChart.barTranslationTransform).toEqual('translate(100, 0)'); + }); + + it('translates the scroll indicator to the far right side', () => { + barChart.vbWidth = 500; + + expect(barChart.scrollIndicatorTransform).toEqual('translate(420, 0)'); + }); + + it('translates the x-axis to the bottom of the viewbox and pan coordinates', () => { + barChart.panX = 100; + barChart.vbHeight = 250; + + expect(barChart.xAxisLocation).toEqual('translate(100, 250)'); + }); + + it('Contains a total of 4 ticks across the y axis', () => { + const ticks = barChart.$el.querySelector('.y-axis').querySelectorAll('.tick').length; + + expect(ticks).toEqual(4); + }); + + it('rotates the x axis labels a total of 90 degress (CCW)', () => { + const xAxisLabel = barChart.$el.querySelector('.x-axis').querySelectorAll('text')[0]; + + expect(xAxisLabel.getAttribute('transform')).toEqual('rotate(-90)'); + }); +}); diff --git a/yarn.lock b/yarn.lock index 85fdb150d34..4c25e1d87a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1774,7 +1774,7 @@ combined-stream@1.0.6, combined-stream@^1.0.5, combined-stream@~1.0.5: dependencies: delayed-stream "~1.0.0" -commander@^2.13.0, commander@^2.15.1, commander@^2.9.0: +commander@2, commander@^2.13.0, commander@^2.15.1, commander@^2.9.0: version "2.15.1" resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" @@ -2070,15 +2070,15 @@ cyclist@~0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-0.2.2.tgz#1b33792e11e914a2fd6d6ed6447464444e5fa640" -d3-array@^1.2.0, d3-array@^1.2.1: +d3-array@1, d3-array@1.2.1, d3-array@^1.2.0, d3-array@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-1.2.1.tgz#d1ca33de2f6ac31efadb8e050a021d7e2396d5dc" -d3-axis@^1.0.8: +d3-axis@1.0.8, d3-axis@^1.0.8: version "1.0.8" resolved "https://registry.yarnpkg.com/d3-axis/-/d3-axis-1.0.8.tgz#31a705a0b535e65759de14173a31933137f18efa" -d3-brush@^1.0.4: +d3-brush@1.0.4, d3-brush@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/d3-brush/-/d3-brush-1.0.4.tgz#00c2f238019f24f6c0a194a26d41a1530ffe7bc4" dependencies: @@ -2088,44 +2088,103 @@ d3-brush@^1.0.4: d3-selection "1" d3-transition "1" -d3-collection@1: +d3-chord@1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/d3-chord/-/d3-chord-1.0.4.tgz#7dec4f0ba886f713fe111c45f763414f6f74ca2c" + dependencies: + d3-array "1" + d3-path "1" + +d3-collection@1, d3-collection@1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/d3-collection/-/d3-collection-1.0.4.tgz#342dfd12837c90974f33f1cc0a785aea570dcdc2" -d3-color@1: +d3-color@1, d3-color@1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/d3-color/-/d3-color-1.0.3.tgz#bc7643fca8e53a8347e2fbdaffa236796b58509b" -d3-dispatch@1: +d3-dispatch@1, d3-dispatch@1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/d3-dispatch/-/d3-dispatch-1.0.3.tgz#46e1491eaa9b58c358fce5be4e8bed626e7871f8" -d3-drag@1: +d3-drag@1, d3-drag@1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/d3-drag/-/d3-drag-1.2.1.tgz#df8dd4c502fb490fc7462046a8ad98a5c479282d" dependencies: d3-dispatch "1" d3-selection "1" -d3-ease@1: +d3-dsv@1, d3-dsv@1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/d3-dsv/-/d3-dsv-1.0.8.tgz#907e240d57b386618dc56468bacfe76bf19764ae" + dependencies: + commander "2" + iconv-lite "0.4" + rw "1" + +d3-ease@1, d3-ease@1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/d3-ease/-/d3-ease-1.0.3.tgz#68bfbc349338a380c44d8acc4fbc3304aa2d8c0e" -d3-format@1: +d3-force@1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/d3-force/-/d3-force-1.1.0.tgz#cebf3c694f1078fcc3d4daf8e567b2fbd70d4ea3" + dependencies: + d3-collection "1" + d3-dispatch "1" + d3-quadtree "1" + d3-timer "1" + +d3-format@1, d3-format@1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/d3-format/-/d3-format-1.2.1.tgz#4e19ecdb081a341dafaf5f555ee956bcfdbf167f" -d3-interpolate@1: +d3-geo@1.9.1: + version "1.9.1" + resolved "https://registry.yarnpkg.com/d3-geo/-/d3-geo-1.9.1.tgz#157e3b0f917379d0f73bebfff3be537f49fa7356" + dependencies: + d3-array "1" + +d3-hierarchy@1.1.5: + version "1.1.5" + resolved "https://registry.yarnpkg.com/d3-hierarchy/-/d3-hierarchy-1.1.5.tgz#a1c845c42f84a206bcf1c01c01098ea4ddaa7a26" + +d3-interpolate@1, d3-interpolate@1.1.6: version "1.1.6" resolved "https://registry.yarnpkg.com/d3-interpolate/-/d3-interpolate-1.1.6.tgz#2cf395ae2381804df08aa1bf766b7f97b5f68fb6" dependencies: d3-color "1" -d3-path@1: +d3-path@1, d3-path@1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/d3-path/-/d3-path-1.0.5.tgz#241eb1849bd9e9e8021c0d0a799f8a0e8e441764" -d3-scale@^1.0.7: +d3-polygon@1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/d3-polygon/-/d3-polygon-1.0.3.tgz#16888e9026460933f2b179652ad378224d382c62" + +d3-quadtree@1, d3-quadtree@1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/d3-quadtree/-/d3-quadtree-1.0.3.tgz#ac7987e3e23fe805a990f28e1b50d38fcb822438" + +d3-queue@3.0.7: + version "3.0.7" + resolved "https://registry.yarnpkg.com/d3-queue/-/d3-queue-3.0.7.tgz#c93a2e54b417c0959129d7d73f6cf7d4292e7618" + +d3-random@1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/d3-random/-/d3-random-1.1.0.tgz#6642e506c6fa3a648595d2b2469788a8d12529d3" + +d3-request@1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/d3-request/-/d3-request-1.0.6.tgz#a1044a9ef4ec28c824171c9379fae6d79474b19f" + dependencies: + d3-collection "1" + d3-dispatch "1" + d3-dsv "1" + xmlhttprequest "1" + +d3-scale@1.0.7, d3-scale@^1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/d3-scale/-/d3-scale-1.0.7.tgz#fa90324b3ea8a776422bd0472afab0b252a0945d" dependencies: @@ -2137,31 +2196,31 @@ d3-scale@^1.0.7: d3-time "1" d3-time-format "2" -d3-selection@1, d3-selection@^1.1.0, d3-selection@^1.2.0: +d3-selection@1, d3-selection@1.2.0, d3-selection@^1.1.0, d3-selection@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/d3-selection/-/d3-selection-1.2.0.tgz#1b8ec1c7cedadfb691f2ba20a4a3cfbeb71bbc88" -d3-shape@^1.2.0: +d3-shape@1.2.0, d3-shape@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/d3-shape/-/d3-shape-1.2.0.tgz#45d01538f064bafd05ea3d6d2cb748fd8c41f777" dependencies: d3-path "1" -d3-time-format@2, d3-time-format@^2.1.1: +d3-time-format@2, d3-time-format@2.1.1, d3-time-format@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/d3-time-format/-/d3-time-format-2.1.1.tgz#85b7cdfbc9ffca187f14d3c456ffda268081bb31" dependencies: d3-time "1" -d3-time@1, d3-time@^1.0.8: +d3-time@1, d3-time@1.0.8, d3-time@^1.0.8: version "1.0.8" resolved "https://registry.yarnpkg.com/d3-time/-/d3-time-1.0.8.tgz#dbd2d6007bf416fe67a76d17947b784bffea1e84" -d3-timer@1: +d3-timer@1, d3-timer@1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/d3-timer/-/d3-timer-1.0.7.tgz#df9650ca587f6c96607ff4e60cc38229e8dd8531" -d3-transition@1: +d3-transition@1, d3-transition@1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/d3-transition/-/d3-transition-1.1.1.tgz#d8ef89c3b848735b060e54a39b32aaebaa421039" dependencies: @@ -2172,10 +2231,59 @@ d3-transition@1: d3-selection "^1.1.0" d3-timer "1" +d3-voronoi@1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/d3-voronoi/-/d3-voronoi-1.1.2.tgz#1687667e8f13a2d158c80c1480c5a29cb0d8973c" + +d3-zoom@1.7.1: + version "1.7.1" + resolved "https://registry.yarnpkg.com/d3-zoom/-/d3-zoom-1.7.1.tgz#02f43b3c3e2db54f364582d7e4a236ccc5506b63" + dependencies: + d3-dispatch "1" + d3-drag "1" + d3-interpolate "1" + d3-selection "1" + d3-transition "1" + d3@3.5.17: version "3.5.17" resolved "https://registry.yarnpkg.com/d3/-/d3-3.5.17.tgz#bc46748004378b21a360c9fc7cf5231790762fb8" +d3@4.12.2: + version "4.12.2" + resolved "https://registry.yarnpkg.com/d3/-/d3-4.12.2.tgz#12f775564c6a9de229f63db03446e2cb7bb56c8f" + dependencies: + d3-array "1.2.1" + d3-axis "1.0.8" + d3-brush "1.0.4" + d3-chord "1.0.4" + d3-collection "1.0.4" + d3-color "1.0.3" + d3-dispatch "1.0.3" + d3-drag "1.2.1" + d3-dsv "1.0.8" + d3-ease "1.0.3" + d3-force "1.1.0" + d3-format "1.2.1" + d3-geo "1.9.1" + d3-hierarchy "1.1.5" + d3-interpolate "1.1.6" + d3-path "1.0.5" + d3-polygon "1.0.3" + d3-quadtree "1.0.3" + d3-queue "3.0.7" + d3-random "1.1.0" + d3-request "1.0.6" + d3-scale "1.0.7" + d3-selection "1.2.0" + d3-shape "1.2.0" + d3-time "1.0.8" + d3-time-format "2.1.1" + d3-timer "1.0.7" + d3-transition "1.1.1" + d3-voronoi "1.1.2" + d3-zoom "1.7.1" + dagre-d3-renderer@^0.4.24: version "0.4.24" resolved "https://registry.yarnpkg.com/dagre-d3-renderer/-/dagre-d3-renderer-0.4.24.tgz#b36ce2fe4ea20de43e7698627c6ede2a9f15ec45" @@ -3771,6 +3879,12 @@ https-proxy-agent@^2.2.1: agent-base "^4.1.0" debug "^3.1.0" +iconv-lite@0.4: + version "0.4.23" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63" + dependencies: + safer-buffer ">= 2.1.2 < 3" + iconv-lite@0.4.15: version "0.4.15" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.15.tgz#fe265a218ac6a57cfe854927e9d04c19825eddeb" @@ -6293,6 +6407,10 @@ run-queue@^1.0.0, run-queue@^1.0.3: dependencies: aproba "^1.1.1" +rw@1: + version "1.3.3" + resolved "https://registry.yarnpkg.com/rw/-/rw-1.3.3.tgz#3f862dfa91ab766b14885ef4d01124bfda074fb4" + rx-lite-aggregates@^4.0.8: version "4.0.8" resolved "https://registry.yarnpkg.com/rx-lite-aggregates/-/rx-lite-aggregates-4.0.8.tgz#753b87a89a11c95467c4ac1626c4efc4e05c67be" @@ -7772,6 +7890,10 @@ xmlhttprequest-ssl@~1.5.4: version "1.5.5" resolved "https://registry.yarnpkg.com/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz#c2876b06168aadc40e57d97e81191ac8f4398b3e" +xmlhttprequest@1: + version "1.8.0" + resolved "https://registry.yarnpkg.com/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz#67fe075c5c24fef39f9d65f5f7b7fe75171968fc" + xregexp@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-2.0.0.tgz#52a63e56ca0b84a7f3a5f3d61872f126ad7a5943"