Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/chart 5 - creating custom tooltips #162

Open
wants to merge 4 commits into
base: stage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"axios-extensions": "^3.1.3",
"canvg": "^3.0.7",
"core-js": "^3.6.5",
"date-fns": "^2.27.0",
"decimal.js-light": "^2.5.1",
"js-base64": "^3.6.1",
"lightweight-charts": "^3.7.0",
Expand Down
119 changes: 117 additions & 2 deletions src/components/UI/SwapChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,48 @@
</template>

<script>
import { createChart } from 'lightweight-charts'
import { isBusinessDay, createChart } from 'lightweight-charts'
import { format } from 'date-fns'

const WIDTH_PRICE_BAR = 54
const tooltipsConfiguration = [
{
name: 'time',
positionName: 'left',
tooltipWidth: 93,
// TODO: Подключить сторонее апи с курсом криптовалют и добавить обработку времени
formatDate({ day, month, year }) {
return format(new Date(year, month, day), 'dd MMM. yy')
tany31 marked this conversation as resolved.
Show resolved Hide resolved
},
getData(param, { widthChartContainer }) {
const dateStr = isBusinessDay(param.time)
? this.formatDate(param.time)
: new Date(param.time * 1000).toLocaleDateString()

let left = param.point.x - this.tooltipWidth / 2
left = Math.max(0, Math.min(widthChartContainer - this.tooltipWidth, left))

return {
text: `${dateStr}`,
position: `${left + WIDTH_PRICE_BAR}px`
}
}
},
{
name: 'price',
positionName: 'top',
tooltipHeight: 16,
getData(param, { areaSeries }) {
const price = param.seriesPrices.get(areaSeries)
const coordinate = Math.round(areaSeries.priceToCoordinate(price))

return {
text: `$${price}`,
position: `${coordinate - this.tooltipHeight / 2}px`
}
}
}
]
export default {
name: 'SwapChart',
props: {
Expand All @@ -27,7 +67,8 @@ export default {
data() {
return {
chart: null,
areaSeries: null
areaSeries: null,
bindedMoveTooltip: null
}
},
watch: {
Expand All @@ -45,8 +86,47 @@ export default {
this.chart = createChart(this.$refs.swapChart, this.chartOptions)
this.areaSeries = this.chart.addAreaSeries(this.areaStyleOptions)
this.areaSeries.setData(this.datasets)

this.createTooltips()
},
destroyed() {
this.chart.unsubscribeCrosshairMove(this.bindedMoveTooltip)
},
methods: {
createTooltips() {
tooltipsConfiguration.forEach(tooltipConf => {
const tooltipNode = document.createElement('div')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

рендер тултипом можно было реализовать через vue, т.е. описать в template

останется только придумать как правильно обработчики добавлять для графика

tooltipNode.className = `swap-chart__tooltip swap-chart__tooltip--${tooltipConf.name}`
this.$refs.swapChart.appendChild(tooltipNode)
this.bindedMoveTooltip = this.movementOfTooltip.bind(this, { tooltipNode, tooltipConf })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

когда несколько конфигов (а их сейчас несколько), то перетирается метод bindedMoveTooltip

по итогу, в destroyed - очищается обработчик для последнего метода только

this.chart.subscribeCrosshairMove(this.bindedMoveTooltip)
})
},
movementOfTooltip(options, param) {
const { tooltipNode, tooltipConf } = options
const widthChartContainer = this.$refs.swapChart.clientWidth
const heightChartContainer = this.$refs.swapChart.clientHeight
// We check to what limits it is necessary to draw the tooltip
if (
!param.time ||
param.point.x < 0 ||
param.point.y > widthChartContainer ||
param.point.y < 0 ||
param.point.y > heightChartContainer
) {
tooltipNode.style.display = 'none'
return
}

const tooltipData = tooltipConf.getData(param, {
widthChartContainer,
areaSeries: this.areaSeries
})

tooltipNode.style.display = 'block'
tooltipNode.innerHTML = tooltipData.text
tooltipNode.style[tooltipConf.positionName] = tooltipData.position
},
updateChartOptions(newOptions) {
this.chart.applyOptions(newOptions)
},
Expand All @@ -62,6 +142,7 @@ export default {

<style lang="scss">
.swap-chart {
position: relative;
&__no-data {
position: absolute;
left: 50%;
Expand All @@ -73,5 +154,39 @@ export default {
text-align: center;
z-index: 1;
}

&__tooltip {
position: absolute;
display: none;
font-family: $--font-base;
font-weight: $--font-weight-semi-bold;
padding: 2px 4px;
font-size: 9px;
line-height: 12px;
border-radius: 22px;
text-align: center;
z-index: 1000;
bottom: 10px;
pointer-events: none;

.theme--light & {
background-color: $--black;
color: $--white;
}

.theme--dark & {
background-color: $--white;
color: $--black;
}

&--time {
width: 93px;
}

&--price {
width: 48px;
height: 16px;
}
}
}
</style>
29 changes: 25 additions & 4 deletions src/components/Wallets/WalletChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const GRID_COLOR = {
[LIGHT_THEME_KEY]: '#f6f6f6'
}

const CROSSHAIR = {
[DARK_THEME_KEY]: '#f6f6f6',
[LIGHT_THEME_KEY]: '#101010'
}

const COLOR_GRADIENT = {
top: {
[DARK_THEME_KEY]: 'rgba(97, 68, 229, 1)',
Expand Down Expand Up @@ -85,6 +90,26 @@ export default {
return `$${price.toFixed(2)}`
}
},
timeScale: {
timeVisible: true,
borderVisible: false
},
crosshair: {
horzLine: {
visible: true,
style: 3,
width: 1,
color: CROSSHAIR[currentTheme],
labelVisible: false
},
vertLine: {
visible: true,
style: 3,
width: 1,
color: CROSSHAIR[currentTheme],
labelVisible: false
}
},
grid: {
vertLines: {
color: GRID_COLOR[currentTheme]
Expand All @@ -99,10 +124,6 @@ export default {
leftPriceScale: {
visible: true,
borderVisible: false
},
timeScale: {
timeVisible: true,
borderVisible: false
}
}

Expand Down
125 changes: 64 additions & 61 deletions src/components/Wallets/WalletInfo.vue
Original file line number Diff line number Diff line change
@@ -1,73 +1,76 @@
<template>
<div class="wallet-info" :class="{ 'wallet-info--compressed': compressed }" @click="uncompressWallet">
<coin-logo v-show="!isChartView" class="wallet-info__background-icon" :path="logo" :name="coin" />

<header class="wallet-info__header " :class="[isChartView && 'wallet-info__header--coin-price-chart']">
<div v-if="!isChartView" class="wallet-info__crypto-value-wrapper">
<span class="wallet-info__crypto-value">
{{ value }} <span class="grey--text">{{ coin }}</span>
</span>
<span class="wallet-info__fiat-value">3000.04 USD</span>
</div>
<h3 v-else class="wallet-info__coin-price-chart-title">{{ coin }} Price Chart</h3>
<chart-date-filters
v-if="isChartView"
:date-range="chartDateRange"
:options="$options.TIME_PERIOD_CHART"
@change="changeChartDateRange"
/>
<div class="wallet-info__optional-buttons">
<div class="wallet-info__chart-switcher-wrapper">
<swap-switch v-model="isChartView" label="Price chart"></swap-switch>
<match-media v-slot="{ phone }" tag="div">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно было использовать inject, чтобы не вносить много изменений

<div class="wallet-info" :class="{ 'wallet-info--compressed': compressed }" @click="uncompressWallet">
<coin-logo v-show="!isChartView" class="wallet-info__background-icon" :path="logo" :name="coin" />

<header class="wallet-info__header " :class="[isChartView && 'wallet-info__header--coin-price-chart']">
<div v-if="!isChartView" class="wallet-info__crypto-value-wrapper">
<span class="wallet-info__crypto-value">
{{ value }} <span class="grey--text">{{ coin }}</span>
</span>
<span class="wallet-info__fiat-value">3000.04 USD</span>
</div>
<h3 v-else class="wallet-info__coin-price-chart-title">{{ coin }} Price Chart</h3>
<chart-date-filters
v-if="isChartView && !phone"
:date-range="chartDateRange"
:options="$options.TIME_PERIOD_CHART"
@change="changeChartDateRange"
/>
<div class="wallet-info__optional-buttons">
<div class="wallet-info__chart-switcher-wrapper">
<swap-switch v-model="isChartView" label="Price chart"></swap-switch>
</div>

<div class="wallet-info__dividing-line"></div>

<swap-button fab small class="wallet-info__optional-button" @click="openShareModal">
<v-icon class="wallet-info__icon">mdi-export-variant</v-icon>
</swap-button>
<swap-button fab small class="wallet-info__optional-button" @click="openSettingsModal">
<v-icon class="wallet-info__icon">mdi-tune</v-icon>
</swap-button>
</div>
</header>

<div v-if="!isChartView" class="wallet-info__main">
<div class="wallet-info__address-wrapper">
<swap-copy-wrapper>
<template #default="{ copy, tooltopOn }">
<button
v-ripple="{ center: true }"
class="wallet-info__button-copy"
tabindex="-1"
@click="mediaQueries.desktop ? copy(address) : openCopyMenu()"
v-on="tooltopOn"
>
<span class="wallet-info__address">{{ mediaQueries.desktop ? address : minifiedAddress }}</span>
<svg-icon class="wallet-info__icon-copy" name="copy" />
</button>
</template>
</swap-copy-wrapper>

<button class="wallet-info__button-qrcode" @click="openShareModal">
<svg-icon class="wallet-info__icon-qrcode" name="qrcode"></svg-icon>
</button>
</div>

<div class="wallet-info__dividing-line"></div>

<swap-button fab small class="wallet-info__optional-button" @click="openShareModal">
<v-icon class="wallet-info__icon">mdi-export-variant</v-icon>
</swap-button>
<swap-button fab small class="wallet-info__optional-button" @click="openSettingsModal">
<v-icon class="wallet-info__icon">mdi-tune</v-icon>
</swap-button>
</div>
</header>

<div v-if="!isChartView" class="wallet-info__main">
<div class="wallet-info__address-wrapper">
<swap-copy-wrapper>
<template #default="{ copy, tooltopOn }">
<button
v-ripple="{ center: true }"
class="wallet-info__button-copy"
tabindex="-1"
@click="mediaQueries.desktop ? copy(address) : openCopyMenu()"
v-on="tooltopOn"
>
<span class="wallet-info__address">{{ mediaQueries.desktop ? address : minifiedAddress }}</span>
<svg-icon class="wallet-info__icon-copy" name="copy" />
</button>
</template>
</swap-copy-wrapper>

<button class="wallet-info__button-qrcode" @click="openShareModal">
<svg-icon class="wallet-info__icon-qrcode" name="qrcode"></svg-icon>
</button>
<div class="wallet-info__buttons">
<swap-button class="wallet-info__button" @click="openInvoiceBlock">Invoice</swap-button>
<swap-button :to="{ name: 'Swap' }" class="wallet-info__button">Swap</swap-button>
<swap-button class="wallet-info__button" @click="openSendForm">Send</swap-button>
</div>
</div>

<div class="wallet-info__buttons">
<swap-button class="wallet-info__button" @click="openInvoiceBlock">Invoice</swap-button>
<swap-button :to="{ name: 'Swap' }" class="wallet-info__button">Swap</swap-button>
<swap-button class="wallet-info__button" @click="openSendForm">Send</swap-button>
<div v-else class="wallet-info__coin-price-chart-container">
<wallet-chart v-bind="{ datasets }"></wallet-chart>
</div>
</div>

<div v-else class="wallet-info__coin-price-chart-container">
<wallet-chart v-bind="{ datasets }"></wallet-chart>
</div>
</div>
</match-media>
</template>

<script>
import { MatchMedia } from 'vue-component-media-queries'
import { mapMutations } from 'vuex'
import { ADD_MODAL } from '@/store/modules/Modals'
import { COPY_MENU, INVOICE_FORM, SEND_FORM, SHARE_MODAL, WALLET_SETTINGS } from '@/store/modules/Modals/names'
Expand All @@ -94,7 +97,7 @@ const CHART_DATE_RANGE_MAP = {
export default {
TIME_PERIOD_CHART,
name: 'WalletInfo',
components: { CoinLogo, WalletChart, ChartDateFilters },
components: { MatchMedia, CoinLogo, WalletChart, ChartDateFilters },
inject: ['mediaQueries'],
props: {
compressed: { type: Boolean, default: false },
Expand Down