Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
Fix grid axis appearing on top of the lines (#197)
Browse files Browse the repository at this point in the history
* Fix grid appearing on top of lines

* Update snapshots

* Add GridAxis to Bar and Scatterplot
  • Loading branch information
Minishlink authored and marzolfb committed Oct 1, 2017
1 parent bff54fe commit 3267c55
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 117 deletions.
16 changes: 1 addition & 15 deletions src/Axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { fontAdapt } from './util'
import _ from 'lodash'
const Pathjs = require('paths-js/path')

class AxisStruct {
export class AxisStruct {

constructor(scale, options, chartArea, horizontal) {
this.scale = scale
Expand Down Expand Up @@ -129,10 +129,6 @@ export default class Axis extends Component {
options.color = '#3E90F0'
}

if (typeof options.gridColor !== 'string') {
options.gridColor = '#3E90F0'
}

if (typeof options.opacity !== 'number') {
options.opacity = 0.5
}
Expand Down Expand Up @@ -179,13 +175,6 @@ export default class Axis extends Component {
return returnValue
})


const gridLines = options.showLines ? _.map(axis.lines, function (c, i) {
return (
<Path key={'gridLines' + i} d={c.print()} strokeOpacity={options.opacity} stroke={options.gridColor} fill="none"/>
)
}) : []

let offset = {
x: chartArea.margin.left * -1,
y: chartArea.margin.top * -1
Expand All @@ -198,9 +187,6 @@ export default class Axis extends Component {
{options.showAxis ? <Path d={axis.path.print()} strokeOpacity={options.opacity} stroke={options.color} strokeWidth={options.strokeWidth} fill="none"/> : null}
</G>
{ticks}
<G x={offset.x} y={offset.y}>
{gridLines}
</G>
</G>

return returnV
Expand Down
4 changes: 3 additions & 1 deletion src/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Svg,{ G, Path, Text } from 'react-native-svg'
import { Colors, Options, fontAdapt, cyclic, color, identity } from './util'
import _ from 'lodash'
import Axis from './Axis'
import GridAxis from './GridAxis'
const Bar = require('paths-js/bar')
import 'babel-polyfill'

Expand Down Expand Up @@ -147,8 +148,9 @@ export default class BarChart extends Component {

return (<Svg width={options.width} height={options.height}>
<G x={options.margin.left} y={options.margin.top}>
<Axis scale={chart.scale} options={options.axisY} chartArea={chartArea} />
<GridAxis scale={chart.scale} options={options.axisY} chartArea={chartArea} />
{lines}
<Axis scale={chart.scale} options={options.axisY} chartArea={chartArea} />
</G>
</Svg>)
}
Expand Down
58 changes: 58 additions & 0 deletions src/GridAxis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2016 Capital One Services, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
SPDX-Copyright: Copyright (c) Capital One Services, LLC
SPDX-License-Identifier: Apache-2.0
*/

import React, { Component } from 'react'
import { G, Path } from 'react-native-svg'
import _ from 'lodash'
import { AxisStruct } from './Axis'

export default class GridAxis extends Component {

render() {
const { chartArea, options, scale } = this.props
const horizontal = options.orient ==='top' || options.orient ==='bottom'

const axis = new AxisStruct(scale,options,chartArea,horizontal).axis()

if (typeof options.gridColor !== 'string') {
options.gridColor = '#3E90F0'
}

if (typeof options.opacity !== 'number') {
options.opacity = 0.5
}

const gridLines = options.showLines ? _.map(axis.lines, function (c, i) {
return (
<Path key={'gridLines' + i} d={c.print()} strokeOpacity={options.opacity} stroke={options.gridColor} fill="none"/>
)
}) : []

let offset = {
x: chartArea.margin.left * -1,
y: chartArea.margin.top * -1
};

let returnV = <G x={offset.x} y={offset.y}>
{gridLines}
</G>;

return returnV

}
}
7 changes: 5 additions & 2 deletions src/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Text as ReactText } from 'react-native';
import Svg, { G, Path, Rect, Text, Circle } from 'react-native-svg';
import { Colors, Options, cyclic, fontAdapt } from './util';
import Axis from './Axis';
import GridAxis from './GridAxis';
import _ from 'lodash';

export default class LineChart extends Component {
Expand Down Expand Up @@ -255,12 +256,14 @@ export default class LineChart extends Component {
let returnValue = (
<Svg width={options.width} height={options.height}>
<G x={options.margin.left} y={options.margin.top}>
<GridAxis key="grid-x" scale={chart.xscale} options={options.axisX} chartArea={chartArea} />
<GridAxis key="grid-y" scale={chart.yscale} options={options.axisY} chartArea={chartArea} />
{regions}
{areas}
{lines}
{points}
<Axis key="x" scale={chart.xscale} options={options.axisX} chartArea={chartArea} />
<Axis key="y" scale={chart.yscale} options={options.axisY} chartArea={chartArea} />
<Axis key="axis-x" scale={chart.xscale} options={options.axisX} chartArea={chartArea} />
<Axis key="axis-y" scale={chart.yscale} options={options.axisY} chartArea={chartArea} />
</G>
</Svg>
);
Expand Down
3 changes: 3 additions & 0 deletions src/Scatterplot.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Text as ReactText} from 'react-native'
import Svg,{ Circle, G } from 'react-native-svg'
import { Options, styleSvg } from './util'
import Axis from './Axis'
import GridAxis from './GridAxis'
import _ from 'lodash'
import 'babel-polyfill'

Expand Down Expand Up @@ -139,6 +140,8 @@ export default class Scatterplot extends Component {

return (<Svg width={options.width} height={options.height}>
<G x={options.margin.left} y={options.margin.top}>
<GridAxis scale={chart.xscale} options={options.axisX} chartArea={chartArea} />
<GridAxis scale={chart.yscale} options={options.axisY} chartArea={chartArea} />
{ points }
<Axis scale={chart.xscale} options={options.axisX} chartArea={chartArea} />
<Axis scale={chart.yscale} options={options.axisY} chartArea={chartArea} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`test renders an example chart correctly 1`] = `
mockedComponent="svg-G"
x={45}
y={20}>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
<view
d="M NaN NaN C NaN NaN NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN S NaN NaN NaN NaN L NaN NaN L NaN NaN Z "
fill="#3199de"
Expand Down Expand Up @@ -49,10 +57,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
</view>
<view
mockedComponent="svg-G">
Expand All @@ -68,10 +72,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
</view>
</view>
</view>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`test renders an example chart correctly 1`] = `
mockedComponent="svg-G"
x={45}
y={20}>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
<view
mockedComponent="svg-G">
<view
Expand Down Expand Up @@ -187,10 +195,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
</view>
<view
mockedComponent="svg-G">
Expand All @@ -206,10 +210,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
</view>
</view>
</view>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`test renders an example chart correctly 1`] = `
mockedComponent="svg-G"
x={45}
y={20}>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
<view
mockedComponent="svg-G">
<view
Expand Down Expand Up @@ -187,10 +195,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
</view>
<view
mockedComponent="svg-G">
Expand All @@ -206,10 +210,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-45}
y={-20} />
</view>
</view>
</view>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`test renders an example chart correctly 1`] = `
mockedComponent="svg-G"
x={40}
y={20}>
<view
mockedComponent="svg-G"
x={-40}
y={-20} />
<view
mockedComponent="svg-G"
x={-40}
y={-20} />
<view
mockedComponent="svg-G"
x={NaN}
Expand Down Expand Up @@ -637,10 +645,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-40}
y={-20} />
</view>
<view
mockedComponent="svg-G">
Expand All @@ -656,10 +660,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-40}
y={-20} />
</view>
</view>
</view>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`test renders an example chart correctly 1`] = `
mockedComponent="svg-G"
x={35}
y={10}>
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
<view
d="M NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN Z "
fill="#3199de"
Expand Down Expand Up @@ -63,10 +71,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
</view>
<view
mockedComponent="svg-G">
Expand All @@ -82,10 +86,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
</view>
</view>
</view>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ exports[`test renders an example chart correctly 1`] = `
mockedComponent="svg-G"
x={35}
y={10}>
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
<view
d="M NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN L NaN NaN Z "
fill="#3199de"
Expand Down Expand Up @@ -35,10 +43,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
</view>
<view
mockedComponent="svg-G">
Expand All @@ -54,10 +58,6 @@ exports[`test renders an example chart correctly 1`] = `
strokeOpacity={0.5}
strokeWidth={3} />
</view>
<view
mockedComponent="svg-G"
x={-35}
y={-10} />
</view>
</view>
</view>
Expand Down
Loading

0 comments on commit 3267c55

Please sign in to comment.