2018-09-20 18:06:23 +02:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2018-09-20 18:06:23 +02:00
|
|
|
import * as d3 from 'd3'
|
2018-10-23 19:10:27 +02:00
|
|
|
import {
|
|
|
|
generateChart,
|
|
|
|
getCoordinateData,
|
|
|
|
handleChartUpdate,
|
|
|
|
hideDataUI,
|
|
|
|
setTickPosition,
|
2018-10-24 00:36:36 +02:00
|
|
|
handleMouseMove,
|
2018-10-23 19:10:27 +02:00
|
|
|
} from './gas-price-chart.utils.js'
|
2018-10-10 18:06:38 +02:00
|
|
|
|
2018-09-20 18:06:23 +02:00
|
|
|
export default class GasPriceChart extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
2018-09-20 18:06:23 +02:00
|
|
|
static propTypes = {
|
2018-10-23 19:10:27 +02:00
|
|
|
gasPrices: PropTypes.array,
|
|
|
|
estimatedTimes: PropTypes.array,
|
|
|
|
gasPricesMax: PropTypes.number,
|
2018-11-27 18:30:41 +01:00
|
|
|
estimatedTimesMax: PropTypes.number,
|
2018-10-10 18:06:38 +02:00
|
|
|
currentPrice: PropTypes.number,
|
|
|
|
updateCustomGasPrice: PropTypes.func,
|
2018-09-20 18:06:23 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 19:10:27 +02:00
|
|
|
renderChart ({
|
|
|
|
currentPrice,
|
|
|
|
gasPrices,
|
|
|
|
estimatedTimes,
|
|
|
|
gasPricesMax,
|
|
|
|
estimatedTimesMax,
|
|
|
|
updateCustomGasPrice,
|
|
|
|
}) {
|
2018-10-24 00:36:36 +02:00
|
|
|
const chart = generateChart(gasPrices, estimatedTimes, gasPricesMax, estimatedTimesMax)
|
2018-10-03 15:20:05 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
setTickPosition('y', 0, -5, 8)
|
2018-10-10 18:06:38 +02:00
|
|
|
setTickPosition('y', 1, -3, -5)
|
2018-11-13 18:36:35 +01:00
|
|
|
setTickPosition('x', 0, 3)
|
2018-10-10 18:06:38 +02:00
|
|
|
setTickPosition('x', 1, 3, -8)
|
2018-10-03 15:20:05 +02:00
|
|
|
|
2018-11-13 18:36:35 +01:00
|
|
|
const { x: domainX } = getCoordinateData('.domain')
|
|
|
|
const { x: yAxisX } = getCoordinateData('.c3-axis-y-label')
|
|
|
|
const { x: tickX } = getCoordinateData('.c3-axis-x .tick')
|
|
|
|
|
|
|
|
d3.select('.c3-axis-x .tick').attr('transform', 'translate(' + (domainX - tickX) / 2 + ', 0)')
|
2018-09-20 18:06:23 +02:00
|
|
|
d3.select('.c3-axis-x-label').attr('transform', 'translate(0,-15)')
|
2018-11-13 18:36:35 +01:00
|
|
|
d3.select('.c3-axis-y-label').attr('transform', 'translate(' + (domainX - yAxisX - 12) + ', 2) rotate(-90)')
|
2018-10-03 15:20:05 +02:00
|
|
|
d3.select('.c3-xgrid-focus line').attr('y2', 98)
|
|
|
|
|
|
|
|
d3.select('.c3-chart').on('mouseout', () => {
|
2018-10-23 19:10:27 +02:00
|
|
|
hideDataUI(chart, '#overlayed-circle')
|
2018-10-03 15:20:05 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
d3.select('.c3-chart').on('click', () => {
|
2018-10-23 19:10:27 +02:00
|
|
|
const { x: newGasPrice } = d3.select('#overlayed-circle').datum()
|
|
|
|
updateCustomGasPrice(newGasPrice)
|
2018-10-03 15:20:05 +02:00
|
|
|
})
|
|
|
|
|
2018-10-23 19:10:27 +02:00
|
|
|
const { x: chartXStart, width: chartWidth } = getCoordinateData('.c3-areas-data1')
|
2018-10-10 18:06:38 +02:00
|
|
|
|
2018-10-24 00:36:36 +02:00
|
|
|
handleChartUpdate({
|
2018-10-23 19:10:27 +02:00
|
|
|
chart,
|
|
|
|
gasPrices,
|
|
|
|
newPrice: currentPrice,
|
|
|
|
cssId: '#set-circle',
|
|
|
|
})
|
2018-10-03 15:20:05 +02:00
|
|
|
|
2018-10-23 19:10:27 +02:00
|
|
|
d3.select('.c3-chart').on('mousemove', function () {
|
2018-10-24 00:36:36 +02:00
|
|
|
handleMouseMove({
|
2018-10-23 19:10:27 +02:00
|
|
|
xMousePos: d3.event.clientX,
|
|
|
|
chartXStart,
|
|
|
|
chartWidth,
|
|
|
|
gasPrices,
|
|
|
|
estimatedTimes,
|
2018-10-24 00:36:36 +02:00
|
|
|
chart,
|
2018-10-03 15:20:05 +02:00
|
|
|
})
|
|
|
|
})
|
2018-09-20 18:06:23 +02:00
|
|
|
}, 0)
|
2018-10-03 15:20:05 +02:00
|
|
|
|
2018-10-10 18:06:38 +02:00
|
|
|
this.chart = chart
|
|
|
|
}
|
2018-10-03 15:20:05 +02:00
|
|
|
|
2018-10-10 18:06:38 +02:00
|
|
|
componentDidUpdate (prevProps) {
|
2018-10-23 19:10:27 +02:00
|
|
|
const { gasPrices, currentPrice: newPrice } = this.props
|
|
|
|
|
|
|
|
if (prevProps.currentPrice !== newPrice) {
|
2018-10-24 00:36:36 +02:00
|
|
|
handleChartUpdate({
|
2018-10-10 18:06:38 +02:00
|
|
|
chart: this.chart,
|
2018-10-23 19:10:27 +02:00
|
|
|
gasPrices,
|
|
|
|
newPrice,
|
|
|
|
cssId: '#set-circle',
|
2018-10-10 18:06:38 +02:00
|
|
|
})
|
|
|
|
}
|
2018-09-20 18:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
2018-10-23 19:10:27 +02:00
|
|
|
this.renderChart(this.props)
|
2018-09-20 18:06:23 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 18:06:23 +02:00
|
|
|
render () {
|
|
|
|
return (
|
2018-09-20 18:06:23 +02:00
|
|
|
<div className="gas-price-chart" id="container">
|
|
|
|
<div className="gas-price-chart__root" id="chart"></div>
|
2018-09-20 18:06:23 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|