|
| 1 | +/* |
| 2 | + * @Author: funlee |
| 3 | + |
| 4 | + * @Date: 2017-12-27 09:18:32 |
| 5 | + * @Last Modified time: 2017-12-27 09:18:32 |
| 6 | + * @Description: 柱状图和面积图混合 |
| 7 | + */ |
| 8 | +import * as d3 from 'd3' |
| 9 | +import config from '../tool/config' |
| 10 | +import { tooltip } from '../tool/utils' |
| 11 | +export default class MixBarArea { |
| 12 | + defaultSetting () { |
| 13 | + return { |
| 14 | + width: 1320, |
| 15 | + height: 330, |
| 16 | + padding: { |
| 17 | + top: 30, |
| 18 | + right: 40, |
| 19 | + bottom: 50, |
| 20 | + left: 30 |
| 21 | + }, |
| 22 | + fillBar: 'url(#fillBarProportion)', |
| 23 | + fillBarUp: 'url(#fillBarProportionUp)', |
| 24 | + fillArea: 'url(#fillAreaProportion)' |
| 25 | + } |
| 26 | + } |
| 27 | + constructor (selector, option) { |
| 28 | + const defaultSetting = this.defaultSetting() |
| 29 | + this.config = Object.assign(defaultSetting, option) |
| 30 | + const { |
| 31 | + width, |
| 32 | + height |
| 33 | + } = this.config |
| 34 | + // 创建svg |
| 35 | + this.svg = d3.select(selector).select('svg') |
| 36 | + .attr('width', width) |
| 37 | + .attr('height', height) |
| 38 | + } |
| 39 | + addGWrap (className) { |
| 40 | + const isWrap = this.svg.select('.' + className).empty() |
| 41 | + let wrap |
| 42 | + if (isWrap) { |
| 43 | + wrap = this.svg.append('g').attr('class', className) |
| 44 | + } else { |
| 45 | + wrap = this.svg.select('.' + className) |
| 46 | + } |
| 47 | + return wrap |
| 48 | + } |
| 49 | + drawChart (data) { |
| 50 | + const { width, height, padding: { bottom, top } } = this.config |
| 51 | + let xData = [] |
| 52 | + data.map(item => { |
| 53 | + xData.push(item.name) |
| 54 | + }) |
| 55 | + this.xScale = d3.scaleBand() // .attr("width", this.xScale.bandwidth()) |
| 56 | + .domain(d3.range(data.length)) |
| 57 | + .rangeRound([0, width]) |
| 58 | + .padding(0.8) |
| 59 | + // 占比的Y轴比例尺 |
| 60 | + let minValue = d3.min(data, d => { |
| 61 | + return d.proportion |
| 62 | + }) |
| 63 | + let maxValue = d3.max(data, d => { |
| 64 | + return d.proportion |
| 65 | + }) |
| 66 | + if (minValue === 0 && maxValue === 0) { |
| 67 | + minValue = 0 |
| 68 | + maxValue = 100 |
| 69 | + } |
| 70 | + this.yComScale = d3.scaleLinear() |
| 71 | + .domain([minValue * 1.2, maxValue * 1.2]) |
| 72 | + .range([0, height - bottom - top]) |
| 73 | + // value值的Y轴比例尺 |
| 74 | + this.yValueScale = d3.scaleLinear() |
| 75 | + .domain([0, d3.max(data, d => { |
| 76 | + return d.value |
| 77 | + }) * 1.2]) |
| 78 | + .range([0, height - bottom]) |
| 79 | + // 绘制面积图 |
| 80 | + this.drawArea(data) |
| 81 | + // 绘制柱子 |
| 82 | + this.drawBar(data) |
| 83 | + // 绘制柱图的Y轴 |
| 84 | + this.drawYaxisValue() |
| 85 | + // // 绘制同环比的Y轴 |
| 86 | + // this.drawYaxisCom() |
| 87 | + // // 绘制公用的X轴 |
| 88 | + // this.drawXaxis() |
| 89 | + } |
| 90 | + drawArea (data) { |
| 91 | + const { height, padding: { bottom }, fillArea } = this.config |
| 92 | + let areaPath = d3.area() |
| 93 | + .x((d, i) => { |
| 94 | + return this.xScale(i) |
| 95 | + }) |
| 96 | + .y0(height - bottom) |
| 97 | + areaPath.y1((d, i) => { |
| 98 | + return height - this.yComScale(d.proportion) - bottom |
| 99 | + }) |
| 100 | + const isArea = this.svg.select('.area-path').empty() |
| 101 | + let area |
| 102 | + if (isArea) { |
| 103 | + area = this.svg.append('path').attr('class', 'area-path') |
| 104 | + } else { |
| 105 | + area = this.svg.select('.area-path') |
| 106 | + } |
| 107 | + area |
| 108 | + .attr('d', areaPath(data)) |
| 109 | + .attr('stroke', 'none') |
| 110 | + .attr('fill', fillArea) |
| 111 | + .attr('opacity', 0.8) |
| 112 | + .attr('transform', `translate(${this.xScale.bandwidth() / 2},0)`) |
| 113 | + } |
| 114 | + drawBar (data) { |
| 115 | + const { height, padding: { bottom }, fillBar, fillBarUp } = this.config |
| 116 | + const { |
| 117 | + pageWidth, |
| 118 | + pageHeight |
| 119 | + } = config |
| 120 | + const barWrap = this.addGWrap('bar-wrap') |
| 121 | + const itemUpdate = barWrap.selectAll('.bar').data(data) |
| 122 | + const itemEnter = itemUpdate.enter().append('g').attr('class', 'bar') |
| 123 | + itemUpdate.exit().remove() |
| 124 | + const group = this.svg.selectAll('.bar') |
| 125 | + // 绘制value的柱子 |
| 126 | + itemEnter.append('rect') |
| 127 | + group.select('rect') |
| 128 | + .attr('x', (d, i) => { |
| 129 | + return this.xScale(i) |
| 130 | + }) |
| 131 | + .attr('width', this.xScale.bandwidth()) |
| 132 | + .attr('fill', fillBar) |
| 133 | + .attr('cursor', 'pointer') |
| 134 | + .attr('y', height - bottom) |
| 135 | + .attr('height', 0) |
| 136 | + .transition() |
| 137 | + .duration(2000) |
| 138 | + .attr('y', d => { |
| 139 | + return height - bottom - this.yValueScale(d.value) |
| 140 | + }) |
| 141 | + .attr('height', d => { |
| 142 | + return this.yValueScale(d.value) |
| 143 | + }) |
| 144 | + |
| 145 | + // 鼠标悬浮事件 |
| 146 | + group.on('mouseover', function (d) { |
| 147 | + d3.select(this).select('rect').attr('fill', fillBarUp) |
| 148 | + const top = d3.event.pageY / (window.innerHeight / pageHeight) |
| 149 | + const left = d3.event.pageX / (window.innerWidth / pageWidth) + 20 |
| 150 | + const option = { |
| 151 | + el: '.auto-tooltip', |
| 152 | + location: { |
| 153 | + x: left, |
| 154 | + y: top |
| 155 | + }, |
| 156 | + data: [{ |
| 157 | + name: '数量', |
| 158 | + value: d.value |
| 159 | + }, { |
| 160 | + name: '占比', |
| 161 | + value: d.proportion + '%' |
| 162 | + }] |
| 163 | + } |
| 164 | + tooltip(option) |
| 165 | + d3.select('.auto-tooltip').style('display', 'block') |
| 166 | + }) |
| 167 | + .on('mouseout', function () { |
| 168 | + d3.select(this).select('rect') |
| 169 | + .transition() |
| 170 | + .duration(1000) |
| 171 | + .attr('fill', fillBar) |
| 172 | + d3.select('.auto-tooltip').style('display', 'none') |
| 173 | + }) |
| 174 | + } |
| 175 | + drawYaxisValue () { |
| 176 | + const { height, padding: { bottom, top } } = this.config |
| 177 | + this.yValueScale.range([height - bottom - top, 0]) |
| 178 | + const yValueAxis = d3.axisLeft(this.yValueScale).ticks(5) |
| 179 | + const isYValueAxis = this.svg.select('.y-value-axis').empty() |
| 180 | + let yValueAxisG |
| 181 | + if (isYValueAxis) { |
| 182 | + yValueAxisG = this.svg.append('g').attr('class', 'y-value-axis axis') |
| 183 | + } else { |
| 184 | + yValueAxisG = this.svg.select('.y-value-axis') |
| 185 | + } |
| 186 | + yValueAxisG.attr('transform', `translate(80,${top})`) |
| 187 | + .call(yValueAxis) |
| 188 | + } |
| 189 | +} |
0 commit comments