Skip to content

Commit e86efda

Browse files
authored
Custom label position (#234)
1 parent f5734d3 commit e86efda

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

docs/modules/ROOT/pages/user-guide/reports/bar-chart.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ groups returned by the Cypher query.
6767
|Show Values on Bars |on/off |off |If enabled, shows the category value
6868
inside the respective bar.
6969

70+
|Skip label on width (px) |number |0 |Skip label if bar width is lower than provided value, ignored if 0.
71+
72+
|Skip label on height (px) |number |0 |Skip label if bar height is lower than provided value, ignored if 0.
73+
74+
|Custom label position |off/top/bottom |off | Allow user to place label out of the bar. This will override any other
75+
label configuration.
76+
7077
|Label Rotation (degrees) |number |45 |the angle at which the bar labels
7178
are rotated.
7279

src/chart/bar/BarChart.tsx

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ const NeoBarChart = (props: ChartProps) => {
8080
const marginBottom = (settings["marginBottom"]) ? settings["marginBottom"] : 40;
8181
const legend = (settings["legend"]) ? settings["legend"] : false;
8282
const labelRotation = (settings["labelRotation"] != undefined) ? settings["labelRotation"] : 45;
83-
const labelSkipSize = (settings["barValues"]) ? 1 : 2000;
83+
84+
const labelSkipWidth = (settings["labelSkipWidth"]) ? (settings["labelSkipWidth"]) : 0;
85+
const labelSkipHeight = (settings["labelSkipHeight"]) ? (settings["labelSkipHeight"]) : 0;
86+
const enableLabel = (settings["barValues"]) ? settings["barValues"] : false;
87+
const positionLabel = (settings["positionLabel"]) ? settings["positionLabel"] : 'off';
88+
89+
const layout = (settings["layout"]) ? settings["layout"] : 'vertical';
8490
const colorScheme = (settings["colors"]) ? settings["colors"] : 'set2';
8591
const groupMode = (settings["groupMode"]) ? settings["groupMode"] : 'stacked';
8692
const valueScale = (settings["valueScale"]) ? settings["valueScale"] : 'linear';
@@ -107,11 +113,69 @@ const NeoBarChart = (props: ChartProps) => {
107113
return <NoDrawableDataErrorMessage />
108114
}
109115

116+
const BarComponent = ({ bar, borderColor }) => {
117+
let shade = false;
118+
let darkTop = false;
119+
let includeIndex = false;
120+
let x = bar.width/ 2,y = bar.height / 2, textAnchor = "middle";
121+
if (positionLabel == "top")
122+
if(layout == "vertical")
123+
y = - 10 ;
124+
else
125+
x = bar.width + 10;
126+
else if (positionLabel == "bottom")
127+
if(layout == "vertical")
128+
y = bar.height + 10;
129+
else
130+
x = - 10 ;
131+
132+
return (
133+
<g transform={`translate(${bar.x},${bar.y})`}>
134+
{shade ? <rect x={-3} y={7} width={bar.width} height={bar.height} fill="rgba(0, 0, 0, .07)" /> : <></>}
135+
<rect width={bar.width} height={bar.height} fill={bar.color} />
136+
{ darkTop ? <rect
137+
x={bar.width - 5}
138+
width={5}
139+
height={bar.height}
140+
fill={borderColor}
141+
fillOpacity={0.2}
142+
/> : <></> }
143+
{includeIndex ? <text
144+
x={bar.width - 16}
145+
y={bar.height / 2}
146+
textAnchor="end"
147+
dominantBaseline="central"
148+
fill="black"
149+
style={{
150+
fontWeight: 900,
151+
fontSize: 15,
152+
}}
153+
>
154+
{bar.data.indexValue}
155+
</text> : <></> }
156+
{ enableLabel ? <text
157+
x={x}
158+
y={y}
159+
textAnchor={textAnchor}
160+
dominantBaseline="central"
161+
fill={borderColor}
162+
style={{
163+
fontWeight: 400,
164+
fontSize: 13,
165+
}}
166+
>
167+
{bar.data.value}
168+
</text> : <></> }
169+
</g>
170+
)
171+
}
110172
// TODO: Get rid of duplicate pie slice names...
111173

174+
const extraProperties = positionLabel == "off" ? {} : { barComponent : BarComponent };
112175
return <ResponsiveBar
113-
layout={settings.layout == "horizontal" ? 'horizontal' : 'vertical'}
176+
layout={layout}
114177
groupMode={groupMode == "stacked" ? 'stacked' : 'grouped'}
178+
enableLabel = {enableLabel}
115179
data={data}
116180
keys={Object.keys(keys)}
117181
indexBy="index"
@@ -133,9 +197,10 @@ const NeoBarChart = (props: ChartProps) => {
133197
tickPadding: 5,
134198
tickRotation: 0,
135199
}}
136-
labelSkipWidth={labelSkipSize}
137-
labelSkipHeight={labelSkipSize}
200+
labelSkipWidth={labelSkipWidth}
201+
labelSkipHeight={labelSkipHeight}
138202
labelTextColor={{ from: 'color', modifiers: [['darker', 1.6]] }}
203+
{ ...extraProperties}
139204
legends={(legend) ? [
140205
{
141206
dataFrom: 'keys',

src/config/ReportConfig.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,22 @@ export const REPORT_TYPES = {
301301
values: [true, false],
302302
default: false
303303
},
304+
"labelSkipWidth": {
305+
label: "Skip label on width (px)",
306+
type: SELECTION_TYPES.NUMBER,
307+
default: 0
308+
},
309+
"labelSkipHeight": {
310+
label: "Skip label on height (px)",
311+
type: SELECTION_TYPES.NUMBER,
312+
default: 0
313+
},
314+
"positionLabel": {
315+
label: "Custom label position",
316+
type: SELECTION_TYPES.LIST,
317+
values: ["off", "top", "bottom"],
318+
default: "off"
319+
},
304320
"labelRotation": {
305321
label: "Label Rotation (degrees)",
306322
type: SELECTION_TYPES.NUMBER,

0 commit comments

Comments
 (0)