|
| 1 | +import * as d3 from 'd3'; |
| 2 | + |
| 3 | +const COLORS_SERIES = ["#FF6492", "#141446", "#7A77FF"]; |
| 4 | +const CHART_HEIGHT = 300; |
| 5 | + |
| 6 | +const drawPieChart = (node, resultSet) => { |
| 7 | + const data = resultSet.series()[0].series.map(s => s.value); |
| 8 | + const data_ready = d3.pie()(data); |
| 9 | + |
| 10 | + d3.select(node).html(""); |
| 11 | + |
| 12 | + // The radius of the pieplot is half the width or half the height (smallest one). |
| 13 | + const radius = CHART_HEIGHT / 2 - 40; |
| 14 | + |
| 15 | + // Seprate container to center align pie chart |
| 16 | + const svg = d3.select(node) |
| 17 | + .append("svg") |
| 18 | + .attr("width", node.clientWidth) |
| 19 | + .attr("height", CHART_HEIGHT) |
| 20 | + .append("g") |
| 21 | + .attr('transform', 'translate(' + node.clientWidth/2 + ',' + CHART_HEIGHT/2 +')'); |
| 22 | + |
| 23 | + svg |
| 24 | + .selectAll('pieArcs') |
| 25 | + .data(data_ready) |
| 26 | + .enter() |
| 27 | + .append('path') |
| 28 | + .attr('d', d3.arc() |
| 29 | + .innerRadius(0) |
| 30 | + .outerRadius(radius) |
| 31 | + ) |
| 32 | + .attr('fill', d => COLORS_SERIES[d.index]) |
| 33 | +} |
| 34 | + |
| 35 | +const drawChart = (node, resultSet, chartType) => { |
| 36 | + if (chartType === 'pie') { |
| 37 | + return drawPieChart(node, resultSet); |
| 38 | + } |
| 39 | + |
| 40 | + const margin = { top: 10, right: 30, bottom: 30, left: 60 }, |
| 41 | + width = node.clientWidth - margin.left - margin.right, |
| 42 | + height = CHART_HEIGHT - margin.top - margin.bottom; |
| 43 | + |
| 44 | + d3.select(node).html(""); |
| 45 | + const svg = d3.select(node) |
| 46 | + .append("svg") |
| 47 | + .attr("width", width + margin.left + margin.right) |
| 48 | + .attr("height", height + margin.top + margin.bottom) |
| 49 | + .append("g") |
| 50 | + .attr("transform", |
| 51 | + "translate(" + margin.left + "," + margin.top + ")"); |
| 52 | + |
| 53 | + const keys = resultSet.seriesNames().map(s => s.key) |
| 54 | + |
| 55 | + let data, maxData; |
| 56 | + if (chartType === 'line') { |
| 57 | + data = resultSet.series().map((series) => ({ |
| 58 | + key: series.title, values: series.series |
| 59 | + })); |
| 60 | + maxData = d3.max(data.map((s) => d3.max(s.values, (i) => i.value))) |
| 61 | + } else { |
| 62 | + data = d3.stack() |
| 63 | + .keys(keys) |
| 64 | + (resultSet.chartPivot()) |
| 65 | + maxData = d3.max(data.map((s) => d3.max(s, (i) => i[1]))) |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + const color = d3.scaleOrdinal() |
| 70 | + .domain(keys) |
| 71 | + .range(COLORS_SERIES); |
| 72 | + |
| 73 | + let x; |
| 74 | + if (chartType === 'bar') { |
| 75 | + x = d3.scaleBand() |
| 76 | + .range([ 0, width ]) |
| 77 | + .domain(resultSet.chartPivot().map(c => c.x)) |
| 78 | + .padding(0.3); |
| 79 | + } else { |
| 80 | + x = d3.scaleTime() |
| 81 | + .domain(d3.extent(resultSet.chartPivot(), c => d3.isoParse(c.x))) |
| 82 | + .range([ 0, width ]); |
| 83 | + } |
| 84 | + |
| 85 | + svg.append("g") |
| 86 | + .attr("transform", "translate(0," + height + ")") |
| 87 | + .call(d3.axisBottom(x)); |
| 88 | + |
| 89 | + const y = d3.scaleLinear() |
| 90 | + .domain([0, maxData]) |
| 91 | + .range([ height, 0 ]); |
| 92 | + svg.append("g") |
| 93 | + .call(d3.axisLeft(y)); |
| 94 | + |
| 95 | + if (chartType === 'line') { |
| 96 | + svg.selectAll(".line") |
| 97 | + .data(data) |
| 98 | + .enter() |
| 99 | + .append("path") |
| 100 | + .attr("fill", "none") |
| 101 | + .attr("stroke", d => color(d.key)) |
| 102 | + .attr("stroke-width", 1.5) |
| 103 | + .attr("d", (d) => { |
| 104 | + return d3.line() |
| 105 | + .x(d => x(d3.isoParse(d.x))) |
| 106 | + .y(d => y(+d.value)) |
| 107 | + (d.values) |
| 108 | + }) |
| 109 | + } else if (chartType === 'area') { |
| 110 | + svg |
| 111 | + .selectAll("mylayers") |
| 112 | + .data(data) |
| 113 | + .enter().append("path") |
| 114 | + .style("fill", d => color(d.key)) |
| 115 | + .attr("d", d3.area() |
| 116 | + .x(d => x(d3.isoParse(d.data.x))) |
| 117 | + .y0(d => y(d[0])) |
| 118 | + .y1(d => y(d[1])) |
| 119 | + ) |
| 120 | + } else { |
| 121 | + svg.append("g") |
| 122 | + .selectAll("g") |
| 123 | + // Enter in the stack data = loop key per key = group per group |
| 124 | + .data(data) |
| 125 | + .enter().append("g") |
| 126 | + .attr("fill", d => color(d.key)) |
| 127 | + .selectAll("rect") |
| 128 | + // enter a second time = loop subgroup per subgroup to add all rectangles |
| 129 | + .data(d => d) |
| 130 | + .enter().append("rect") |
| 131 | + .attr("x", d => x(d.data.x)) |
| 132 | + .attr("y", d => y(d[1])) |
| 133 | + .attr("height", d => y(d[0]) - y(d[1])) |
| 134 | + .attr("width",x.bandwidth()) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +const D3Chart = ({ resultSet, type }) => ( |
| 139 | + <div |
| 140 | + ref={el => el && drawChart(el, resultSet, type)} |
| 141 | + /> |
| 142 | +); |
| 143 | + |
| 144 | +const TypeToChartComponent = { |
| 145 | + line: ({ resultSet }) => ( |
| 146 | + <D3Chart type='line' resultSet={resultSet} /> |
| 147 | + ), |
| 148 | + bar: ({ resultSet }) => ( |
| 149 | + <D3Chart type='bar' resultSet={resultSet} /> |
| 150 | + ), |
| 151 | + area: ({ resultSet }) => ( |
| 152 | + <D3Chart type='area' resultSet={resultSet} /> |
| 153 | + ), |
| 154 | + pie: ({ resultSet }) => ( |
| 155 | + <D3Chart type='pie' resultSet={resultSet} /> |
| 156 | + ), |
| 157 | +}; |
0 commit comments