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

feat: funnel chart add conversion and arrival statistics #1488

Merged
merged 1 commit into from
Feb 24, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface ILabelConfig {
| 'indicatorName'
| 'indicatorValue'
| 'percentage'
| 'conversion'
| 'arrival'
>
pieLabelPosition?: string
funnelLabelPosition?: string
Expand All @@ -49,6 +51,8 @@ export class LabelSection extends React.PureComponent<ILabelSectionProps, {}> {
value: 'indicatorValue',
charts: ['pie', 'funnel', 'radar']
},
{ label: '转化率', value: 'conversion', charts: ['funnel'] },
{ label: '到达率', value: 'arrival', charts: ['funnel'] },
{ label: '百分比', value: 'percentage', charts: ['pie', 'funnel'] }
]

Expand Down Expand Up @@ -183,7 +187,6 @@ export class LabelSection extends React.PureComponent<ILabelSectionProps, {}> {
gutter={8}
type="flex"
align="middle"
className={styles.blockRow}
>
<Col span={24}>
<CheckboxGroup
Expand Down
2 changes: 1 addition & 1 deletion webapp/app/containers/Widget/config/chart/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const funnel: IChartInfo = {
labelFontFamily: PIVOT_CHART_FONT_FAMILIES[0].value,
labelFontSize: '12',
labelColor: PIVOT_DEFAULT_FONT_COLOR,
labelParts: ['dimensionValue', 'indicatorValue', 'percentage']
labelParts: ['dimensionValue', 'indicatorValue']
},
legend: {
showLegend: true,
Expand Down
140 changes: 85 additions & 55 deletions webapp/app/containers/Widget/render/chart/funnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function (chartProps: IChartProps, drillOptions?: any) {

let seriesObj = {}
const seriesArr = []
let legendData = []
const legendData = []
let grouped: { [key: string]: object[] } = {}

if (metrics.length <= 1) {
Expand All @@ -81,6 +81,7 @@ export default function (chartProps: IChartProps, drillOptions?: any) {

metrics.forEach((metric) => {
const decodedMetricName = decodeMetricName(metric.name)
const metricNameWithAgg = `${metric.agg}(${decodedMetricName})`

const seriesData = []
Object.entries(grouped).forEach(([key, value]) => {
Expand All @@ -89,21 +90,21 @@ export default function (chartProps: IChartProps, drillOptions?: any) {
value.forEach((v) => {
const obj = {
name: legendStr,
value: v[`${metric.agg}(${decodedMetricName})`]
value: v[metricNameWithAgg]
}
seriesData.push(obj)
})
})

const maxValue = Math.max(
...data.map((s) => s[`${metric.agg}(${decodedMetricName})`])
...data.map((s) => s[metricNameWithAgg])
)
const minValue = Math.min(
...data.map((s) => s[`${metric.agg}(${decodedMetricName})`])
...data.map((s) => s[metricNameWithAgg])
)

const numValueArr = data.map(
(d) => d[`${metric.agg}(${decodedMetricName})`] >= 0
(d) => d[metricNameWithAgg] >= 0
)
const minSizePer = (minValue / maxValue) * 100
const minSizeValue =
Expand Down Expand Up @@ -153,25 +154,25 @@ export default function (chartProps: IChartProps, drillOptions?: any) {
width: widthValue,
height: heightValue,
color: colorArr,
// data: seriesData,
data: seriesData.map((data, index) => {
const itemStyleObj =
selectedItems &&
selectedItems.length &&
selectedItems.some((item) => item === index)
? {
itemStyle: {
normal: {
opacity: 1
data: getFunnelSeriesData(seriesData)
.map((data, index) => {
const itemStyleObj =
selectedItems &&
selectedItems.length &&
selectedItems.some((item) => item === index)
? {
itemStyle: {
normal: {
opacity: 1
}
}
}
}
: {}
return {
...data,
...itemStyleObj
}
}),
: {}
return {
...data,
...itemStyleObj
}
}),
itemStyle: {
emphasis: {
shadowBlur: 10,
Expand All @@ -188,7 +189,16 @@ export default function (chartProps: IChartProps, drillOptions?: any) {
seriesArr.push(seriesObj)
})
} else {
legendData = []
const seriesData = []
metrics.forEach((metric) => {
const decodedMetricName = decodeMetricName(metric.name)
legendData.push(decodedMetricName)
seriesData.push({
name: decodedMetricName,
value: data.reduce((sum, record) => sum + record[`${metric.agg}(${decodedMetricName})`], 0)
})
})

seriesObj = {
type: 'funnel',
sort: sortMode,
Expand All @@ -198,29 +208,25 @@ export default function (chartProps: IChartProps, drillOptions?: any) {
top: height * 0.12,
width: width - width * 0.15 * 2,
height: height - height * 0.12 * 2,
data: metrics.map((metric) => {
const decodedMetricName = decodeMetricName(metric.name)
legendData.push(decodedMetricName)

const itemStyleObj =
selectedItems &&
selectedItems.length &&
selectedItems.some((item) => item === 0)
? {
itemStyle: {
normal: {
opacity: 1
data: getFunnelSeriesData(seriesData)
.map((data, index) => {
const itemStyleObj =
selectedItems &&
selectedItems.length &&
selectedItems.some((item) => item === 0)
? {
itemStyle: {
normal: {
opacity: 1
}
}
}
}
: {}

return {
name: decodedMetricName,
value: data.reduce((sum, record) => sum + record[`${metric.agg}(${decodedMetricName})`], 0),
...itemStyleObj
}
}),
: {}
return {
...data,
...itemStyleObj
}
}),
itemStyle: {
emphasis: {
shadowBlur: 10,
Expand All @@ -239,20 +245,25 @@ export default function (chartProps: IChartProps, drillOptions?: any) {
const tooltip: EChartOption.Tooltip = {
trigger: 'item',
formatter (params: EChartOption.Tooltip.Format) {
const { color, name, value, percent, dataIndex } = params
const { color, name, value, percent, dataIndex, data } = params
const formattedValue = getFormattedValue(
value as number,
metrics[metrics.length > 1 ? dataIndex : 0].format
)
const tooltipLabels = []
let basicInfo = `${name}: ${formattedValue}`
if (color) {
tooltipLabels.push(
`<span class="widget-tooltip-circle" style="background: ${color}"></span>`
)
basicInfo = `<span class="widget-tooltip-circle" style="background: ${color}"></span> ${basicInfo}`
}
tooltipLabels.push(
`${name}<br/>${getFormattedValue(
value as number,
metrics[metrics.length > 1 ? dataIndex : 0].format
)}(${percent}%)`
)
return tooltipLabels.join('')
tooltipLabels.push(basicInfo)
if (data.conversion) {
tooltipLabels.push(`转化率: ${data.conversion}%`)
}
if (data.arrival) {
tooltipLabels.push(`到达率: ${data.arrival}%`)
}
tooltipLabels.push(`百分比: ${percent}%`)
return tooltipLabels.join('<br/>')
}
}

Expand All @@ -262,3 +273,22 @@ export default function (chartProps: IChartProps, drillOptions?: any) {
series: seriesArr
}
}

function getFunnelSeriesData (seriesData) {
return seriesData
.sort((d1, d2) => d2.value - d1.value)
.map((d, index) => {
if (index) {
d.conversion = formatPercent(d.value / seriesData[index - 1].value * 100)
d.arrival = formatPercent(d.value / seriesData[0].value * 100)
}
return d
})
}

function formatPercent (per) {
const perStr = per + ''
return perStr.length - (perStr.indexOf('.') + 1) > 2
? per.toFixed(2)
: perStr
}
18 changes: 12 additions & 6 deletions webapp/app/containers/Widget/render/chart/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,32 @@ export function getLabelOption (type: string, labelConfig: ILabelConfig, metrics
case 'pie':
case 'funnel':
formatter = (params) => {
const { name, value, percent, dataIndex } = params
const { name, value, percent, dataIndex, data } = params
const formattedValue = getFormattedValue(value, metrics[metrics.length > 1 ? dataIndex : 0].format)
const { labelParts } = labelConfig
if (!labelParts) {
return `${name}\n${formattedValue}(${percent}%)`
}
const labels: string[] = []
const multiRate = labelParts
.filter((label) => ['percentage', 'conversion', 'arrival'].includes(label))
.length > 1
if (labelParts.includes('dimensionValue')) {
labels.push(name)
}
if (labelParts.includes('indicatorValue')) {
labels.push(formattedValue)
}
if (labelParts.includes('percentage')) {
labels.push(labels.length ? `(${percent}%)` : `${percent}%`)
if (labelParts.includes('conversion') && data.conversion) {
labels.push(`${multiRate ? '转化率:' : ''}${data.conversion}%`)
}
if (labels.length > 1 && labelParts.includes('dimensionValue')) {
labels.splice(1, 0, '\n')
if (labelParts.includes('arrival') && data.arrival) {
labels.push(`${multiRate ? '到达率:' : ''}${data.arrival}%`)
}
return labels.join('')
if (labelParts.includes('percentage')) {
labels.push(`${multiRate ? '百分比:' : ''}${percent}%`)
}
return labels.join('\n')
}
break
case 'radar':
Expand Down