Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1457 from metasfresh/dev-1427
Browse files Browse the repository at this point in the history
Fix d3 errors #1427
  • Loading branch information
teosarca authored Dec 15, 2017
2 parents bdc7bd5 + b9be3ba commit f6915d3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/components/charts/BarChartComponent/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ const getXAxisTickAngle = (size, width) => {
return 2 * Math.atan((a + Math.sqrt(a * a + b * b - c * c)) / (b + c));
};

export const populateY0Axis = (svg, rangeY, width, data, fields) => {
const keys = fields.map(field => field.fieldName);
const minY = d3.min(data, d => {
return d3.min(keys, key => {
return d[key];
});
});
if (minY >= 0) return;

svg
.select("g.y0-axis")
.selectAll("*")
.remove();

svg
.select("g.y0-axis")
.append("line")
.attr("y1", rangeY(-3))
.attr("x1", 0)
.attr("y2", rangeY(-3))
.attr("x2", width)
.attr("stroke-width", 1)
.attr("opacity", 0.5)
.attr("stroke", "black");
};

export const populateXAxis = (svg, rangeX0) => {
const sizes = [];

Expand Down
3 changes: 1 addition & 2 deletions src/components/charts/BarChartComponent/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export const drawData = (
.merge(bars)
.attr("x", d => ranges.x1(d.key))
.attr("width", ranges.x1.bandwidth())

.attr("y", d => {
if (yChanged || reRender) {
return dimensions.height;
Expand All @@ -118,6 +117,6 @@ export const drawData = (
.transition()
.duration(1000)
.attr("y", d => ranges.y(d.value))
.attr("height", d => dimensions.height - ranges.y(d.value))
.attr("height", d => Math.abs(ranges.y(d.value) - ranges.y(0)))
.attr("fill", d => ranges.z(d.key));
};
6 changes: 5 additions & 1 deletion src/components/charts/BarChartComponent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
getXAxisLabelsHeight,
moveXAxis,
populateXAxis,
populateYAxis
populateYAxis,
populateY0Axis
} from "./axes";
import { drawData } from "./data";
import { getHorizontalDimensions, getVerticalDimensions } from "./dimensions";
Expand Down Expand Up @@ -41,6 +42,9 @@ class BarChartComponent extends Component {
height
);
const rangeY = getYRange(vertical.height, data, fields);

populateY0Axis(this.svg, rangeY, horizontal.width, data, fields);

populateYAxis(this.svg, rangeY);

// adjust x axis
Expand Down
6 changes: 5 additions & 1 deletion src/components/charts/BarChartComponent/ranges.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export const getYRange = (height, data, fields) => {
.scaleLinear()
.range([height, 0])
.domain([
0,
d3.min(data, d => {
return d3.min(keys, key => {
return d[key];
});
}),
d3.max(data, d => {
return d3.max(keys, key => {
return d[key];
Expand Down
2 changes: 2 additions & 0 deletions src/components/charts/BarChartComponent/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const getSvg = className => {

container.append("g").classed("x-axis", true);

container.append("g").classed("y0-axis", true);

container.append("g").classed("y-axis", true);

container.append("g").classed("datasets", true);
Expand Down

0 comments on commit f6915d3

Please sign in to comment.