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

Commit

Permalink
Draw additional x-axis line at y-0 if negative values are available #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wiadev committed Dec 15, 2017
1 parent 5b4b2f2 commit b9be3ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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
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
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 b9be3ba

Please sign in to comment.