Chart styling updates

pull/2859/head
Uku Taht 4 years ago committed by Adam Rutkowski
parent c9a92a9e14
commit d0c4b2da8e

@ -100,6 +100,10 @@ export default class Conversions extends React.Component {
} else if (this.state.goals) {
return (
<React.Fragment>
<div className="flex justify-between w-full">
<h3 className="font-bold dark:text-gray-100">Goal conversions</h3>
{this.props.tabs}
</div>
<div className="flex items-center justify-between mt-3 mb-2 text-xs font-bold tracking-wide text-gray-500 dark:text-gray-400">
<span>Goal</span>
<div className="text-right">

@ -6,8 +6,6 @@ Chart.register(ChartDataLabels);
import * as api from '../../api'
import LazyLoader from '../../components/lazy-loader'
export default class Funnel extends React.Component {
constructor(props) {
super(props)
@ -30,10 +28,14 @@ export default class Funnel extends React.Component {
}
}
formatDataLabel(visitors) {
const total = this.state.funnel.steps[0].visitors
const percentage = (visitors / total) * 100
return `${visitors} Visitors (${percentage}%)`
formatDataLabel(visitors, ctx) {
if (ctx.dataset.label === 'Visitors') {
const total = this.state.funnel.steps[0].visitors
const percentage = (visitors / total) * 100
return `${percentage}%\n${visitors} Visitors`
} else {
return null
}
}
fetchFunnel() {
@ -46,8 +48,18 @@ export default class Funnel extends React.Component {
if (this.chart) {
this.chart.destroy()
}
const labels = this.state.funnel.steps.map(step => step.label);
const labels = this.state.funnel.steps.map((step, i) => `${i + 1}. ${step.label}`);
const stepData = this.state.funnel.steps.map(step => step.visitors)
const dropOffData = this.state.funnel.steps.map((step, i) => {
const thisVisitors = step.visitors
const prevEntry = this.state.funnel.steps[i - 1]
if (prevEntry) {
return prevEntry.visitors - thisVisitors
} else {
return 0
}
})
const ctx = document.getElementById('funnel').getContext('2d')
var gradient = ctx.createLinearGradient(0, 0, 0, 300);
@ -56,15 +68,22 @@ export default class Funnel extends React.Component {
const data = {
labels: labels,
datasets: [{
label: 'Visitors',
data: stepData,
backgroundColor: [gradient],
borderColor: ['rgba(101,116,205)'],
borderWidth: 1,
borderSkipped: false,
borderRadius: 4
}]
datasets: [
{
label: 'Visitors',
data: stepData,
backgroundColor: ['rgb(99, 102, 241)'],
borderRadius: 4,
stack: 'Stack 0',
},
{
label: 'Dropoff',
data: dropOffData,
backgroundColor: ['rgb(224, 231, 255)'],
borderRadius: 4,
stack: 'Stack 0',
}
],
};
const config = {
@ -79,12 +98,19 @@ export default class Funnel extends React.Component {
display: false,
},
datalabels: {
formatter: this.formatDataLabel.bind(this)
formatter: this.formatDataLabel.bind(this),
anchor: 'end',
backgroundColor: 'rgba(25, 30, 56)',
color: 'rgb(243, 244, 246)',
borderRadius: 4,
font: {size: 14, weight: 'bold'},
textAlign: 'center',
padding: {top: 4, bottom: 4, right: 8, left: 8}
}
},
scales: {
y: { display: false, border: { display: false } },
x: { display: true, border: { display: false }, grid: { drawOnChartArea: false, drawTicks: false }, ticks: { font: { weight: 'bold', size: 14 }, color: 'rgb(75, 85, 99)' } }
y: { display: false},
x: { position: 'top', display: true, border: { display: false }, grid: {drawBorder: false, display: false}, ticks: {padding: 24, font: { weight: 'bold', size: 14 }, color: 'rgb(17, 24, 39)'}}
}
},
};
@ -94,7 +120,7 @@ export default class Funnel extends React.Component {
renderInner() {
if (this.state.loading) {
return <div className="mx-auto my-2 loading"><div></div></div>
return <div className="mx-auto loading pt-44"><div></div></div>
} else if (this.state.funnel) {
const firstStep = this.state.funnel.steps[0].visitors
const lastStep = this.state.funnel.steps[this.state.funnel.steps.length - 1].visitors
@ -102,9 +128,12 @@ export default class Funnel extends React.Component {
return (
<React.Fragment>
<h3 className="font-semibold dark:text-gray-100">{this.state.funnel.name}</h3>
<p className="text-gray-600">{this.state.funnel.steps.length}-step funnel • {conversionRate}% conversion rate</p>
<canvas className="mt-8" id="funnel" height="100px"></canvas>
<div className="flex justify-between w-full">
<h3 className="font-bold dark:text-gray-100">{this.state.funnel.name}</h3>
{this.props.tabs}
</div>
<p className="mt-1 text-gray-500 text-sm">{this.state.funnel.steps.length}-step funnel • {conversionRate}% conversion rate</p>
<canvas className="py-4" id="funnel" height="100px"></canvas>
</React.Fragment>
)
}
@ -112,7 +141,7 @@ export default class Funnel extends React.Component {
render() {
return (
<LazyLoader className="mt-2" onVisible={this.onVisible} ref={this.htmlNode}>
<LazyLoader style={{minHeight: '400px'}} onVisible={this.onVisible} ref={this.htmlNode}>
{this.renderInner()}
</LazyLoader>
)

@ -63,14 +63,12 @@ export default function Behaviours({ query, site }) {
const funnelNames = site.funnels.map(({name}) => name)
const selectedFunnel = site.funnels.find(funnel => funnel.name === tab)
const tabs = <Tabs tab={tab} setTab={setTab} funnelNames={funnelNames} />
return (
<div className="w-full p-4 bg-white rounded shadow-xl dark:bg-gray-825">
<div className="flex justify-between w-full">
<h3 className="font-bold dark:text-gray-100">Behaviors</h3>
<Tabs tab={tab} setTab={setTab} funnelNames={funnelNames} />
</div>
{ tab == 'conversions' && <Conversions query={query} site={site} /> }
{ funnelNames.includes(tab) && <Funnel funnel={selectedFunnel} query={query} site={site} /> }
{ tab == 'conversions' && <Conversions tabs={tabs} query={query} site={site} /> }
{ funnelNames.includes(tab) && <Funnel tabs={tabs} funnel={selectedFunnel} query={query} site={site} /> }
</div>
)
}

@ -55,6 +55,7 @@ class LineGraph extends React.Component {
animation: false,
plugins: {
legend: { display: false },
datalabels: { display: false},
tooltip: {
enabled: false,
mode: 'index',

Loading…
Cancel
Save