Draw dot and line with svg

This commit is contained in:
Julia Friesel
2018-09-17 18:33:12 +02:00
parent 961009d74f
commit 7d6e45b1e2
3 changed files with 27 additions and 51 deletions
+1 -1
View File
@@ -191,7 +191,7 @@ export default class DayColumn extends Component {
</View> </View>
</View> </View>
<Svg width={config.columnWidth} height={chartHeight}> <Svg width={config.columnWidth} height={columnHeight}>
{column} {column}
</Svg> </Svg>
+16 -34
View File
@@ -1,5 +1,6 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { View } from 'react-native' import { Circle, Line } from 'react-native-svg'
import styles from './styles' import styles from './styles'
import config from '../../config' import config from '../../config'
@@ -17,21 +18,20 @@ export default class DotAndLine extends Component {
if (this.props.leftY) { if (this.props.leftY) {
const middleY = ((this.props.leftY - y) / 2) + y const middleY = ((this.props.leftY - y) / 2) + y
const excludedLine = this.props.leftTemperatureExclude || exclude const excludedLine = this.props.leftTemperatureExclude || exclude
lineToLeft = makeLine(middleY, y, 'left', excludedLine) lineToLeft = makeLine(y, middleY, 0, excludedLine)
} }
if (this.props.rightY) { if (this.props.rightY) {
const middleY = ((y - this.props.rightY) / 2) + this.props.rightY const middleY = ((y - this.props.rightY) / 2) + this.props.rightY
const excludedLine = this.props.rightTemperatureExclude || exclude const excludedLine = this.props.rightTemperatureExclude || exclude
lineToRight = makeLine(y, middleY, 'right', excludedLine) lineToRight = makeLine(y, middleY, config.columnWidth, excludedLine)
} }
const dotStyle = exclude ? styles.curveDotsExcluded : styles.curveDots const dotStyle = exclude ? styles.curveDotsExcluded : styles.curveDots
const dot = ( const dot = (
<View <Circle
position='absolute' cx={config.columnMiddle}
top={y - (dotStyle.height / 2)} cy={y}
left={config.columnMiddle - (dotStyle.width / 2)} {...dotStyle}
style={dotStyle}
key='dot' key='dot'
/> />
) )
@@ -39,33 +39,15 @@ export default class DotAndLine extends Component {
} }
} }
function makeLine(leftY, rightY, direction, excludeLine) { function makeLine(currY, middleY, x, excludeLine) {
const colWidth = config.columnWidth
const heightDiff = -(leftY - rightY)
const angle = Math.atan2(heightDiff, colWidth / 2)
const lineStyle = excludeLine ? styles.curveExcluded : styles.curve const lineStyle = excludeLine ? styles.curveExcluded : styles.curve
// hypotenuse, we add 3px for good measure, because otherwise the lines
// don't quite touch at the day border
const h = (colWidth / 2) / Math.cos(angle) + 10
// the rotation by default rotates from the middle of the line,
// but we want the transform origin to be at its beginning
// react native doesn't have transformOrigin, so we do this manually
// if it's the right line, we put the pivot at 3/4 of the column
// if it's to the left, at 1/4
const pivot = direction === 'right' ? colWidth / 4 : -(colWidth / 4)
const projectedX = -(h - colWidth) / 2 + pivot
return (<View return <Line
width={h} x1={config.columnMiddle}
position='absolute' y1={currY}
top={((leftY + rightY) / 2) - lineStyle.borderWidth / 2} x2={x}
left={projectedX} y2={middleY}
style={{
transform: [
{ rotateZ: `${angle}rad` }
],
}}
{...lineStyle} {...lineStyle}
key ={direction} key={x.toString()}
/>) />
} }
+10 -16
View File
@@ -3,32 +3,26 @@ import {primaryColor, shadesOfRed} from '../../styles/index'
const colorTemperature = '#765285' const colorTemperature = '#765285'
const colorTemperatureLight = '#a67fb5' const colorTemperatureLight = '#a67fb5'
const dotWidth = 10 const dotRadius = 5
const lineWidth = 2 const lineWidth = 1.5
const colorLtl = '#feb47b' const colorLtl = '#feb47b'
const styles = { const styles = {
curve: { curve: {
borderStyle: 'solid', stroke: colorTemperature,
borderColor: colorTemperature, strokeWidth: lineWidth,
borderWidth: lineWidth,
}, },
curveExcluded: { curveExcluded: {
borderColor: colorTemperatureLight, stroke: colorTemperatureLight,
borderWidth: lineWidth, strokeWidth: lineWidth
borderStyle: 'dotted'
}, },
curveDots: { curveDots: {
backgroundColor: colorTemperature, fill: colorTemperature,
width: dotWidth, r: dotRadius
height: dotWidth,
borderRadius: 50
}, },
curveDotsExcluded: { curveDotsExcluded: {
backgroundColor: colorTemperatureLight, fill: colorTemperatureLight,
width: dotWidth, r: dotRadius
height: dotWidth,
borderRadius: 50
}, },
column: { column: {
label: { label: {